commit 1e4bc22cf53a4bdb6677ddb1c84cabc358cf28cd Author: Liam Waldron Date: Wed Mar 19 20:27:58 2025 -0400 init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4a51cc3 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +# +# Makefile +# + +all: + @echo "Run 'make install' to install the library" + +install: ./include/libevconf.h + install ./include/libevconf.h -t $(PREFIX)/include -m 644 diff --git a/README b/README new file mode 100644 index 0000000..8c45e66 --- /dev/null +++ b/README @@ -0,0 +1,44 @@ ++ libevconf + +The Everest configuration file parser + ++ Installation + +Libevconf is provided as a header-only library. + +To install, clone this repository: + + $ git clone https://git.everestlinux.org/EverestLinux/libevconf + +Enter the directory and edit config.mk: + + $ cd libevconf + $ $EDITOR config.mk + +Install the library: + + $ make install # note that you may need root access, depending on the value of PREFIX + ++ Documentation + +Online documentation can be found at https://everestlinux.org/docs/libevconf +Offline documentation can be accessed with the following command: + + $ man 3 libevconf + ++ Copyright + +(C) 2025 Everest Developers +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation, either version 3 of the License, or (at your option) +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU General Public License for more details. +You should have received a copy of the GNU General Public License along with +this program. +If not, see . + diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..6fa6264 --- /dev/null +++ b/config.mk @@ -0,0 +1,5 @@ +# +# config.mk +# + +PREFIX = diff --git a/include/libevconf.h b/include/libevconf.h new file mode 100644 index 0000000..285158b --- /dev/null +++ b/include/libevconf.h @@ -0,0 +1,70 @@ +#ifndef EVCONF_H_ +#define EVCONF_H_ + +#include +#include +#include + +#define MAX_LINE 256 +#define MAX_ENTRIES 100 + +typedef struct { + char option[64]; + char value[256]; +} ev_config_entry; + +typedef struct { + ev_config_entry entries[MAX_ENTRIES]; + int count; +} ev_config; + +static inline +int evconf_parse_config(const char *filename, ev_config *config) +{ + FILE *config = fopen(filename, "r"); + if (! config) { + return 1; + } + + char line[MAX_LINE]; + while (fgets(line, sizeof(line), config)) { + if (line[0] == "#" || line[0] == '\n') { + continue; + } + + char *equal_sign = strchr(line, '='); + if (! equal_sign) { + continue; + } + + *equal_sign = '\0'; + char *key = line; + char *value = equal_sign + 1; + + key[strcspn(key, "\r\n")] = 0; + value[strcspn(value, "\r\n")] = 0; + + if (config -> count < MAX_ENTRIES) { + strncpy(config -> entries[config -> count].key, key, sizeof(config -> entries[config -> count].key) - 1); + strncpy(config -> entries[config -> count].value, value, sizeof(config -> entries[config -> entries].value) - 1); + config -> count++; + } + } + + fclose(config); +} + +static inline const +char *evconf_get_config_value(ev_config *config, const char *key) +{ + for (int index = 0; index < config -> count; index++) { + if (strcmp(config -> entries[index].key, key) == 0) { + return config -> entries[index].value; + } + } + + return NULL; +} + +#endif +