mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 06:15:47 +01:00
implement conf
This commit is contained in:
116
src/util/conf.c
116
src/util/conf.c
@@ -1,24 +1,25 @@
|
||||
#include "conf.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "../error.h"
|
||||
#include "atrb.h"
|
||||
|
||||
int conf_procbuf(char const* restrict buf, size_t buflen, struct conf_entry const* opts, size_t optc) {
|
||||
enum {
|
||||
ERR_ABSENT = 1,
|
||||
ERR_NOMATCH = 2,
|
||||
ERR_INVALID_TYPE = 3,
|
||||
};
|
||||
int conf_procbuf(char const* restrict buf, char* restrict kout, char* restrict vout, size_t len) {
|
||||
// length data storage
|
||||
unsigned idx = 0; // index to the data below
|
||||
|
||||
unsigned idx = 0; // index to the data below
|
||||
char const* dat[2] = {0}; // stores key-value data
|
||||
unsigned len[2] = {0}; // stores the length of the data
|
||||
for (size_t i = 0; i < buflen; i++) {
|
||||
// data traversal
|
||||
char* pos = kout; // will point to the next point in the buffer, where we'll write data
|
||||
|
||||
// acquire data
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
// handling of termination tokens
|
||||
bool brk = false; // whether we broke out of the loop, and are done reading
|
||||
switch (buf[i]) {
|
||||
@@ -33,41 +34,90 @@ int conf_procbuf(char const* restrict buf, size_t buflen, struct conf_entry cons
|
||||
|
||||
// everything after `=` is interpreted as a value
|
||||
if (buf[i] == '=' && !idx) {
|
||||
*pos = '\0'; // terminate string
|
||||
pos = vout; // move pointer to start of value data
|
||||
idx++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// set the value pointer to the current buffer char
|
||||
dat[idx] = dat[idx] ? dat[idx] : &buf[i];
|
||||
len[idx]++;
|
||||
*pos = buf[i]; // copy over the buffer's data
|
||||
pos++; // increment the position pointer
|
||||
}
|
||||
if (!(dat[0] && dat[1])) return ERR_ABSENT;
|
||||
if (pos == kout) return 0; // line was ignored if pos didn't move
|
||||
|
||||
// null-terminate what we've got now (yes, there should be enough space for this since \0 isn't stored)
|
||||
*pos = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct conf_entry const* conf_matchopt(struct conf_entry const* opts, size_t optc, char const* restrict key) {
|
||||
// find a match for the current key
|
||||
size_t i = 0;
|
||||
for (; i < optc; i++) {
|
||||
if (strcmp(opts[i].key, dat[0]) == 0) break;
|
||||
if (strcmp(opts[i].key, key) == 0)
|
||||
return opts + i;
|
||||
}
|
||||
if (i >= optc) return ERR_NOMATCH; // no match was found
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// TODO: keys and vals are not stored as null-terminated strings, thus we should do that
|
||||
switch (opts[i].type) {
|
||||
case CONF_I8: break;
|
||||
case CONF_I16: break;
|
||||
case CONF_I32: break;
|
||||
case CONF_I64: break;
|
||||
case CONF_U8: break;
|
||||
case CONF_U16: break;
|
||||
case CONF_U32: break;
|
||||
case CONF_U64: break;
|
||||
case CONF_F32: break;
|
||||
case CONF_F64: break;
|
||||
case CONF_STR: break;
|
||||
case CONF_FSTR: break;
|
||||
default: return ERR_INVALID_TYPE;
|
||||
int conf_procval(struct conf_entry const* opt, char const* restrict val) {
|
||||
// parse the data
|
||||
errno = 0;
|
||||
char* end;
|
||||
int8_t dat[sizeof(long)];
|
||||
|
||||
switch (opt->type) {
|
||||
// signed integer data parsing
|
||||
case CONF_I8:
|
||||
case CONF_I16:
|
||||
case CONF_I32:
|
||||
case CONF_I64:
|
||||
*(long*)dat = strtol(val, &end, 10);
|
||||
break;
|
||||
// unsigned integer data parsing
|
||||
case CONF_U8:
|
||||
case CONF_U16:
|
||||
case CONF_U32:
|
||||
case CONF_U64:
|
||||
*(ulong*)dat = strtoul(val, &end, 10);
|
||||
break;
|
||||
|
||||
// floating-point data parsing
|
||||
case CONF_F32: *(float*)dat = strtof(val, &end); break;
|
||||
case CONF_F64: *(double*)dat = strtod(val, &end); break;
|
||||
|
||||
// string data parsing
|
||||
case CONF_STR:
|
||||
if (*(char**)opt->out) {
|
||||
free(*(char**)opt->out); // if the same key is given multiple times, free the memory so we don't leak.
|
||||
warn("encountered a dynamic string multiple times, this is sub-optimal.");
|
||||
}
|
||||
*(char**)opt->out = strdup(val);
|
||||
return 0;
|
||||
case CONF_FSTR: {
|
||||
struct conf_fstr* s = opt->out;
|
||||
strncpy(s->out, val, s->len);
|
||||
s->out[s->len - 1] = '\0'; // ensure the string is null-terminated
|
||||
return 0;
|
||||
}
|
||||
default: return CONF_EINVALIDTYPE; // return error code indicating function not implemented
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (errno || end == val || *end != '\0') {
|
||||
error("failed to parse '%s' as a numeric value", val);
|
||||
return CONF_EPARSE;
|
||||
}
|
||||
|
||||
switch (opt->type) {
|
||||
case CONF_U8:
|
||||
case CONF_I8: *(int8_t*)opt->out = *(int8_t*)dat; return 0;
|
||||
case CONF_U16:
|
||||
case CONF_I16: *(int16_t*)opt->out = *(int16_t*)dat; return 0;
|
||||
case CONF_U32:
|
||||
case CONF_I32: *(int32_t*)opt->out = *(int32_t*)dat; return 0;
|
||||
case CONF_U64:
|
||||
case CONF_I64: *(int64_t*)opt->out = *(int64_t*)dat; return 0;
|
||||
default: abort(); // abort; this shouldn't be possible, so I blame the programmer
|
||||
}
|
||||
}
|
||||
|
||||
/* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
|
||||
|
||||
@@ -6,8 +6,17 @@
|
||||
|
||||
#include "atrb.h"
|
||||
|
||||
/* error codes */
|
||||
enum conf_err {
|
||||
CONF_ENODAT = -1, // no data was found
|
||||
CONF_ESYNTAX = 1, // couldn't extract clear key/val data
|
||||
CONF_ENOMATCH = 2, // couldn't find a match for the inputted key
|
||||
CONF_EINVALIDTYPE = 3, // the type inputted in conf_entry was invalid
|
||||
CONF_EPARSE = 4, // something went wrong whilst parsing
|
||||
};
|
||||
|
||||
/* defines the primitive types available in the config file */
|
||||
enum config_primitive {
|
||||
enum conf_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.
|
||||
@@ -25,7 +34,7 @@ enum config_primitive {
|
||||
/* for outputting a fixed string as this config field */
|
||||
struct conf_fstr {
|
||||
size_t len; // length in BYTES of the output data
|
||||
char* datl; // where we will output the data
|
||||
char* out; // where we will output the data
|
||||
};
|
||||
|
||||
/* defines the structure of a config file entry */
|
||||
@@ -35,9 +44,20 @@ struct conf_entry {
|
||||
uint8_t type; // the primitive type which we are querying for
|
||||
};
|
||||
|
||||
/* processes an incoming buffer for conf_entry
|
||||
* returns 0 upon success, 1 upon failure*/
|
||||
int conf_procbuf(char const*, size_t, struct conf_entry const*, size_t);
|
||||
/* processes an incoming buffer.
|
||||
* `buf`, `kout` and `vout` mustn't overlap, and must be (at least) `len` bytes long!
|
||||
* `kout` and `vout` will contain a null-terminated string if the function returned successfully.
|
||||
* returns `0` on success, `<0` when no data was found. `>0` when data was invalid but something went wrong.
|
||||
* see `CONF_E*` or `enum conf_err` */
|
||||
int conf_procbuf(char const* restrict buf, char* restrict kout, char* restrict vout, size_t len);
|
||||
|
||||
/* matches the key with one of the options and returns the pointer. Returns NULL if none could be found. */
|
||||
struct conf_entry const* conf_matchopt(struct conf_entry const* opts, size_t optc, char const* restrict key);
|
||||
|
||||
/* processes the value belonging to the key and outputs the result to opts.
|
||||
* - `val` points to a null-terminated string which contains the key and value.
|
||||
* returns `0` upon success, non-zero upon failure. For information about specific error codes, see `enum conf_err` */
|
||||
int conf_procval(struct conf_entry const* opts, char const* restrict val);
|
||||
|
||||
/* acquires the config file path, appending str to the end (you need to handle path separators yourself)
|
||||
* expecting str to be null-terminated
|
||||
|
||||
Reference in New Issue
Block a user