network: rename NetworkConfigSection -> ConfigSection
And move it and relevant functions to conf-parser.[ch].
This commit is contained in:
@@ -573,6 +573,50 @@ int config_parse_many(
|
||||
return config_parse_many_files(conf_files, files, sections, lookup, table, flags, userdata, ret_stats_by_path);
|
||||
}
|
||||
|
||||
static void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
|
||||
siphash24_compress_string(c->filename, state);
|
||||
siphash24_compress(&c->line, sizeof(c->line), state);
|
||||
}
|
||||
|
||||
static int config_section_compare_func(const ConfigSection *x, const ConfigSection *y) {
|
||||
int r;
|
||||
|
||||
r = strcmp(x->filename, y->filename);
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
return CMP(x->line, y->line);
|
||||
}
|
||||
|
||||
DEFINE_HASH_OPS(config_section_hash_ops, ConfigSection, config_section_hash_func, config_section_compare_func);
|
||||
|
||||
int config_section_new(const char *filename, unsigned line, ConfigSection **s) {
|
||||
ConfigSection *cs;
|
||||
|
||||
cs = malloc0(offsetof(ConfigSection, filename) + strlen(filename) + 1);
|
||||
if (!cs)
|
||||
return -ENOMEM;
|
||||
|
||||
strcpy(cs->filename, filename);
|
||||
cs->line = line;
|
||||
|
||||
*s = TAKE_PTR(cs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned hashmap_find_free_section_line(Hashmap *hashmap) {
|
||||
ConfigSection *cs;
|
||||
unsigned n = 0;
|
||||
void *entry;
|
||||
|
||||
HASHMAP_FOREACH_KEY(entry, cs, hashmap)
|
||||
if (n < cs->line)
|
||||
n = cs->line;
|
||||
|
||||
return n + 1;
|
||||
}
|
||||
|
||||
#define DEFINE_PARSER(type, vartype, conv_func) \
|
||||
DEFINE_CONFIG_PARSE_PTR(config_parse_##type, conv_func, vartype, "Failed to parse " #type " value")
|
||||
|
||||
|
||||
@@ -114,6 +114,43 @@ int config_parse_many(
|
||||
void *userdata,
|
||||
Hashmap **ret_stats_by_path); /* possibly NULL */
|
||||
|
||||
typedef struct ConfigSection {
|
||||
unsigned line;
|
||||
bool invalid;
|
||||
char filename[];
|
||||
} ConfigSection;
|
||||
|
||||
static inline ConfigSection* config_section_free(ConfigSection *cs) {
|
||||
return mfree(cs);
|
||||
}
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(ConfigSection*, config_section_free);
|
||||
|
||||
int config_section_new(const char *filename, unsigned line, ConfigSection **s);
|
||||
extern const struct hash_ops config_section_hash_ops;
|
||||
unsigned hashmap_find_free_section_line(Hashmap *hashmap);
|
||||
|
||||
static inline bool section_is_invalid(ConfigSection *section) {
|
||||
/* If this returns false, then it does _not_ mean the section is valid. */
|
||||
|
||||
if (!section)
|
||||
return false;
|
||||
|
||||
return section->invalid;
|
||||
}
|
||||
|
||||
#define DEFINE_SECTION_CLEANUP_FUNCTIONS(type, free_func) \
|
||||
static inline type* free_func##_or_set_invalid(type *p) { \
|
||||
assert(p); \
|
||||
\
|
||||
if (p->section) \
|
||||
p->section->invalid = true; \
|
||||
else \
|
||||
free_func(p); \
|
||||
return NULL; \
|
||||
} \
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(type*, free_func); \
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(type*, free_func##_or_set_invalid);
|
||||
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_int);
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_unsigned);
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_long);
|
||||
|
||||
Reference in New Issue
Block a user