init
This commit is contained in:
commit
1e4bc22cf5
9
Makefile
Normal file
9
Makefile
Normal file
@ -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
|
44
README
Normal file
44
README
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
70
include/libevconf.h
Normal file
70
include/libevconf.h
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#ifndef EVCONF_H_
|
||||||
|
#define EVCONF_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user