mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-17 09:25:44 +01:00
remove loading file logic and instead focus on parsing the incomming text
This commit is contained in:
106
src/util/conf.c
106
src/util/conf.c
@@ -7,61 +7,65 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
#define BUFS 64
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
/* contains the basic structure for a string */
|
bool isval = 0; // whether we're reading the value at the moment (used as index)
|
||||||
struct str {
|
char const* dat[2] = {0}; // stores key-value data
|
||||||
char* dat; // pointer to the string data
|
unsigned len[2] = {0}; // stores the length of the data
|
||||||
unsigned cap; // current string capacity
|
for (size_t i = 0; i < buflen; i++) {
|
||||||
};
|
// handling of termination tokens
|
||||||
|
bool brk = false; // whether we broke out of the loop, and are done reading
|
||||||
/* loads the config file */
|
switch (buf[i]) {
|
||||||
int conf_loadfile(char const* pat, struct conf_entry* dat, size_t cnt) {
|
case '\n':
|
||||||
FILE* f = fopen(pat, "r");
|
case '\r':
|
||||||
if (!f) return 1;
|
case '\0':
|
||||||
|
case '#':
|
||||||
size_t ln = 0;
|
brk = true;
|
||||||
char buf[BUFS];
|
break;
|
||||||
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.
|
if (brk) break;
|
||||||
// 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.
|
// everything after `=` is interpreted as a value
|
||||||
// FIX: rewrite the whole thing to make testing easier. i.e. don't handle file reading AND conf parsing in the same funciton
|
if (buf[i] == '=' && !isval) {
|
||||||
|
isval++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the value pointer to the current buffer char
|
||||||
|
dat[isval] = dat[isval] ? dat[isval] : &buf[i];
|
||||||
|
len[isval]++;
|
||||||
|
}
|
||||||
|
if (!(dat[0] && dat[1])) return ERR_ABSENT;
|
||||||
|
|
||||||
|
// 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 (i >= optc) return ERR_NOMATCH; // no match was found
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ struct conf_entry {
|
|||||||
uint8_t type; // the primitive type which we are querying for
|
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);
|
||||||
|
|
||||||
/* acquires the config file path, appending str to the end (you need to handle path separators yourself)
|
/* acquires the config file path, appending str to the end (you need to handle path separators yourself)
|
||||||
* expecting str to be null-terminated
|
* expecting str to be null-terminated
|
||||||
* - linux: reads $XDG_CONFIG_HOME, if empty $HOME/.config is used, if $HOME is empty NULL is returned.
|
* - linux: reads $XDG_CONFIG_HOME, if empty $HOME/.config is used, if $HOME is empty NULL is returned.
|
||||||
@@ -40,6 +44,3 @@ struct conf_entry {
|
|||||||
* - osx: reads $HOME, uses $HOME/Library/Application Support, if $HOME is 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 !! */
|
* !! A malloc'd null-terminated string is returned !! */
|
||||||
__attribute_malloc__ __attribute_nonnull__((1)) char* conf_getpat(char const*);
|
__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);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user