aboutsummaryrefslogtreecommitdiff
path: root/lib/libcfg/rdcfg.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-10-10 11:28:46 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-10-10 16:45:11 +0200
commit24c90b7700e18d0668799f8f343bc854a42dea20 (patch)
tree224d9b5e81a46e27f354c6975fc3fa4cd1f1fb79 /lib/libcfg/rdcfg.c
parent7b647eefef00afb6104e84fae2a2fbf0481d4364 (diff)
Configuration parser cleanup
- Do a getline() & process in rdline instead of doing a read per character and feeding it through a state machine. - Move splitkv to rdcfg.c, the only place where it is used Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib/libcfg/rdcfg.c')
-rw-r--r--lib/libcfg/rdcfg.c32
1 files changed, 30 insertions, 2 deletions
diff --git a/lib/libcfg/rdcfg.c b/lib/libcfg/rdcfg.c
index 71a994f..ccbcf7b 100644
--- a/lib/libcfg/rdcfg.c
+++ b/lib/libcfg/rdcfg.c
@@ -19,6 +19,7 @@
#include <string.h>
#include <stdio.h>
+#include <ctype.h>
static const cfg_param_t *find_param(rdline_t *rd, const char *name,
const cfg_param_t *params, size_t count)
@@ -35,6 +36,33 @@ static const cfg_param_t *find_param(rdline_t *rd, const char *name,
return NULL;
}
+static int splitkv(rdline_t *rd, char **k, char **v)
+{
+ char *key = rd->line, *value = rd->line;
+
+ while (*value != ' ' && *value != '\0') {
+ if (!isalpha(*value)) {
+ fprintf(stderr,
+ "%s: %zu: unexpected '%c' in keyword\n",
+ rd->filename, rd->lineno, *value);
+ return -1;
+ }
+ ++value;
+ }
+
+ if (*value != ' ') {
+ fprintf(stderr, "%s: %zu: expected argument after '%s'\n",
+ rd->filename, rd->lineno, key);
+ return -1;
+ }
+
+ *(value++) = '\0';
+
+ *k = key;
+ *v = value;
+ return 0;
+}
+
int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
int flags)
{
@@ -61,9 +89,9 @@ int rdcfg(void *cfgobj, rdline_t *rd, const cfg_param_t *params, size_t count,
}
while ((ret = rdline(rd)) == 0) {
- if (strcmp(rd->buffer, "}") == 0)
+ if (strcmp(rd->line, "}") == 0)
break;
- if (p->handle(cfgobj, rd->buffer, rd, flags))
+ if (p->handle(cfgobj, rd->line, rd, flags))
return -1;
}