Skip to content
Snippets Groups Projects
Commit d0af24be authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Import helper functions from previous assignment

parent fcdd186f
Branches
No related merge requests found
/**
* 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;
}
/**
* Removes quotes from provided string.
*
* Return: Quote-less version of initial string.
*/
char* remove_quotes(char* input_string) {
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) {
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;
}
/**
* Brandon Rodriguez
* CS 3240
* 09-19-17
*/
/**
* A personal header for helper-functions.
*/
// 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);
char* remove_quotes(char* input_string);
char* remove_newline(char* input_string);
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment