101 lines
2.2 KiB
C
101 lines
2.2 KiB
C
// dvxPrefs.c — INI-based preferences system
|
|
//
|
|
// Thin wrapper around rxi/ini that adds typed accessors with defaults.
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#include "dvxPrefs.h"
|
|
#include "thirdparty/ini/src/ini.h"
|
|
|
|
|
|
static ini_t *sIni = NULL;
|
|
|
|
|
|
// ============================================================
|
|
// prefsFree
|
|
// ============================================================
|
|
|
|
void prefsFree(void) {
|
|
if (sIni) {
|
|
ini_free(sIni);
|
|
sIni = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
// ============================================================
|
|
// prefsGetBool
|
|
// ============================================================
|
|
|
|
bool prefsGetBool(const char *section, const char *key, bool defaultVal) {
|
|
const char *val = prefsGetString(section, key, NULL);
|
|
|
|
if (!val) {
|
|
return defaultVal;
|
|
}
|
|
|
|
// Case-insensitive first character check covers true/yes/1 and false/no/0
|
|
char c = (char)tolower((unsigned char)val[0]);
|
|
|
|
if (c == 't' || c == 'y' || c == '1') {
|
|
return true;
|
|
}
|
|
|
|
if (c == 'f' || c == 'n' || c == '0') {
|
|
return false;
|
|
}
|
|
|
|
return defaultVal;
|
|
}
|
|
|
|
|
|
// ============================================================
|
|
// prefsGetInt
|
|
// ============================================================
|
|
|
|
int32_t prefsGetInt(const char *section, const char *key, int32_t defaultVal) {
|
|
const char *val = prefsGetString(section, key, NULL);
|
|
|
|
if (!val) {
|
|
return defaultVal;
|
|
}
|
|
|
|
char *end = NULL;
|
|
long n = strtol(val, &end, 10);
|
|
|
|
if (end == val) {
|
|
return defaultVal;
|
|
}
|
|
|
|
return (int32_t)n;
|
|
}
|
|
|
|
|
|
// ============================================================
|
|
// prefsGetString
|
|
// ============================================================
|
|
|
|
const char *prefsGetString(const char *section, const char *key, const char *defaultVal) {
|
|
if (!sIni) {
|
|
return defaultVal;
|
|
}
|
|
|
|
const char *val = ini_get(sIni, section, key);
|
|
|
|
return val ? val : defaultVal;
|
|
}
|
|
|
|
|
|
// ============================================================
|
|
// prefsLoad
|
|
// ============================================================
|
|
|
|
bool prefsLoad(const char *filename) {
|
|
prefsFree();
|
|
|
|
sIni = ini_load(filename);
|
|
|
|
return sIni != NULL;
|
|
}
|