From b595d83468f6717933737de3c26b9fb8952b05f4 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 16 Dec 2025 11:36:31 +0100 Subject: [PATCH] add value processing to conf.c --- src/io/conf.c | 36 +++++++++++++++++++++++++++++++++++- src/io/conf.h | 14 ++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/io/conf.c b/src/io/conf.c index d47f8b7..7506648 100644 --- a/src/io/conf.c +++ b/src/io/conf.c @@ -5,6 +5,9 @@ * at: www.github.com/thepigeongenerator/mcaselector-lite */ #include "conf.h" +#include +#include +#include #include #include "../types.h" @@ -37,4 +40,35 @@ int conf_getkeyval(const char *restrict buf, const char *const restrict *restric return i; } -int conf_val(int); +int conf_procval(u8 type, const char *val, void *out) +{ + char *end; + switch (type) { + case CONF_STR: *(char **)out = strdup(val); return 0; + + case CONF_I8: *(i8 *)out = strtoimax(val, &end, 0); return (end && !*end); + case CONF_I16: *(i16 *)out = strtoimax(val, &end, 0); return (end && !*end); + case CONF_I32: *(i32 *)out = strtoimax(val, &end, 0); return (end && !*end); + case CONF_I64: *(i64 *)out = strtoimax(val, &end, 0); return (end && !*end); + + case CONF_U8: *(u8 *)out = strtoumax(val, &end, 0); return (end && !*end); + case CONF_U16: *(u16 *)out = strtoumax(val, &end, 0); return (end && !*end); + case CONF_U32: *(u32 *)out = strtoumax(val, &end, 0); return (end && !*end); + case CONF_U64: *(u64 *)out = strtoumax(val, &end, 0); return (end && !*end); + +#if __SIZEOF_FLOAT__ == 4 + case CONF_F32: *(f32 *)out = strtof(val, &end); return (end && !*end); +#else +#error float is not 4 bytes wide +#endif +#if __SIZEOF_DOUBLE__ == 8 + case CONF_F64: *(f64 *)out = strtod(val, &end); return (end && !*end); +#elif __SIZEOF_LONG_DOUBLE__ == 8 + case CONF_F64: *(f64 *)out = strtold(val, &end); return (end && !*end); +#else +#error double or long double are not 8 bytes wide +#endif + default: assert("invalid type"); + } + return 0; +} diff --git a/src/io/conf.h b/src/io/conf.h index 5295ae6..2de909f 100644 --- a/src/io/conf.h +++ b/src/io/conf.h @@ -7,6 +7,20 @@ #include "../types.h" #include "../util/atrb.h" +enum conf_type { + CONF_STR = 0x00, + CONF_I8 = 0x00 | 0x01, + CONF_I16 = 0x00 | 0x02, + CONF_I32 = 0x00 | 0x04, + CONF_I64 = 0x00 | 0x08, + CONF_U8 = 0x10 | 0x01, + CONF_U16 = 0x10 | 0x02, + CONF_U32 = 0x10 | 0x04, + CONF_U64 = 0x10 | 0x08, + CONF_F32 = 0x20 | 0x04, + CONF_F64 = 0x20 | 0x08, +}; + /* Gets the key and value, if present. Writes the pointer for the value to `out`. * Returns the key index, or <0 upon failure. */ int conf_getkeyval(const char *restrict buf, const char *const restrict *restrict keys, int klen,