This commit is contained in:
Alec Obradovich 2019-05-05 14:30:58 -05:00
parent ed799d158a
commit 466c1e5cc7
15 changed files with 296 additions and 45 deletions

153
src/conf/ini.c Normal file
View file

@ -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);
}

31
src/conf/ini.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef GLV_CONF_INI_H
#define GLV_CONF_INI_H
#include <stdio.h>
#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

View file

@ -1,12 +1,10 @@
#include <stdio.h>
#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;
}

View file

@ -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 <stdio.h>
#include <string.h>

View file

@ -31,7 +31,7 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "util/ipaddr.h"
#include "ipaddr.h"
#include "util/thread.h"
#define GLV_TCP_FLAG_TYPE 1

View file

@ -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]);

View file

@ -1,6 +1,7 @@
#ifndef GLV_UTIL_CONTAINERS_H
#define GLV_UTIL_CONTAINERS_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@ -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 **/

View file

@ -1,5 +1,7 @@
#include "etc.h"
#include <string.h>
#include <ctype.h>
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));
}

View file

@ -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

View file

@ -1,2 +0,0 @@
#include "ini.h"

View file

@ -1,8 +0,0 @@
#ifndef GLV_UTIL_INI_H
#define GLV_UTIL_INI_H
typedef struct {
} glv_ini_t;
#endif

View file

@ -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);

View file

@ -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;

View file

@ -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);