aboutsummaryrefslogtreecommitdiffstats
path: root/main/config.c
diff options
context:
space:
mode:
authorjpeeler <jpeeler@f38db490-d61c-443f-a65b-d21fe96a405b>2008-02-12 00:24:36 +0000
committerjpeeler <jpeeler@f38db490-d61c-443f-a65b-d21fe96a405b>2008-02-12 00:24:36 +0000
commit3847faed34e077909c3f00497c61104740240e0d (patch)
tree58614e54e5084df515faf84b5358b3c10bc0fe95 /main/config.c
parent509fcae4803f1ffe0bbb6269b6ae26be6f8babe6 (diff)
Requested changes from Pari, reviewed by Russell.
Added ability to retrieve list of categories in a config file. Added ability to retrieve the content of a particular category. Added ability to empty a context. Created new action to create a new file. Updated delete action to allow deletion by line number with respect to category. Added new action insert to add new variable to category at specified line. Updated action newcat to allow new category to be inserted in file above another existing category. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@103331 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/config.c')
-rw-r--r--main/config.c64
1 files changed, 62 insertions, 2 deletions
diff --git a/main/config.c b/main/config.c
index d5b0738fc..ce7bb50b2 100644
--- a/main/config.c
+++ b/main/config.c
@@ -354,6 +354,28 @@ void ast_variable_append(struct ast_category *category, struct ast_variable *var
category->last = category->last->next;
}
+void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line)
+{
+ struct ast_variable *cur = category->root;
+ int lineno;
+ int insertline;
+
+ if (!variable || sscanf(line, "%d", &insertline) != 1)
+ return;
+ if (!insertline) {
+ variable->next = category->root;
+ category->root = variable;
+ } else {
+ for (lineno = 1; lineno < insertline; lineno++) {
+ cur = cur->next;
+ if (!cur->next)
+ break;
+ }
+ variable->next = cur->next;
+ cur->next = variable;
+ }
+}
+
void ast_variables_destroy(struct ast_variable *v)
{
struct ast_variable *vn;
@@ -481,6 +503,26 @@ void ast_category_append(struct ast_config *config, struct ast_category *categor
config->current = category;
}
+void ast_category_insert(struct ast_config *config, struct ast_category *cat, const char *match)
+{
+ struct ast_category *cur_category;
+
+ if (!cat || !match)
+ return;
+ if (!strcasecmp(config->root->name, match)) {
+ cat->next = config->root;
+ config->root = cat;
+ return;
+ }
+ for (cur_category = config->root; cur_category; cur_category = cur_category->next) {
+ if (!strcasecmp(cur_category->next->name, match)) {
+ cat->next = cur_category->next;
+ cur_category->next = cat;
+ break;
+ }
+ }
+}
+
static void ast_destroy_comments(struct ast_category *cat)
{
struct ast_comment *n, *p;
@@ -629,10 +671,11 @@ struct ast_config *ast_config_new(void)
return config;
}
-int ast_variable_delete(struct ast_category *category, const char *variable, const char *match)
+int ast_variable_delete(struct ast_category *category, const char *variable, const char *match, const char *line)
{
struct ast_variable *cur, *prev=NULL, *curn;
int res = -1;
+ int lineno = 0;
cur = category->root;
while (cur) {
@@ -658,7 +701,7 @@ int ast_variable_delete(struct ast_category *category, const char *variable, con
cur = category->root;
while (cur) {
curn = cur->next;
- if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
+ if ((!ast_strlen_zero(line) && lineno == atoi(line)) || (ast_strlen_zero(line) && !strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match)))) {
if (prev) {
prev->next = cur->next;
if (cur == category->last)
@@ -675,6 +718,7 @@ int ast_variable_delete(struct ast_category *category, const char *variable, con
prev = cur;
cur = curn;
+ lineno++;
}
return res;
}
@@ -760,6 +804,22 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
return -1;
}
+int ast_category_empty(struct ast_config *cfg, const char *category)
+{
+ struct ast_category *cat;
+
+ for (cat = cfg->root; cat; cat = cat->next) {
+ if (!strcasecmp(cat->name, category))
+ continue;
+ ast_variables_destroy(cat->root);
+ cat->root = NULL;
+ cat->last = NULL;
+ return 0;
+ }
+
+ return -1;
+}
+
void ast_config_destroy(struct ast_config *cfg)
{
struct ast_category *cat, *catn;