diff --git a/Makefile.am b/Makefile.am index 7bc4a48..edb35cf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ AUTOMAKE_OPTIONS = foreign subdir-objects -Wall bin_PROGRAMS = forge -forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c src/strings.c $(top_srcdir)/include/glad/gl.h $(top_srcdir)/hashmap.c/hashmap.c +forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c src/strings.c src/config_file.c $(top_srcdir)/include/glad/gl.h $(top_srcdir)/hashmap.c/hashmap.c forge_CFLAGS = -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -I$(top_srcdir)/include -DGLFW_INCLUDE_NONE forge_LDADD = -lm -lGL -lglfw -include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h src/strings.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/linmath.h $(top_srcdir)/include/hashmap.h \ No newline at end of file +include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h src/strings.h src/config_file.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/linmath.h $(top_srcdir)/include/hashmap.h \ No newline at end of file diff --git a/configure.ac b/configure.ac index e9335da..db80341 100644 --- a/configure.ac +++ b/configure.ac @@ -6,6 +6,7 @@ AC_CHECK_HEADERS([stdlib.h]) AC_CHECK_HEADERS([stdbool.h]) AC_CHECK_HEADERS([stddef.h]) AC_CHECK_HEADERS([string.h]) +AC_CHECK_HEADERS([stdint.h]) AC_CHECK_HEADERS([time.h]) AC_CHECK_HEADERS([math.h]) AC_CHECK_HEADERS([sys/stat.h]) diff --git a/src/config_file.c b/src/config_file.c new file mode 100644 index 0000000..58f0c2f --- /dev/null +++ b/src/config_file.c @@ -0,0 +1,73 @@ +#include +#include + +#include "config_file.h" +#include "file.h" +#include "types.h" + +static int item_compare(const void *a, const void *b, + __attribute__((unused)) void *udata) { + const ConfigFileItem *c_item_a; + const ConfigFileItem *c_item_b; + + c_item_a = a; + c_item_b = b; + + return strcmp(c_item_a->key, c_item_b->key); +} + +static uint64_t item_hash(const void *item, uint64_t seed0, uint64_t seed1) { + const ConfigFileItem *c_item; + + c_item = item; + + return hashmap_sip(c_item->key, strlen(c_item->key), seed0, seed1); +} + +ConfigFile config_file_read(char *path, bool free_path) { + File file; + ConfigFile output; + + file = file_read(path); + + output.map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash, + item_compare, NULL, NULL); + + // TODO + + file_free(&file, free_path); + + return output; +} + +char *config_file_get_str(ConfigFile config, char *key, char *default_value) { + ConfigFileItem c_key; + ConfigFileItem *item; + + c_key.key = key; + + item = (ConfigFileItem *)hashmap_get(config.map, &c_key); + + if (item == NULL) { + return default_value; + } + + return item->value; +} + +int config_file_get_int(ConfigFile config, char *key, int default_value) { + ConfigFileItem c_key; + ConfigFileItem *item; + + c_key.key = key; + + item = (ConfigFileItem *)hashmap_get(config.map, &c_key); + + if (item == NULL) { + return default_value; + } + + return atoi(item->value); +} + +void config_file_free(ConfigFile config) { hashmap_free(config.map); } \ No newline at end of file diff --git a/src/config_file.h b/src/config_file.h new file mode 100644 index 0000000..7e15448 --- /dev/null +++ b/src/config_file.h @@ -0,0 +1,14 @@ +#include "types.h" + +#ifndef CONFIG_FILE_H +#define CONFIG_FILE_H + +ConfigFile config_file_read(char *path, bool free_path); + +char *config_file_get_str(ConfigFile config, char *key, char *default_value); + +int config_file_get_int(ConfigFile config, char *key, int default_value); + +void config_file_free(ConfigFile config); + +#endif /* CONFIG_FILE_H */ \ No newline at end of file diff --git a/src/types.h b/src/types.h index a5e631f..59f0906 100644 --- a/src/types.h +++ b/src/types.h @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -77,4 +78,13 @@ typedef struct Timer { unsigned int target; } Timer; +typedef struct ConfigFile { + struct hashmap *map; +} ConfigFile; + +typedef struct ConfigFileItem { + char *key; + char *value; +} ConfigFileItem; + #endif /* TYPES_H */ \ No newline at end of file