config file source
This commit is contained in:
+2
-2
@@ -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
|
||||
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
|
||||
@@ -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])
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include <hashmap.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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); }
|
||||
@@ -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 */
|
||||
+10
@@ -1,5 +1,6 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/gl.h>
|
||||
#include <hashmap.h>
|
||||
#include <linmath.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/time.h>
|
||||
@@ -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 */
|
||||
Reference in New Issue
Block a user