diff --git a/Main.c b/Main.c index 52269b31737224b47334917e129197b79f59585f..47a55efb9c84b8dc745b310c8ef1560725fe9d71 100644 --- a/Main.c +++ b/Main.c @@ -19,10 +19,10 @@ // Import headers. -#include <ctype.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#ifndef brodriguez_helper_functions + #define brodriguez_helper_functions + #include "brodriguez_helper_functions.h" +#endif // Constant Defines. @@ -40,5 +40,27 @@ */ int main(int argc, char* argv[]) { + // Ensure correct number of commands. + if (argc < 4) { + fprintf(stderr, "Too few arguments. Please provide sleep time, number of producers, and number of consumers.\n"); + exit(1); + } else if (argc > 4) { + fprintf(stderr, "Too many arguments. Please provide sleep time, number of producers, and number of consumers.\n"); + exit(1); + } + + // Initialize. + printf("Initializing...\n"); + + + // Create producer thread(s). + printf("Creating producers...\n"); + + + // Create consumer thread(s). + printf("Creating consumers...\n"); + + // Sleep main. + exit(0); } diff --git a/brodriguez_helper_functions.c b/brodriguez_helper_functions.c new file mode 100644 index 0000000000000000000000000000000000000000..93d03c50534fbac1d56b7d32fcaa1d47e4923a84 --- /dev/null +++ b/brodriguez_helper_functions.c @@ -0,0 +1,338 @@ +/** + * Brandon Rodriguez + * CS 4540 + * 02-13-18 + */ + + +/** + * 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. + */ + + +// Import headers. +#ifndef brodriguez_helper_functions + #define brodriguez_helper_functions + #include "brodriguez_helper_functions.h" +#endif + + +// ******************** // +// User Input Functions // +// ******************** // + +/** + * 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; +} + + +// ************** // +// 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 { + fprintf(stderr, "Could not allocate memory for string calloc."); + exit(1); + } + 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 { + fprintf(stderr, "Could not allocate memory for string calloc."); + exit(1); + } + 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 { + fprintf(stderr, "Could not allocate memory for int calloc."); + exit(1); + } + 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 { + fprintf(stderr, "Could not allocate memory for float calloc."); + exit(1); + } + 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 { + fprintf(stderr, "Could not allocate memory for double calloc."); + exit(1); + } + 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; +} diff --git a/brodriguez_helper_functions.h b/brodriguez_helper_functions.h new file mode 100644 index 0000000000000000000000000000000000000000..4dceec5994f1f482aec01dfc85b4d29ea5d9f27a --- /dev/null +++ b/brodriguez_helper_functions.h @@ -0,0 +1,53 @@ +/** + * Brandon Rodriguez + * CS 4540 + * 02-13-18 + */ + + +/** + * A personal header for helper-functions. + */ + + +// Import headers. +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +// *********************** // +// 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 // +// ******************* // + +// User Input Functions. +char* get_user_input(); +char* get_user_input_with_prompt(char* prompt); + +// Copy Functions. +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); + +// String Manipulation Functions. +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);