56 lines
1.6 KiB
C
56 lines
1.6 KiB
C
// Minimal INI parser. This file accepts the small configuration dialect used by boot.ini and drivers.ini.
|
|
|
|
#include <string.h>
|
|
#include <common/ini_parser.h>
|
|
|
|
// Checks whether ws.
|
|
static inline int IsWs(char c) { return c == ' ' || c == '\t' || c == '\r'; }
|
|
|
|
// Implements the trim Left operation.
|
|
static char* TrimLeft(char* s) {
|
|
while (*s && IsWs(*s)) s++;
|
|
return s;
|
|
}
|
|
|
|
// Implements the trim Right operation.
|
|
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);
|
|
}
|
|
}
|