You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
4.6 KiB
187 lines
4.6 KiB
#include "util.h" |
|
|
|
void *dualloc(void *thing, size_t size) { |
|
|
|
void *dupl = malloc(size); |
|
memset(dupl, 0, size); |
|
memcpy(dupl, thing, size); |
|
|
|
return dupl; |
|
} |
|
|
|
/* Read a line into a newly created buffer */ |
|
int get_line(FILE* in, char **out) { |
|
int size_of_buffer = INITIAL_LINE_BUFFER; |
|
char *buffer = calloc(size_of_buffer, sizeof(char)); |
|
char character; |
|
int count = 0; |
|
|
|
while (true) { |
|
|
|
character = fgetc(in); |
|
if (character == EOF) { |
|
buffer[count] = '\0'; |
|
*out = buffer; |
|
return EOF; |
|
} |
|
if (character == '\n') { |
|
buffer[count] = '\0'; |
|
*out = buffer; |
|
return ER_SUCCESS; |
|
} else { |
|
if (count == (size_of_buffer - 3)) { |
|
buffer = reallocarray(buffer, (size_of_buffer *= 2), |
|
sizeof(char)); |
|
|
|
if (!buffer) { |
|
return ER_ALLOC; |
|
} |
|
} |
|
|
|
buffer[count] = character; |
|
count++; |
|
} |
|
} |
|
} |
|
|
|
/* Join an array of strings together using the delim string as the joiner. */ |
|
int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) { |
|
int length = 0; |
|
int delim_len = strlen(delim); |
|
|
|
char *new_string = calloc(INITIAL_LINE_BUFFER, sizeof(char)); |
|
int stringsize = INITIAL_LINE_BUFFER; |
|
|
|
for (int i = 0; i < arr_len; i++) { |
|
if (length == stringsize - 2) { |
|
new_string = reallocarray(new_string, stringsize *= 2, |
|
sizeof(char)); |
|
} |
|
|
|
int len = strlen(sep_stirng[i]); |
|
for (int k = 0; k < len; k++) { |
|
new_string[length + k] = sep_stirng[i][k]; |
|
} |
|
|
|
length += len; |
|
|
|
if (i != (arr_len - 1) && sep_stirng[i+1] == 0) { |
|
*joined = new_string; |
|
return ER_SUCCESS; |
|
} else { |
|
for (int k = 0; k < delim_len; k++) { |
|
new_string[length + k] = delim[k]; |
|
} |
|
length += delim_len; |
|
} |
|
} |
|
*joined = new_string; |
|
return ER_SUCCESS; |
|
|
|
return ER_FAILURE; |
|
} |
|
|
|
/* Remove all instances of char del from the given string (in place). */ |
|
int strip_char(char *string, char del) { |
|
int len = strlen(string); |
|
bool move = false; |
|
|
|
for (int i = 0; i < len; i++) { |
|
if (move && i != len-1) { |
|
string[i] = string[i + 1]; |
|
} else if (move) { |
|
string[i] ='\0'; |
|
} |
|
|
|
if (string[i] == del) { |
|
move = true; |
|
len -= 1; |
|
if (move && i != len-1) { |
|
string[i] = string[i + 1]; |
|
} else if (move) { |
|
string[i] ='\0'; |
|
} |
|
} |
|
} |
|
|
|
if (!move) { |
|
return ER_SUCCESS; |
|
} else { |
|
strip_char(string, del); |
|
} |
|
|
|
return ER_FAILURE; |
|
} |
|
|
|
|
|
/* Take a string and split it into an array of strings, separate by the delim |
|
* character. |
|
*/ |
|
int split_string(char **sep_string[], int *num, char *string, char delim) { |
|
char **word_list = calloc(10, sizeof(char*)); |
|
int word_list_size = 10; |
|
int word_size; |
|
|
|
char *word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char)); |
|
int num_words = 0; |
|
int word_length = 0; |
|
|
|
for (int i = 0; true; i++) { |
|
|
|
if (word_list_size - 1 == num_words) { |
|
word_list_size += 1; |
|
word_list = reallocarray(word_list, word_list_size, sizeof(char*)); |
|
} |
|
|
|
if (string[i] == '\0') { |
|
if (word_length) { |
|
word_list[num_words] = word; |
|
num_words++; |
|
} |
|
|
|
*sep_string = word_list; |
|
*num = num_words; |
|
return ER_SUCCESS; |
|
} |
|
|
|
if (string[i] == delim) { |
|
if (word_length) { |
|
word_list[num_words] = word; |
|
|
|
num_words++; |
|
word_length = 0; |
|
word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char)); |
|
} |
|
} else { |
|
word[word_length] = string[i]; |
|
word_length++; |
|
|
|
if (word_length == word_size - 2) { |
|
word = reallocarray(word, word_size *= 2, sizeof(char)); |
|
} |
|
} |
|
} |
|
|
|
return ER_FAILURE; |
|
} |
|
|
|
|
|
int print_chicken(State *state) { |
|
char *chicken[] = { |
|
" _\n", |
|
" (o)\n", |
|
" / |\n", |
|
" / |==========\n", |
|
" \\ CHSH /\n", |
|
"o____o \\______/\n", |
|
" |||| |\n", |
|
" |||| _|\n", |
|
" ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\n\n"}; |
|
|
|
|
|
for (int i = 0; i < 8; i ++) { |
|
fprintf(state->output, "%s", chicken[i]); |
|
} |
|
|
|
return ER_SUCCESS; |
|
}
|
|
|