feat: modernize config

- config now is more toml like
- no need to declare all fields, it implements defaults
- no yanderedev code, introspection babbyyy 😎
- desktop and config files parser semi-unification
- misc tweaks all over (mainly allocation failures handling)
This commit is contained in:
2025-06-13 14:05:19 +02:00
parent 3480393a66
commit 5174f0b2bf
13 changed files with 480 additions and 304 deletions

View File

@@ -5,7 +5,20 @@
#include <string.h>
#include "desktop.h"
#include "macros.h"
char* trim_str(char* str) {
while (*str == ' ' || *str == '\t')
str++;
size_t i = strlen(str);
while (i > 0) {
if (str[i - 1] != ' ' && str[i - 1] != '\t' && str[i - 1] != '\n') break;
i--;
}
str[i] = '\0';
return str;
}
int read_desktop(FILE* fd, void* ctx,
struct status (*cb)(void* ctx, char* table, char* key,
@@ -19,34 +32,42 @@ int read_desktop(FILE* fd, void* ctx,
while ((read_size = getline(&buf, &alloc_size, fd)) > 0) {
ret = 0;
if (read_size <= 1) continue;
char* buf_start = trim_str(buf);
size_t indent_size = buf_start - buf;
if (buf[0] == '[' && buf[read_size - 2] == ']') {
if (read_size - indent_size < 1) continue;
if (*buf_start == '#') continue;
if (*buf_start == '[' && buf_start[strlen(buf_start) - 1] == ']') {
if (table_name != NULL) free(table_name);
table_name = realloc(buf, read_size);
table_name[read_size - 1] = '\0'; // newline
buf = NULL;
alloc_size = 0;
buf_start[strlen(buf_start) - 1] = '\0';
table_name = strdup(buf_start + 1);
if (table_name == NULL) {
ret = -1;
break;
}
} else {
// Find '='
size_t eq_idx = 0;
while (buf[eq_idx] != '\0') {
if (buf[eq_idx] == '=') break;
while (buf_start[eq_idx] != '\0') {
if (buf_start[eq_idx] == '=') break;
eq_idx++;
}
// impossible with a min len of 1 (empty line)
if (eq_idx == 0) continue;
// Check its not end
if (buf[eq_idx] != '=') {
if (buf_start[eq_idx] != '=') {
ret = -1;
break;
}
// Key & Value
char* key = buf;
buf[eq_idx] = '\0'; // the equal
char* value = &buf[eq_idx + 1];
buf[read_size - 1] = '\0'; // the newline
char* key = buf_start;
buf_start[eq_idx] = '\0'; // the equal
key = trim_str(key);
char* value = &buf_start[eq_idx + 1];
buf_start[read_size - 1] = '\0'; // the newline
value = trim_str(value);
// Callback
struct status cb_ret = cb(ctx, table_name, key, value);