Files
nt-lite/code/ini_parser.c
T

54 lines
1.7 KiB
C

// ini_parser.c - minimal [section] key=value parser
// Used by the bootloader to read boot.ini and drivers/drivers.ini.
// No heap; caller provides fixed-size output buffers.
#include <string.h>
#include <ini_parser.h>
static inline int IsWs(char c) { return c == ' ' || c == '\t' || c == '\r'; }
static char* TrimLeft(char* s) {
while (*s && IsWs(*s)) s++;
return s;
}
static void TrimRight(char* s) {
char* e = s + strlen(s);
while (e > s && IsWs(*(e-1))) *--e = '\0';
}
// Parse 'buf' (null-terminated) for [section] / key=value.
// Calls cb(section, key, value, userdata) for each key found.
void IniParse(char* buf, IniCallback cb, void* userdata) {
char section[64] = "";
char* line = buf;
while (*line) {
char* end = line;
while (*end && *end != '\n') end++;
char saved = *end;
*end = '\0';
char* p = TrimLeft(line);
if (*p == '[') {
p++;
char* close = strchr(p, ']');
if (close) { *close = '\0'; strncpy(section, p, 63); section[63] = '\0'; }
} else if (*p && *p != ';' && *p != '#') {
char* eq = strchr(p, '=');
if (eq) {
*eq = '\0';
char* key = p; TrimRight(key);
char* val = TrimLeft(eq + 1); TrimRight(val);
cb(section, key, val, userdata);
} else {
// Key with no value (e.g., a driver filename in drivers.ini).
TrimRight(p);
if (*p) cb(section, p, "", userdata);
}
}
*end = saved;
line = end + (*end ? 1 : 0);
}
}