From 466c1e5cc7fe1f0378bb9baa23bd3c96e69a6427 Mon Sep 17 00:00:00 2001 From: Alec Obradovich Date: Sun, 5 May 2019 14:30:58 -0500 Subject: [PATCH] woomini --- src/conf/ini.c | 153 ++++++++++++++++++++++++++++++++++++ src/conf/ini.h | 31 ++++++++ src/main.c | 8 +- src/{util => sock}/ipaddr.c | 0 src/{util => sock}/ipaddr.h | 4 +- src/sock/tcp.h | 2 +- src/util/containers.c | 51 +++++++----- src/util/containers.h | 13 ++- src/util/etc.c | 45 +++++++++++ src/util/etc.h | 18 +++++ src/util/ini.c | 2 - src/util/ini.h | 8 -- src/util/thread.h | 2 +- src/util/thread_posix.c | 2 +- src/util/thread_win.c | 2 +- 15 files changed, 296 insertions(+), 45 deletions(-) create mode 100644 src/conf/ini.c create mode 100644 src/conf/ini.h rename src/{util => sock}/ipaddr.c (100%) rename src/{util => sock}/ipaddr.h (91%) delete mode 100644 src/util/ini.c delete mode 100644 src/util/ini.h diff --git a/src/conf/ini.c b/src/conf/ini.c new file mode 100644 index 0000000..1acc079 --- /dev/null +++ b/src/conf/ini.c @@ -0,0 +1,153 @@ +#include "ini.h" + + + +glv_ini_t* glv_ini_read_file(const char* path) { + char *split, line[GLV_INI_MAX_LN + 1]; + glv_map_t* section = NULL; + glv_ini_t* ini = NULL; + FILE* file; + int length; + + if((file = fopen(path, "r")) == NULL) { + printf(GLV_ERR "INI file '%s' was not found or not readable.", path); + return NULL; + } + + ini = glv_map_create(); + while(fgets(line, sizeof(line), file) != NULL) { + trim(line); + if(line[0] == '\0' || line[0] == ';') + continue; + + length = strlen(line); + if(length == GLV_INI_MAX_LN && line[length - 1] != '\n' && !feof(file)) + { + printf(GLV_ERR + "INI file '%s' has line that exceeds %i bytes.", + path, GLV_INI_MAX_LN + ); + + glv_ini_destroy(ini); + fclose(file); + return NULL; + } + + if(line[0] == '[') { + if(line[length - 2] == ']') { + line[length - 2] = '\0'; + strlower(trim(line + 1)); + + if(!glv_map_has_key(ini, line + 1)) { + section = glv_map_create(); + glv_map_set_copy(ini, line + 1, section, GLV_STRLEN); + } + } else { + printf(GLV_ERR + "INI file '%s' contains malformed section header.", path + ); + + glv_ini_destroy(ini); + fclose(file); + return NULL; + } + } else { + split = strchr(line, '='); + if(section != NULL && split != NULL) { + *split = '\0'; + trim(split + 1); + strlower(trim(line)); + + if(glv_map_has_key(ini, line)) { + printf(GLV_WARN + "INI file '%s' redefines key '%s' in same section.", + path, line + ); + } + + glv_map_setf_copy(section, line, split + 1, GLV_STRLEN); + } else { + printf(GLV_ERR + "INI file '%s' contains malformed assignment.", path + ); + + glv_ini_destroy(ini); + fclose(file); + return NULL; + } + } + } + + return ini; +} + +int glv_ini_has_section(glv_ini_t* ini, const char* section) { + char* section_lc = strlower(strdup(section)); + int check = glv_map_has_key(ini, section_lc); + + free(section_lc); + return check; +} + +int glv_ini_section_has_key + (glv_ini_t* ini, const char* section, const char* key) +{ + char *section_lc = strlower(strdup(section)), + *key_lc = strlower(strdup(key)); + glv_map_t* section_map; + int check = 0; + + section_map = glv_map_get(ini, section_lc); + if(section_map != NULL) + check = glv_map_has_key(section_map, key_lc); + + free(section_lc); + free(key_lc); + return check; +} + +char* glv_ini_get(glv_ini_t* ini, const char* section, const char* key) { + char *section_lc = strlower(strdup(section)), + *key_lc = strlower(strdup(key)), + *value = NULL; + glv_map_t* section_map; + + section_map = glv_map_get(ini, section_lc); + if(section_map != NULL) + value = glv_map_get(section_map, key_lc); + + free(section_lc); + free(key_lc); + return value; +} + +int glv_ini_get_type + (glv_ini_t* ini, const char* section, const char* key, int type, void* out) +{ + char *value = glv_ini_get(ini, section, key); + if(value == NULL) + return 0; + + switch(type) { + case GLV_INI_INT: + case GLV_INI_INT_HEX: + *(int*)out = (int) + strtol(value, NULL, (type == GLV_INI_INT ? 10 : 16)); + break; + case GLV_INI_UINT: + case GLV_INI_UINT_HEX: + *(unsigned int*)out = (unsigned int) + strtoul(value, NULL, (type == GLV_INI_UINT ? 10 : 16)); + break; + case GLV_INI_DOUBLE: + *(double*)out = strtod(value, NULL); + break; + } +} + +void glv_ini_destroy(glv_ini_t* ini) { + if(ini == NULL) + return; + + glv_map_destroyf_func(ini, glv_map_destroyf); +} \ No newline at end of file diff --git a/src/conf/ini.h b/src/conf/ini.h new file mode 100644 index 0000000..50a711f --- /dev/null +++ b/src/conf/ini.h @@ -0,0 +1,31 @@ +#ifndef GLV_CONF_INI_H +#define GLV_CONF_INI_H + +#include + +#include "util/containers.h" +#include "util/etc.h" + +#define GLV_INI_INT 0 +#define GLV_INI_INT_HEX 1 +#define GLV_INI_UINT 2 +#define GLV_INI_UINT_HEX 3 +#define GLV_INI_DOUBLE 4 + +#define GLV_INI_MAX_LN 2048 + +typedef glv_map_t glv_ini_t; + +glv_ini_t* glv_ini_read_file(const char* path); + +int glv_ini_has_section(glv_ini_t* ini, const char* section); +int glv_ini_section_has_key + (glv_ini_t* ini, const char* section, const char* key); + +char* glv_ini_get(glv_ini_t* ini, const char* section, const char* key); +int glv_ini_get_type + (glv_ini_t* ini, const char* section, const char* key, int type, void* out); + +void glv_ini_destroy(glv_ini_t* ini); + +#endif diff --git a/src/main.c b/src/main.c index 403e6cc..1a549f4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,10 @@ #include -#include "util/ipaddr.h" +#include "sock/ipaddr.h" #include "util/containers.h" +#include "util/etc.h" int main(int argc, char** argv) { - glv_map_t* map = glv_map_create(); - glv_map_set_copy(map, "test", "Hello!", 0); - glv_map_set_copy(map, "amazing", "Hello!!", 0); - glv_map_set_copy(map, "holy me WOW", "Hello!!", 0); + printf("Starting Glove Chat server..."); return 0; } \ No newline at end of file diff --git a/src/util/ipaddr.c b/src/sock/ipaddr.c similarity index 100% rename from src/util/ipaddr.c rename to src/sock/ipaddr.c diff --git a/src/util/ipaddr.h b/src/sock/ipaddr.h similarity index 91% rename from src/util/ipaddr.h rename to src/sock/ipaddr.h index deb52d8..4b65e44 100644 --- a/src/util/ipaddr.h +++ b/src/sock/ipaddr.h @@ -1,5 +1,5 @@ -#ifndef GLV_UTIL_IPADDR_H -#define GLV_UTIL_IPADDR_H +#ifndef GLV_SOCK_IPADDR_H +#define GLV_SOCK_IPADDR_H #include #include diff --git a/src/sock/tcp.h b/src/sock/tcp.h index 4eafd9a..5cfe0f4 100644 --- a/src/sock/tcp.h +++ b/src/sock/tcp.h @@ -31,7 +31,7 @@ #include #include -#include "util/ipaddr.h" +#include "ipaddr.h" #include "util/thread.h" #define GLV_TCP_FLAG_TYPE 1 diff --git a/src/util/containers.c b/src/util/containers.c index a7b44f4..5e7d3b5 100644 --- a/src/util/containers.c +++ b/src/util/containers.c @@ -4,15 +4,16 @@ #define GLV_MAP_DEFAULT_BUCKETS 8 -static int glv_map_hash_func(const char* key) { - int hash = 7, length = strlen(key), i; +static uint32_t glv_map_hash_func(const char* key) { + uint32_t hash = 7; + size_t length = strlen(key), i; + for(i = 0; i < length; ++i) hash = hash * 31 + key[i]; - return hash; } -glv_map_t* glv_map_create() { +glv_map_t* glv_map_create(void) { return glv_map_create_ex(GLV_MAP_DEFAULT_BUCKETS); } @@ -27,7 +28,7 @@ glv_map_t* glv_map_create_ex(int initial_size) { } void* glv_map_get(glv_map_t* map, const char* key) { - int hash = glv_map_hash_func(key) % map->bucket_count, i; + uint32_t hash = glv_map_hash_func(key) % map->bucket_count, i; for(i = 0; i < map->bucket_lengths[hash]; ++i) if(map->buckets[hash][i].key != NULL && @@ -41,9 +42,8 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value) { if(value == NULL) return NULL; - int hash = glv_map_hash_func(key), i; + uint32_t hash = glv_map_hash_func(key), i; glv_pair_t* pair = NULL; - char* copy; void* tmp; if(glv_map_has_key(map, key)) { @@ -75,10 +75,7 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value) { pair = &(map->buckets[hash][map->bucket_lengths[hash] - 1]); } - copy = malloc((strlen(key) + 1) * sizeof(char)); - strcpy(copy, key); - - pair->key = copy; + pair->key = strdup(key); pair->value = value; ++(map->pair_count); } @@ -89,6 +86,7 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value) { void* glv_map_set_copy (glv_map_t* map, const char* key, void* value, size_t length) { + length = (length == 0 ? strlen(value) + 1 : length); void* copy = malloc(length); memcpy(copy, value, length); @@ -96,8 +94,18 @@ void* glv_map_set_copy return glv_map_set(map, key, copy); } +void glv_map_setf(glv_map_t* map, const char* key, void* value) { + free(glv_map_set(map, key, value)); +} + +void glv_map_setf_copy + (glv_map_t* map, const char* key, void* value, size_t length) +{ + free(glv_map_set_copy(map, key, value, length)); +} + void* glv_map_remove(glv_map_t* map, const char* key) { - int hash = glv_map_hash_func(key) % map->bucket_count, i; + uint32_t hash = glv_map_hash_func(key) % map->bucket_count, i; for(i = 0; i < map->bucket_lengths[hash]; ++i) { if(map->buckets[hash][i].key != NULL && strcmp(map->buckets[hash][i].key, key) == 0) @@ -111,18 +119,18 @@ void* glv_map_remove(glv_map_t* map, const char* key) { return NULL; } -void glv_map_remove_dealloc(glv_map_t* map, const char* key) { +void glv_map_removef(glv_map_t* map, const char* key) { free(glv_map_remove(map, key)); } int glv_map_has_key(glv_map_t* map, const char* key) { - return glv_map_get(map, key) == NULL; + return glv_map_get(map, key) != NULL; } void glv_map_resize(glv_map_t* map, int size) { glv_pair_t** new_buckets = calloc(size, sizeof(glv_pair_t*)); int* new_lengths = calloc(size, sizeof(int)); - int i, j, hash; + uint32_t i, j, hash; for(i = 0; i < map->bucket_count; ++i) { for(j = 0; j < map->bucket_lengths[i]; ++j) { @@ -147,21 +155,24 @@ void glv_map_resize(glv_map_t* map, int size) { } void glv_map_destroy(glv_map_t* map) { - glv_map_destroy_dealloc_func(map, NULL); + glv_map_destroyf_func(map, NULL); } -void glv_map_destroy_dealloc(glv_map_t* map) { - glv_map_destroy_dealloc_func(map, free); +void glv_map_destroyf(glv_map_t* map) { + glv_map_destroyf_func(map, free); } -void glv_map_destroy_dealloc_func(glv_map_t* map, glv_map_dealloc_func func) { +void glv_map_destroyf_func(glv_map_t* map, glv_map_dealloc_func func) { int i, j; + if(map == NULL) + return; + for(i = 0; i < map->bucket_count; ++i) { for(j = 0; j < map->bucket_lengths[i]; ++j) { free(map->buckets[i][j].key); if(func != NULL) - func(map); + func(map->buckets[i][j].value); } free(map->buckets[i]); diff --git a/src/util/containers.h b/src/util/containers.h index e2403f7..a0db2ae 100644 --- a/src/util/containers.h +++ b/src/util/containers.h @@ -1,6 +1,7 @@ #ifndef GLV_UTIL_CONTAINERS_H #define GLV_UTIL_CONTAINERS_H +#include #include #include @@ -20,7 +21,7 @@ typedef struct { typedef void (*glv_map_dealloc_func)(void*); -glv_map_t* glv_map_create(); +glv_map_t* glv_map_create(void); glv_map_t* glv_map_create_ex(int initial_size); void* glv_map_get(glv_map_t* map, const char* key); @@ -29,15 +30,19 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value); void* glv_map_set_copy (glv_map_t* map, const char* key, void* value, size_t length); +void glv_map_setf(glv_map_t* map, const char* key, void* value); +void glv_map_setf_copy + (glv_map_t* map, const char* key, void* value, size_t length); + void* glv_map_remove(glv_map_t* map, const char* key); -void glv_map_remove_dealloc(glv_map_t* map, const char* key); +void glv_map_removef(glv_map_t* map, const char* key); int glv_map_has_key(glv_map_t* map, const char* key); void glv_map_resize(glv_map_t* map, int size); void glv_map_destroy(glv_map_t* map); -void glv_map_destroy_dealloc(glv_map_t* map); -void glv_map_destroy_dealloc_func(glv_map_t* map, glv_map_dealloc_func func); +void glv_map_destroyf(glv_map_t* map); +void glv_map_destroyf_func(glv_map_t* map, glv_map_dealloc_func func); /** STRING HASHMAP END **/ diff --git a/src/util/etc.c b/src/util/etc.c index 28ec62c..0cced3b 100644 --- a/src/util/etc.c +++ b/src/util/etc.c @@ -1,5 +1,7 @@ #include "etc.h" + #include +#include int axtoi(const char* str) { int value = 0, i; @@ -23,4 +25,47 @@ int strcnt(const char* str, char c) { ++cnt; return cnt; +} + +char* strtrans(char* str, trans_func_t func) { + int length = strlen(str), i; + for(i = 0; i < length; ++i) + str[i] = func(str[i]); + + return str; +} + +char* strlower(char* str) { + return strtrans(str, tolower); +} + +char* strupper(char* str) { + return strtrans(str, toupper); +} + +char* ltrim(char* str) { + int length = strlen(str), i; + for(i = 0; i < length; ++i) + if(str[i] > ' ') + break; + if(i > 0) + strcpy(str, str + i); + + return str; +} + +char* rtrim(char* str) { + int length = strlen(str), i; + for(i = length - 1; i >= 0; --i) { + if(str[i] <= ' ') + str[i] = '\0'; + else + break; + } + + return str; +} + +char* trim(char* str) { + return ltrim(rtrim(str)); } \ No newline at end of file diff --git a/src/util/etc.h b/src/util/etc.h index 003d89e..38fbce6 100644 --- a/src/util/etc.h +++ b/src/util/etc.h @@ -4,8 +4,26 @@ #define MAX(X,Y) (((X)>(Y))?(X):(Y)) #define MIN(X,Y) (((X)<(Y))?(X):(Y)) +// STDOUT DEFS +#define GLV_INFO "[ INFO ] " +#define GLV_WARN "[ WARN ] " +#define GLV_ERR "[ ERROR ] " +#define GLV_CRIT "[ CRITICAL ] " + // AUX STRING FUNCS +#define GLV_STRLEN 0 + +typedef char(*trans_func_t)(char); + +char* strtrans(char* str, trans_func_t func); +char* strlower(char* str); +char* strupper(char* str); + int axtoi(const char* str); int strcnt(const char* str, char c); +char* ltrim(char* str); +char* rtrim(char* str); +char* trim(char* str); + #endif diff --git a/src/util/ini.c b/src/util/ini.c deleted file mode 100644 index a6420b1..0000000 --- a/src/util/ini.c +++ /dev/null @@ -1,2 +0,0 @@ -#include "ini.h" - diff --git a/src/util/ini.h b/src/util/ini.h deleted file mode 100644 index e2c5681..0000000 --- a/src/util/ini.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef GLV_UTIL_INI_H -#define GLV_UTIL_INI_H - -typedef struct { - -} glv_ini_t; - -#endif diff --git a/src/util/thread.h b/src/util/thread.h index d292474..2070fd9 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -29,7 +29,7 @@ void glv_thread_destroy(glv_thread_t* thread); /**********************/ /** BEGIN MUTX DECLS **/ -glv_mutex_t* glv_mutex_create(); +glv_mutex_t* glv_mutex_create(void); void glv_mutex_lock(glv_mutex_t* mutex); void glv_mutex_unlock(glv_mutex_t* mutex); void glv_mutex_destroy(glv_mutex_t* mutex); diff --git a/src/util/thread_posix.c b/src/util/thread_posix.c index 4afe156..8a43f7c 100644 --- a/src/util/thread_posix.c +++ b/src/util/thread_posix.c @@ -37,7 +37,7 @@ void glv_thread_destroy(glv_thread_t* thread) { /*********************/ /** BEGIN MUTX IMPL **/ -glv_mutex_t* glv_mutex_create() { +glv_mutex_t* glv_mutex_create(void) { glv_mutex_t* mtx = malloc(sizeof(glv_mutex_t)); if(pthread_mutex_init(mtx, NULL) == 0) return mtx; diff --git a/src/util/thread_win.c b/src/util/thread_win.c index 69619ff..680d3fc 100644 --- a/src/util/thread_win.c +++ b/src/util/thread_win.c @@ -21,7 +21,7 @@ void glv_thread_destroy(glv_thread_t* thread) { /*********************/ /** BEGIN MUTX IMPL **/ -glv_mutex_t* glv_mutex_create() { +glv_mutex_t* glv_mutex_create(void) { glv_mutex_t* mutex = malloc(sizeof(glv_mutex_t)); *mutex = CreateMutexA(NULL, NULL, NULL);