From c3838f0f17eda9b23177eeeb8ebca732095e1384 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 15 Nov 2017 08:59:54 -0500 Subject: [PATCH] Import code from previous assignments --- HelperFunctions.c | 327 ++++++++++++++++++++++++++++++++++++++++++++++ HelperHeader.h | 37 ++++++ Main.c | 54 ++++++++ 3 files changed, 418 insertions(+) create mode 100644 HelperFunctions.c create mode 100644 HelperHeader.h create mode 100644 Main.c diff --git a/HelperFunctions.c b/HelperFunctions.c new file mode 100644 index 0000000..1b9d4df --- /dev/null +++ b/HelperFunctions.c @@ -0,0 +1,327 @@ +/** + * 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) { + // Check for valid input. + if (source_ptr == NULL) { + return NULL; + } + 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 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) { + // Check for valid input. + if (source_ptr == NULL) { + return NULL; + } + 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; + 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) { + // Check for valid input. + if (input_string == NULL) { + return NULL; + } + 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) { + // Check for valid input. + if (input_string == NULL) { + return NULL; + } + 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) { + // Check for valid input. + if (input_string == NULL) { + return NULL; + } + 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; +} + + +/** + * Removes quotes from provided string. + * + * Return: Quote-less version of initial string. + */ +char* remove_quotes(char* input_string) { + // Check for valid input. + if (input_string == NULL) { + return NULL; + } + size_t string_length = strlen(input_string); + int orig_index; + int replace_index = 0; + + for (orig_index = 0; orig_index < string_length; orig_index++) { + if (input_string[orig_index] != '\"') { + input_string[replace_index] = input_string[orig_index]; + replace_index++; + } + } + + // Fill rest of values with null terminators. + while (replace_index < string_length) { + input_string[replace_index] = '\0'; + replace_index++; + } + + return input_string; +} + + +/** + * Removes newline character from string. + * + * Return: Newline-less version of initial string. + */ +char* remove_newline(char* input_string) { + // Check for valid input. + if (input_string == NULL) { + return NULL; + } + size_t string_length = strlen(input_string); + int orig_index; + int replace_index = 0; + + for (orig_index = 0; orig_index < string_length; orig_index++) { + if (input_string[orig_index] != '\n') { + input_string[replace_index] = input_string[orig_index]; + replace_index++; + } + } + + // Fill rest of values with null terminators. + while (replace_index < string_length) { + input_string[replace_index] = '\0'; + replace_index++; + } + + return input_string; +} + + +/** + * Gets user input from stdin. + */ +char* get_user_input() { + int get_input = 1; + char* user_input_buffer; + char* user_input; + + // Loop until input is given. + while (get_input == 1) { + user_input_buffer = calloc(4096, sizeof(char)); + printf("Enter input: "); + fgets(user_input_buffer, 4096, stdin); + + // Use copy_string function to remove extra buffer. + user_input = copy_string(user_input_buffer); + free(user_input_buffer); + + // Check that any input was given. + // Check that any input was given. + user_input = remove_newline(user_input); + if (strcmp(user_input, "") == 0) { + free(user_input); + } else { + get_input = 0; + } + } + + return user_input; +} + + +/** + * Gets user input from stdin and displays variable prompt. + */ +char* get_user_input_with_prompt(char* prompt) { + int get_input = 1; + char* user_input_buffer; + char* user_input; + + // Loop until input is given. + while (get_input == 1) { + user_input_buffer = calloc(4096, sizeof(char)); + printf("%s", prompt); + fgets(user_input_buffer, 4096, stdin); + + // Use copy_string function to remove extra buffer. + user_input = copy_string(user_input_buffer); + free(user_input_buffer); + + // Check that any input was given. + user_input = remove_newline(user_input); + if (strcmp(user_input, "") == 0) { + free(user_input); + } else { + get_input = 0; + } + } + + return user_input; +} diff --git a/HelperHeader.h b/HelperHeader.h new file mode 100644 index 0000000..402053e --- /dev/null +++ b/HelperHeader.h @@ -0,0 +1,37 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 09-19-17 + */ + + +/** + * 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); + +char* to_lower_case(char* input_string); +char* to_upper_case(char* input_string); +char* first_letter_upper(char* input_string); +char* remove_quotes(char* input_string); +char* remove_newline(char* input_string); + +char* get_user_input(); +char* get_user_input_with_prompt(); diff --git a/Main.c b/Main.c new file mode 100644 index 0000000..19d8a53 --- /dev/null +++ b/Main.c @@ -0,0 +1,54 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 11-15-17 + * a4 (Assignment 5) + */ + + +/** + * Description: + * Implement threads to solve a problem. + * + * In this "real world scenario" problem, originally a master file had a bunch of data. + * Somehow, someone "messed up real good" and now the master file no longer exists. + * Furthermore, all the data from the master file technically exists, but it's + * fragmented into smaller subfiles, and out of order. + * + * This program needs to be able to correct this issue, rebuilding the master file + * and sorting it as well. And it needs to use threads so that it doesn't take + * forever to accomplish (assuming the master file was originally really really big). + */ + + +/** + * Known Issues: + * + */ + + +// Import headers. +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include "apue.h" + + +// Define Vars. +//#define ??? + + +// Variables. + + +// Method Declaration. + + +/** + * Program's main. + * Initializes and runs program. + */ +int main(int argc, char* argv[]) { + +} -- GitLab