conf-parser: introduce config section parser wrapper

It will be used later.
This commit is contained in:
Yu Watanabe
2024-09-06 10:33:05 +09:00
parent 5a11437e2e
commit 152b8a4e71
3 changed files with 73 additions and 35 deletions
+31
View File
@@ -793,6 +793,37 @@ bool stats_by_path_equal(Hashmap *a, Hashmap *b) {
return true;
}
int config_section_parse(
const ConfigSectionParser *parsers,
size_t n_parsers,
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *userdata) {
assert(parsers);
assert(n_parsers > 0);
assert(ltype >= 0);
assert((size_t) ltype < n_parsers);
assert(userdata);
const ConfigSectionParser *e = parsers + ltype;
assert(e->parser);
/* This is used when a object is dynamically allocated per [SECTION] in a config parser, e.g.
* [Address] for systemd.network. Takes the allocated object as 'userdata', then it is passed to
* config parsers in the table. The 'data' field points to an element of the passed object, where
* its offset is given by the table. */
return e->parser(unit, filename, line, section, section_line, lvalue, e->ltype, rvalue,
(uint8_t*) userdata + e->offset, userdata);
}
void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
siphash24_compress_string(c->filename, state);
siphash24_compress_typesafe(c->line, state);
+19
View File
@@ -147,6 +147,25 @@ int config_get_stats_by_path(
int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st);
bool stats_by_path_equal(Hashmap *a, Hashmap *b);
typedef struct ConfigSectionParser {
ConfigParserCallback parser;
int ltype;
size_t offset;
} ConfigSectionParser;
int config_section_parse(
const ConfigSectionParser *parsers,
size_t n_parsers,
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *userdata);
typedef struct ConfigSection {
unsigned line;
bool invalid;