From 0fe19b28fc942f2f773f53df041c8bdafa97443b Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 10 Jun 2025 16:11:30 +0200 Subject: [PATCH] add conf code start add the start for conf file loading --- src/util/conf.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++ src/util/conf.h | 45 +++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 src/util/conf.c create mode 100644 src/util/conf.h diff --git a/src/util/conf.c b/src/util/conf.c new file mode 100644 index 0000000..7a6d6db --- /dev/null +++ b/src/util/conf.c @@ -0,0 +1,150 @@ +#include "conf.h" + +#include +#include +#include +#include +#include +#include + +#define BUFS 64 + +/* contains the basic structure for a string */ +struct str { + char* dat; // pointer to the string data + unsigned cap; // current string capacity +}; + +/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */ +__attribute_nonnull__((1, 3)) static inline char* conf_getpat_concat( + char const* restrict s1, char const* restrict s2, char const* restrict s3, + size_t s1len, size_t s2len, size_t s3len) { + char *buf, *ptr; + + // allocate enough data for all three to the buffer + ptr = malloc(s1len + s2len + s3len + 1); + if (!ptr) return NULL; + + // copy data to the buffer + buf = strcpy(ptr, s1); // copy s1 data to the buffer + if (s2len) ptr = memcpy(ptr + s1len, s2, s2len); // increment ptr by s1len, and copy s2 data to the buffer (excluding null-terminator) + (void)strcpy(ptr + s2len, s3); // add s2len to the pointer and copy s3 as a string, thus including null-terminator + + // return the buffer + return buf; +} + +/* appends str to the config directory string we acquire from environment variables. */ +char* conf_getpat(char const* restrict str) { + char* buf = NULL; + size_t len; + size_t str_len = strlen(str); +#if defined(__linux__) + buf = getenv("XDG_CONFIG_HOME"); + if (!buf) { + buf = getenv("HOME"); + if (!buf) return NULL; + len = strlen(buf); + return conf_getpat_concat(buf, "/.config", str, len, 8, str_len); + } + return conf_getpat_concat(buf, NULL, str, strlen(buf), 0, str_len); +#elif defined(__APPLE__) + buf = getenv("HOME"); + if (!buf) return NULL; + len = strlen(buf); + return conf_getpat_concat(buf, "/Library/Application Support", str, len, 28, str_len); +#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) + buf = getenv("APPDATA"); + if (!buf) { + cfgpat = getenv("USERPROFILE"); + if (!buf) return NULL; + len = strlen(buf); + return conf_getpat_concat(buf, "\\AppData\\Roaming", str, buf, 16, str_len); + } + return conf_getpat_concat(buf, NULL, str, strlen(buf), 0, str_len); +#else +#error platform unsupported! +#endif +} + +/* appends src onto dest.dat, if test.dat hasn't been allocated, or does not have enough space. + The size is the nearest 2ˣ rounded-up value of src_len. + returns 0 upon success, 1 upon failure. */ +__attribute_nonnull__((1, 3)) static inline int str_put(struct str* restrict dest, size_t dest_len, char const* restrict src, size_t src_len) { + // check if (re)allocation needs to occur + if (dest->cap <= src_len) { // check for equality as well due to `\0` + dest->cap = src_len; // using src_len as-is, since it is string length, thus already one short. + for (unsigned i = 1; i < (sizeof(size_t) * 8); i <<= 1) // then loop through each 2ˣ bit in size_t + dest->cap |= dest->cap >> i; // OR together the shifted result (shifted 1, 2, 4, 8, 16 on 32 bit systems) + dest->cap++; // round to the most significant bit (0111 -> 1000) + + // (re)allocate the array + if (!dest->dat) { + dest->dat = malloc(dest->cap); // allocate memory for this capacity + if (!dest->dat) return 1; // return 1 upon failure + } else { + void* ptr = realloc(dest->dat, dest->cap); // reallocate to the new capacity + if (!ptr) { + free(dest->dat); // free up resources by the old (still valid) pointer, and return failure + return 1; + } + dest->dat = ptr; + } + } + + + // copy the missing data to the end of the destination + memcpy(dest->dat + dest_len, src, src_len - dest_len); + dest->dat[src_len] = '\0'; // null-terminate the destination + return 0; +} + +/* loads the config file */ +int conf_loadfile(char const* pat, struct conf_entry* dat, size_t cnt) { + FILE* f = fopen(pat, "r"); + if (!f) return 1; + + size_t ln = 0; + char buf[BUFS]; + while (fgets(buf, BUFS, f)) { + bool isval = 0; // whether we're reading the value at the moment (used as index) + bool nodat = false; // whether there is no more data after this point + bool brk = false; // whether we broke out of the loop + unsigned vallen = 0; + unsigned keylen = 0; + + // loop through each character + for (unsigned i = 0; i < BUFS; i++) { + // + switch (buf[i]) { + case '\n': + case '\0': + brk = true; + break; // break out of the switch + case '#': + nodat = true; // specify that there is no more data from this point onwards + continue; // continue in the loop + case '=': + if (!isval) { + isval = 1; + continue; + } else break; + } + if (brk) break; + if (nodat) continue; + + if (isval) vallen++; + else keylen++; + + // TODO: key/value are initialized NULL + // TODO: key/value caches the pointer to the data in the buffer + } + // TODO: key/value are checked for NULL, if either are NULL; something went wrong. + // TODO: append the key/value to the str_put thingie + // TODO: check what type we're converting the data to and go and do that. + // FIX: rewrite the whole thing to make testing easier. i.e. don't handle file reading AND conf parsing in the same funciton + } + + fclose(f); + return 0; +} diff --git a/src/util/conf.h b/src/util/conf.h new file mode 100644 index 0000000..b2df073 --- /dev/null +++ b/src/util/conf.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +/* defines the primitive types available in the config file */ +enum config_primitive { + CONF_STR = 0, // expects: `char**`, will output malloc'd data !!must be freed!! + CONF_I8 = 1, // expects: `int8_t*`, will point to a location in memory where an i8 is stored. + CONF_I16 = 2, // expects: `int16_t*`, will point to a location in memory where an i16 is stored. + CONF_I32 = 4, // expects: `int32_t*`, will point to a location in memory where an i32 is stored. + CONF_I64 = 8, // expects: `int64_t*`, will point to a location in memory where an i64 is stored. + CONF_U8 = CONF_I8 | 0x80, // expects: `uint8_t*`, will point to a location in memory where an u8 is stored. + CONF_U16 = CONF_I16 | 0x80, // expects: `uint16_t*`, will point to a location in memory where an u16 is stored. + CONF_U32 = CONF_I32 | 0x80, // expects: `uint32_t*`, will point to a location in memory where an u32 is stored. + CONF_U64 = CONF_I64 | 0x80, // expects: `uint64_t*`, will point to a location in memory where an u64 is stored. + CONF_F32 = CONF_I32 | 0x40, // expects: `float*`, will point to a location in memory where an f32 is stored. + CONF_F64 = CONF_I64 | 0x40, // expects: `double*`, will point to a location in memory where an f64 is stored. + CONF_FSTR = 0x40, // expects: `struct conf_fstr*`, which contains the data for a fixed-width string +}; + +/* for outputting a fixed string as this config field */ +struct conf_fstr { + size_t len; // length in BYTES of the output data + char* datl; // output data location +}; + +/* defines the structure of a config file entry */ +struct conf_entry { + char const* key; // the key of this entry + void* out; // the pointer to which the data is written value is read if the given option is incorrect or missing + uint8_t type; // the primitive type which we are querying for +}; + +/* acquires the config file path, appending str to the end (you need to handle path separators yourself) + * expecting str to be null-terminated + * - linux: reads $XDG_CONFIG_HOME, if empty $HOME/.config is used, if $HOME is empty NULL is returned. + * - windows: reads %APPDATA%, if empty %USERPROFILE%\AppData\Roaming is used, if both are empty NULL is returned. + * - osx: reads $HOME, uses $HOME/Library/Application Support, if $HOME is empty NULL is returned. + * !! A malloc'd null-terminated string is returned !! */ +__attribute_malloc__ __attribute_nonnull__((1)) char* conf_getpat(char const*); + +/* loads the config file with cnt of dat options */ +int conf_loadfile(char const* pat, struct conf_entry* dat, size_t cnt);