Skip to content

Commit

Permalink
Add cf_section_dup, doesn't currently dup data, but could do in futur…
Browse files Browse the repository at this point in the history
…e...
  • Loading branch information
arr2036 committed Dec 30, 2014
1 parent 429e7d9 commit bfd1a0b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/include/conffile.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp);
void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp);

CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2);
CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs, char const *name1, char const *name2);
void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs);
int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value);
int cf_item_parse(CONF_SECTION *cs, char const *name, int type, void *data, char const *dflt);
Expand Down
50 changes: 50 additions & 0 deletions src/main/conffile.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,56 @@ CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char con
return cs;
}

/** Duplicate a configuration section
*
* @note recursively duplicates any child sections.
* @note does not duplicate any data associated with a section, or its child sections.
*
* @param parent section.
* @param cs to duplicate.
* @return a duplicate of the existing section, or NULL on error.
*/
CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs, char const *name1, char const *name2)
{
CONF_SECTION *new, *child;
CONF_PAIR *cp;
CONF_ITEM *ci;

new = cf_section_alloc(parent, name1, name2);
new->template = cs->template;
new->base = cs->base;
new->depth = cs->depth;
new->variables = cs->variables;
new->item.lineno = cs->item.lineno;
new->item.filename = talloc_strdup(new, cs->item.filename);

for (ci = cf_item_find_next(cs, NULL);
ci;
ci = cf_item_find_next(cs, ci)) {
if (cf_item_is_section(ci)) {
child = cf_itemtosection(ci);
child = cf_section_dup(new, child, cf_section_name1(child), cf_section_name2(child));
if (!child) {
talloc_free(new);
return NULL;
}
cf_section_add(new, child);
continue;
}

if (cf_item_is_pair(ci)) {
cp = cf_pair_dup(new, cf_itemtopair(ci));
if (!cp) {
talloc_free(new);
return NULL;
}
cf_pair_add(new, cp);
}
}

return new;
}

void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
{
cf_item_add(parent, &(cs->item));
Expand Down

0 comments on commit bfd1a0b

Please sign in to comment.