config file source

This commit is contained in:
2025-09-18 18:33:45 +02:00
parent 0b9c54adad
commit ebb8c4db46
5 changed files with 100 additions and 2 deletions
+73
View File
@@ -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); }
+14
View File
@@ -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
View File
@@ -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 */