diff --git a/CopyFunctions.c b/CopyFunctions.c deleted file mode 100644 index 643ab8214883eaa0af4f2a1d1e6e0d29595a974e..0000000000000000000000000000000000000000 --- a/CopyFunctions.c +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Brandon Rodriguez - * CS 3240 - * 09-19-17 - */ - - -/** - * Helper functions to have consistent value copying and error handling. - * - * All functions use memcpy to copy data, and error.c's err_dump on failure. - * - * All functions require a "source" pointer of equivalent typing, and returns - * a pointer to copied item's location. - */ - - -#include <stdlib.h> -#include <string.h> -#include "apue.h" - - -/** - * Copies string from destination to source. - */ -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))); - } else { - err_dump("Could not allocate memory for string calloc."); - } - return copy_ptr; -} - - -/** - * Copies float from destination to source. - */ -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)); - } else { - err_dump("Could not allocate memory for int calloc."); - } - return copy_ptr; -} - - -/** - * Copies float from destination to source. - */ -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)); - } else { - err_dump("Could not allocate memory for float calloc."); - } - return copy_ptr; -} - - -/** - * Copies double from destination to source. - */ -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)); - } else { - err_dump("Could not allocate memory for double calloc."); - } - return copy_ptr; -} diff --git a/HelperFunctions.c b/HelperFunctions.c new file mode 100644 index 0000000000000000000000000000000000000000..9ab8f671dd4f14b1a05b9810e7f51683f451136d --- /dev/null +++ b/HelperFunctions.c @@ -0,0 +1,164 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 09-19-17 + */ + + +/** + * Helper functions to have consistent value copying and error handling. + * + * + * Copy Functions: + * Copy functions use memcpy to copy data, and error.c's err_dump on + * failure. + * + * Copy functions require a "source" pointer of equivalent typing, and + * return a pointer to copied item's location. + * + * + * String Functions: + * String manipulator functions to make string handling easier. + * + * String manipulator functions require an "input" string and return + * a modified copy of original string. + */ + + +#include <ctype.h> +#include <string.h> +#include "apue.h" + + +// ************** // +// Copy Functions // +// ************** // + +/** + * Copies string from destination to source. + * + * Return: Copy of source string. + */ +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))); + } 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; + copy_ptr = calloc(1, (sizeof(int) + 1)); + if (copy_ptr != NULL) { + memcpy(copy_ptr, source_ptr, sizeof(int)); + } else { + err_dump("Could not allocate memory for int calloc."); + } + return copy_ptr; +} + + +/** + * Copies float from destination to source. + * + * Return: Copy of source float. + */ +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)); + } else { + err_dump("Could not allocate memory for float calloc."); + } + return copy_ptr; +} + + +/** + * Copies double from destination to source. + * + * Return: Copy of source double. + */ +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)); + } else { + err_dump("Could not allocate memory for double calloc."); + } + return copy_ptr; +} + + +// ***************************** // +// String Manipulation Functions // +// ***************************** // + +/** + * Converts provided string to lowercase. + * + * Return: Lowercase version of initial string. + */ +char* to_lower_case(char* input_string) { + int index = 0; + char* return_string = calloc(strlen((input_string) + 1), sizeof(char)); + while (input_string[index]) { + return_string[index] = tolower(input_string[index]); + index++; + } + return return_string; +} + + +/** + * Converts provided string to uppercase. + * + * Return: Uppercase version of initial string. + */ +char* to_upper_case(char* input_string) { + int index = 0; + char* return_string = calloc((strlen(input_string) + 1), sizeof(char)); + while (input_string[index]) { + return_string[index] = toupper(input_string[index]); + index++; + } + return return_string; +} + + +/** + * Converts provided string to: + * First letter of each word is uppercase. + * All other letters lowercase. + * + * Return: Converted version of initial string. + */ +char* first_letter_upper(char* input_string) { + int index = 0; + char* return_string = calloc((strlen(input_string) + 1), sizeof(char)); + while (input_string[index]) { + if (index == 0) { // First char is always upper. + return_string[index] = toupper(input_string[index]); + } else { // Check all other chars. If space preceeds, then upper. + if (input_string[index - 1] == ' ') { + return_string[index] = toupper(input_string[index]); + } else { // No space found. + return_string[index] = tolower(input_string[index]); + } + } + index++; + } + return return_string; +} diff --git a/HelperHeader.h b/HelperHeader.h index 11f2fe63b0708ac49268069008a817dd90488ee9..9f7430110943b229ff594fb1937f9e22300049bf 100644 --- a/HelperHeader.h +++ b/HelperHeader.h @@ -11,8 +11,12 @@ // Function prototypes. + char* copy_string(char* source_ptr); int* copy_int(int* source_ptr); float* copy_float(float* source_ptr); double* copy_double(double* source_ptr); +char* to_lower_case(char* input_string); +char* to_upper_case(char* input_string); +char* first_letter_upper(char* input_string); diff --git a/makefile b/makefile index 9cf216c559fd501c0ce214833fb4e3bd3f35e490..ff7be5ecffa57a0c971abf444c96434b76dc4181 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ all: - gcc -Wall -Wpedantic -std=c99 -g BuildDataBase.c error.c CopyFunctions.c -o BuildDataBase - gcc -Wall -Wpedantic -std=c99 -g UseDataBase.c error.c CopyFunctions.c -o UseDataBase + gcc -Wall -Wpedantic -std=c99 -g BuildDataBase.c error.c HelperFunctions.c -o BuildDataBase + gcc -Wall -Wpedantic -std=c99 -g UseDataBase.c error.c HelperFunctions.c -o UseDataBase build: ./BuildDataBase use: