diff --git a/Makefile b/Makefile index 1f9edfc..75f31d0 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,4 @@ CC?=gcc BIN?=elem make: src/main.c - $(CC) -o $(BIN) src/map.c src/main.c \ No newline at end of file + $(CC) -o $(BIN) src/map.c src/loader.c src/command.c src/main.c \ No newline at end of file diff --git a/src/command.c b/src/command.c new file mode 100644 index 0000000..0b1b5a6 --- /dev/null +++ b/src/command.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include + +#define MAX_BUF_LENGTH 1024 +#define MAX_COMBO_LENGTH 1024 + +int fix_delim(char *command, char *needle) { + char *delim = strstr(command, needle); + + if (!delim) + return 0; + + delim[0] = ';'; + + return 1; +} + +int sort_comp(const void *a, const void *b) { + return strcmp(*(char **)a, *(char **)b); +} + +char *get_command(char *command, char *command_re, char **sort_tmp) { + fgets(command, MAX_BUF_LENGTH - 1, stdin); + + while (1) { + int get_out = 0; + get_out |= fix_delim(command, "+"); + get_out |= fix_delim(command, ","); + if (!get_out) + break; + } + int cl = strlen(command); + + command[cl - 1] = '\0'; + + for (int i = 0; i < cl - 1; i++) { + command[i] = tolower(command[i]); + } + + sort_tmp[0] = command; + int combos = 0; + for (int i = 1; i < MAX_COMBO_LENGTH; i++) { + combos = i; + sort_tmp[i] = strstr(command, ";"); + if (sort_tmp[i] == 0) + break; + sort_tmp[i][0] = '\0'; + sort_tmp[i]++; + } + + qsort(sort_tmp, combos, sizeof(char *), sort_comp); + + char *command_re_tmp = command_re; + for (int i = 0; i < combos; i++) { + if (i != 0) { + command_re_tmp[0] = ';'; + command_re_tmp++; + } + char *last = sort_tmp[i]; + strcpy(command_re_tmp, last); + command_re_tmp += strlen(last); + } + + return command_re; +} \ No newline at end of file diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..aead233 --- /dev/null +++ b/src/command.h @@ -0,0 +1 @@ +char *get_command(char *command, char *command_re, char **sort_tmp); \ No newline at end of file diff --git a/src/loader.c b/src/loader.c new file mode 100644 index 0000000..e712c63 --- /dev/null +++ b/src/loader.c @@ -0,0 +1,29 @@ +#include "map.h" +#include +#include +#include +#include + +#define MAX_FILE_SIZE 1024 * 16 + +void load_elements(hashmap *m, char *table) { + FILE *fptr; + + fptr = fopen(table, "r"); + + char *str; + + while (1) { + str = calloc(MAX_FILE_SIZE, sizeof(char)); + if (!fgets(str, MAX_FILE_SIZE, fptr)) + break; + + char *combo = strstr(str, ";"); + combo[0] = '\0'; + combo++; + + hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str); + } + + // todo: properly free this +} \ No newline at end of file diff --git a/src/loader.h b/src/loader.h new file mode 100644 index 0000000..2f1bdde --- /dev/null +++ b/src/loader.h @@ -0,0 +1,2 @@ +#include "map.h" +void load_elements(hashmap *m, char *table); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 2fe5817..463c8b7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,51 +1,13 @@ -#include "map.h" -#include #include #include #include #include +#include "command.h" +#include "loader.h" -#define MAX_FILE_SIZE 1024 * 16 #define MAX_BUF_LENGTH 1024 #define MAX_COMBO_LENGTH 1024 -void load_elements(hashmap *m, char *table) { - FILE *fptr; - - fptr = fopen(table, "r"); - - char *str; - - while (1) { - str = calloc(MAX_FILE_SIZE, sizeof(char)); - if (!fgets(str, MAX_FILE_SIZE, fptr)) - break; - - char *combo = strstr(str, ";"); - combo[0] = '\0'; - combo++; - - hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str); - } - - // todo: properly free this -} - -int fix_delim(char *command, char *needle) { - char *delim = strstr(command, needle); - - if (!delim) - return 0; - - delim[0] = ';'; - - return 1; -} - -int sort_comp(const void *a, const void *b) { - return strcmp(*(char **)a, *(char **)b); -} - int main(int argc, char *argv[]) { hashmap *elements = hashmap_create(); hashmap *inv = hashmap_create(); @@ -58,46 +20,8 @@ int main(int argc, char *argv[]) { while (1) { // todo: separate into functions - fgets(command, MAX_BUF_LENGTH - 1, stdin); - while (1) { - int get_out = 0; - get_out |= fix_delim(command, "+"); - get_out |= fix_delim(command, ","); - if (!get_out) - break; - } - int cl = strlen(command); - - command[cl - 1] = '\0'; - - for (int i = 0; i < cl - 1; i++) { - command[i] = tolower(command[i]); - } - - sort_tmp[0] = command; - int combos = 0; - for (int i = 1; i < MAX_COMBO_LENGTH; i++) { - combos = i; - sort_tmp[i] = strstr(command, ";"); - if (sort_tmp[i] == 0) - break; - sort_tmp[i][0] = '\0'; - sort_tmp[i]++; - } - - qsort(sort_tmp, combos, sizeof(char *), sort_comp); - - char *command_re_tmp = command_re; - for (int i = 0; i < combos; i++) { - if (i != 0) { - command_re_tmp[0] = ';'; - command_re_tmp++; - } - char *last = sort_tmp[i]; - strcpy(command_re_tmp, last); - command_re_tmp += strlen(last); - } + command_re = get_command(command, command_re, sort_tmp); uintptr_t result; hashmap_get(elements, command_re, strlen(command_re), &result);