From 7d13a69f560d4ad6b1ee1717ee805edd749cc276 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 5 Oct 2017 11:54:02 -0400 Subject: [PATCH] Update helper header/functions Add ANSI text color codes and second string helper function. --- HelperFunctions.c | 34 ++++++++++++++++++++++++++-------- HelperHeader.h | 10 ++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/HelperFunctions.c b/HelperFunctions.c index e36edb6..3695395 100644 --- a/HelperFunctions.c +++ b/HelperFunctions.c @@ -39,8 +39,8 @@ * * Return: Copy of source string. */ -char* copy_string(char *source_ptr) { - char *copy_ptr; +char* copy_string(char* source_ptr) { + char* copy_ptr; copy_ptr = calloc((strlen(source_ptr) + 1), sizeof(char)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, ((strlen(source_ptr) + 1) * sizeof(char))); @@ -51,13 +51,31 @@ char* copy_string(char *source_ptr) { } +/** + * Copies string from destination to source. + * Uses a buffer of indicated size. + * + * Return: Copy of source string. + */ +char* copy_string_with_buffer(char* source_ptr, int buffer_size) { + char* copy_ptr; + copy_ptr = calloc(buffer_size, sizeof(char)); + if (copy_ptr != NULL) { + memcpy(copy_ptr, source_ptr, ((strlen(source_ptr) + 1) * sizeof(char))); + } else { + err_dump("Could not allocate memory for string calloc."); + } + return copy_ptr; +} + + /** * Copies float from destination to source. * * Return: Copy of source int. */ -int* copy_int(int *source_ptr) { - int *copy_ptr; +int* copy_int(int* source_ptr) { + int* copy_ptr; copy_ptr = calloc(1, (sizeof(int) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(int)); @@ -73,8 +91,8 @@ int* copy_int(int *source_ptr) { * * Return: Copy of source float. */ -float* copy_float(float *source_ptr) { - float *copy_ptr; +float* copy_float(float* source_ptr) { + float* copy_ptr; copy_ptr = calloc(1, (sizeof(float) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(float)); @@ -90,8 +108,8 @@ float* copy_float(float *source_ptr) { * * Return: Copy of source double. */ -double* copy_double(double *source_ptr) { - double *copy_ptr; +double* copy_double(double* source_ptr) { + double* copy_ptr; copy_ptr = calloc(1, (sizeof(double) + 1)); if (copy_ptr != NULL) { memcpy(copy_ptr, source_ptr, sizeof(double)); diff --git a/HelperHeader.h b/HelperHeader.h index 3d52d6f..7634698 100644 --- a/HelperHeader.h +++ b/HelperHeader.h @@ -9,10 +9,20 @@ * A personal header for helper-functions. */ +// ANSI color escape codes. +#define ANSI_COLOR_RESET "\x1b[0m" +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" + // Function prototypes. char* copy_string(char* source_ptr); +char* copy_string_with_buffer(char* source_ptr, int buffer_size); int* copy_int(int* source_ptr); float* copy_float(float* source_ptr); double* copy_double(double* source_ptr); -- GitLab