From 8d5da04cb1c86044aa2703fe230ace094cf1786a Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sun, 24 Sep 2017 15:37:00 -0400
Subject: [PATCH] Add a few more functions into function helper files

---
 CopyFunctions.c   |  80 ----------------------
 HelperFunctions.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++
 HelperHeader.h    |   4 ++
 makefile          |   4 +-
 4 files changed, 170 insertions(+), 82 deletions(-)
 delete mode 100644 CopyFunctions.c
 create mode 100644 HelperFunctions.c

diff --git a/CopyFunctions.c b/CopyFunctions.c
deleted file mode 100644
index 643ab82..0000000
--- 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 0000000..9ab8f67
--- /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 11f2fe6..9f74301 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 9cf216c..ff7be5e 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:
-- 
GitLab