chore: reduce debt for config_file

This commit is contained in:
2025-11-10 15:56:04 +01:00
parent 54224779b6
commit 4405203459
6 changed files with 23 additions and 20 deletions
+14 -12
View File
@@ -30,9 +30,9 @@ static uint64_t item_hash(const void *item, uint64_t seed0, uint64_t seed1) {
return hashmap_sip(c_item->key, strnlen(c_item->key, STR_LEN), seed0, seed1); return hashmap_sip(c_item->key, strnlen(c_item->key, STR_LEN), seed0, seed1);
} }
static void parse_config_file_line(ConfigFile *config, char *line) { static void parse_config_file_line(const ConfigFile *config, char *line) {
unsigned int size; unsigned int size;
char *equal_pos; const char *equal_pos;
unsigned int key_size; unsigned int key_size;
unsigned int value_size; unsigned int value_size;
ConfigFileItem item; ConfigFileItem item;
@@ -67,6 +67,7 @@ static void parse_config_file_line(ConfigFile *config, char *line) {
void config_file_read(ConfigFile *config, char *path) { void config_file_read(ConfigFile *config, char *path) {
File file; File file;
char *line; char *line;
char *rest;
config->map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash, config->map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash,
item_compare, NULL, NULL); item_compare, NULL, NULL);
@@ -77,23 +78,24 @@ void config_file_read(ConfigFile *config, char *path) {
return; return;
} }
line = strtok(file.content, "\n"); line = strtok_r(file.content, "\n", &rest);
while (line != NULL) { while (line != NULL) {
parse_config_file_line(config, line); parse_config_file_line(config, line);
line = strtok(NULL, "\n"); line = strtok_r(rest, "\n", &rest);
} }
file_free(&file); file_free(&file);
} }
char *config_file_get_str(ConfigFile *config, char *key, char *default_value) { const char *config_file_get_str(const ConfigFile *config, const char *key,
const char *default_value) {
ConfigFileItem c_key; ConfigFileItem c_key;
ConfigFileItem *item; const ConfigFileItem *item;
strlcpy(c_key.key, key, STR_LEN); strlcpy(c_key.key, key, STR_LEN);
item = (ConfigFileItem *)hashmap_get(config->map, &c_key); item = (const ConfigFileItem *)hashmap_get(config->map, &c_key);
if (item == NULL || strnlen(item->value, STR_LEN) == 0) { if (item == NULL || strnlen(item->value, STR_LEN) == 0) {
return default_value; return default_value;
@@ -102,20 +104,20 @@ char *config_file_get_str(ConfigFile *config, char *key, char *default_value) {
return item->value; return item->value;
} }
unsigned int config_file_get_int(ConfigFile *config, char *key, unsigned int config_file_get_int(const ConfigFile *config, const char *key,
unsigned int default_value) { unsigned int default_value) {
ConfigFileItem c_key; ConfigFileItem c_key;
ConfigFileItem *item; const ConfigFileItem *item;
strlcpy(c_key.key, key, STR_LEN); strlcpy(c_key.key, key, STR_LEN);
item = (ConfigFileItem *)hashmap_get(config->map, &c_key); item = (const ConfigFileItem *)hashmap_get(config->map, &c_key);
if (item == NULL || strnlen(item->value, STR_LEN) == 0) { if (item == NULL || strnlen(item->value, STR_LEN) == 0) {
return default_value; return default_value;
} }
if (!string_is_number(item->value)) { if (!string_is_number((char *)item->value)) {
log_warn("Invalid number for %s: '%s'", item->key, item->value); log_warn("Invalid number for %s: '%s'", item->key, item->value);
return default_value; return default_value;
} }
@@ -123,4 +125,4 @@ unsigned int config_file_get_int(ConfigFile *config, char *key,
return (unsigned int)atoi(item->value); return (unsigned int)atoi(item->value);
} }
void config_file_free(ConfigFile *config) { hashmap_free(config->map); } void config_file_free(const ConfigFile *config) { hashmap_free(config->map); }
+4 -3
View File
@@ -5,11 +5,12 @@
void config_file_read(ConfigFile *config, char *path); void config_file_read(ConfigFile *config, char *path);
char *config_file_get_str(ConfigFile *config, char *key, char *default_value); const char *config_file_get_str(const ConfigFile *config, const char *key,
const char *default_value);
unsigned int config_file_get_int(ConfigFile *config, char *key, unsigned int config_file_get_int(const ConfigFile *config, const char *key,
unsigned int default_value); unsigned int default_value);
void config_file_free(ConfigFile *config); void config_file_free(const ConfigFile *config);
#endif /* CONFIG_FILE_H */ #endif /* CONFIG_FILE_H */
+1 -1
View File
@@ -7,7 +7,7 @@
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
void midi_open(MidiDevice *device, char *name) { void midi_open(MidiDevice *device, const char *name) {
strlcpy(device->name, name, STR_LEN); strlcpy(device->name, name, STR_LEN);
device->input = NULL; device->input = NULL;
device->output = NULL; device->output = NULL;
+1 -1
View File
@@ -3,7 +3,7 @@
#ifndef MIDI_H #ifndef MIDI_H
#define MIDI_H #define MIDI_H
void midi_open(MidiDevice *device, char *name); void midi_open(MidiDevice *device, const char *name);
void midi_write(MidiDevice *device, unsigned char code, unsigned char value); void midi_write(MidiDevice *device, unsigned char code, unsigned char value);
bool midi_background_listen(MidiDevice *device, SharedContext *context, bool midi_background_listen(MidiDevice *device, SharedContext *context,
void (*event_callback)(unsigned char code, void (*event_callback)(unsigned char code,
+2 -2
View File
@@ -60,7 +60,7 @@ static bool parse_fragment_shader_file(Project *project, unsigned int i) {
return true; return true;
} }
static bool read_fragment_shader_file(Project *project, char *frag_prefix, static bool read_fragment_shader_file(Project *project, const char *frag_prefix,
unsigned int i) { unsigned int i) {
char file_path[STR_LEN]; char file_path[STR_LEN];
@@ -78,7 +78,7 @@ static bool read_fragment_shader_file(Project *project, char *frag_prefix,
void project_init(Project *project, char *project_path, char *config_file) { void project_init(Project *project, char *project_path, char *config_file) {
char config_path[STR_LEN]; char config_path[STR_LEN];
char *frag_prefix; const char *frag_prefix;
strlcpy(project->path, project_path, STR_LEN); strlcpy(project->path, project_path, STR_LEN);
+1 -1
View File
@@ -232,7 +232,7 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
unsigned int index1; unsigned int index1;
unsigned int index2; unsigned int index2;
char name[STR_LEN]; char name[STR_LEN];
char *prefix; const char *prefix;
program->programs[i] = glCreateProgram(); program->programs[i] = glCreateProgram();