Merge pull request #29123 from yuwata/conf-parser-cleanups-for-config-section
conf-parser: several cleanups and generalizations for ConfigSection
This commit is contained in:
@@ -748,9 +748,13 @@ static int config_section_compare_func(const ConfigSection *x, const ConfigSecti
|
||||
|
||||
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) {
|
||||
int config_section_new(const char *filename, unsigned line, ConfigSection **ret) {
|
||||
ConfigSection *cs;
|
||||
|
||||
assert(filename);
|
||||
assert(line > 0);
|
||||
assert(ret);
|
||||
|
||||
cs = malloc0(offsetof(ConfigSection, filename) + strlen(filename) + 1);
|
||||
if (!cs)
|
||||
return -ENOMEM;
|
||||
@@ -758,21 +762,31 @@ int config_section_new(const char *filename, unsigned line, ConfigSection **s) {
|
||||
strcpy(cs->filename, filename);
|
||||
cs->line = line;
|
||||
|
||||
*s = TAKE_PTR(cs);
|
||||
|
||||
*ret = TAKE_PTR(cs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned hashmap_find_free_section_line(Hashmap *hashmap) {
|
||||
int _hashmap_by_section_find_unused_line(
|
||||
HashmapBase *entries_by_section,
|
||||
const char *filename,
|
||||
unsigned *ret) {
|
||||
|
||||
ConfigSection *cs;
|
||||
unsigned n = 0;
|
||||
void *entry;
|
||||
|
||||
HASHMAP_FOREACH_KEY(entry, cs, hashmap)
|
||||
if (n < cs->line)
|
||||
n = cs->line;
|
||||
HASHMAP_BASE_FOREACH_KEY(entry, cs, entries_by_section) {
|
||||
if (filename && !streq(cs->filename, filename))
|
||||
continue;
|
||||
n = MAX(n, cs->line);
|
||||
}
|
||||
|
||||
return n + 1;
|
||||
/* overflow? */
|
||||
if (n >= UINT_MAX)
|
||||
return -EFBIG;
|
||||
|
||||
*ret = n + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define DEFINE_PARSER(type, vartype, conv_func) \
|
||||
|
||||
@@ -136,9 +136,24 @@ static inline ConfigSection* config_section_free(ConfigSection *cs) {
|
||||
}
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(ConfigSection*, config_section_free);
|
||||
|
||||
int config_section_new(const char *filename, unsigned line, ConfigSection **s);
|
||||
int config_section_new(const char *filename, unsigned line, ConfigSection **ret);
|
||||
extern const struct hash_ops config_section_hash_ops;
|
||||
unsigned hashmap_find_free_section_line(Hashmap *hashmap);
|
||||
int _hashmap_by_section_find_unused_line(
|
||||
HashmapBase *entries_by_section,
|
||||
const char *filename,
|
||||
unsigned *ret);
|
||||
static inline int hashmap_by_section_find_unused_line(
|
||||
Hashmap *entries_by_section,
|
||||
const char *filename,
|
||||
unsigned *ret) {
|
||||
return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret);
|
||||
}
|
||||
static inline int ordered_hashmap_by_section_find_unused_line(
|
||||
OrderedHashmap *entries_by_section,
|
||||
const char *filename,
|
||||
unsigned *ret) {
|
||||
return _hashmap_by_section_find_unused_line(HASHMAP_BASE(entries_by_section), filename, ret);
|
||||
}
|
||||
|
||||
static inline bool section_is_invalid(ConfigSection *section) {
|
||||
/* If this returns false, then it does _not_ mean the section is valid. */
|
||||
|
||||
Reference in New Issue
Block a user