Better config query & multiple value/tag support

Signed-off-by: Lon Hohberger <lon@users.sourceforge.net>
This commit is contained in:
Lon Hohberger 2010-01-08 16:40:31 -05:00
parent 6502600674
commit 2643994886
3 changed files with 129 additions and 21 deletions

View File

@ -13,6 +13,7 @@ struct value {
struct node { struct node {
char *id; char *id;
char *val;
struct node *nodes; struct node *nodes;
struct value *values; struct value *values;
struct node *next; struct node *next;
@ -30,8 +31,8 @@ extern struct node *node_list;
extern struct parser_context *context_stack; extern struct parser_context *context_stack;
int _sc_value_add(char *id, char *val, struct value **list); int _sc_value_add(char *id, char *val, struct value **list);
int _sc_node_add(char *id, struct value *vallist, struct node *nodelist, int _sc_node_add(char *id, char *val, struct value *vallist,
struct node **list); struct node *nodelist, struct node **list);
#endif #endif

View File

@ -32,8 +32,8 @@ _sc_value_add(char *id, char *val, struct value **list)
int int
_sc_node_add(char *id, struct value *vallist, struct node *nodelist, _sc_node_add(char *id, char *val, struct value *vallist,
struct node **list) struct node *nodelist, struct node **list)
{ {
struct node *n; struct node *n;
@ -45,6 +45,7 @@ _sc_node_add(char *id, struct value *vallist, struct node *nodelist,
memset(n, 0, sizeof(*n)); memset(n, 0, sizeof(*n));
//snprintf(n->id, sizeof(n->id), "%s", id); //snprintf(n->id, sizeof(n->id), "%s", id);
n->id = id; /* malloc'd during parsing */ n->id = id; /* malloc'd during parsing */
n->val = val; /* malloc'd during parsing */
n->values = vallist; n->values = vallist;
n->nodes = nodelist; n->nodes = nodelist;
n->next = *list; n->next = *list;
@ -72,7 +73,19 @@ node:
struct parser_context *c = NULL; struct parser_context *c = NULL;
c = context_stack; c = context_stack;
_sc_node_add($1, val_list, node_list, &c->node_list); _sc_node_add($1, NULL, val_list, node_list, &c->node_list);
val_list = c->val_list;
node_list = c->node_list;
context_stack = c->next;
free(c);
}
|
T_ID T_EQ T_VAL T_OBRACE stuff T_CBRACE {
struct parser_context *c = NULL;
c = context_stack;
_sc_node_add($1, $3, val_list, node_list, &c->node_list);
val_list = c->val_list; val_list = c->val_list;
node_list = c->node_list; node_list = c->node_list;
context_stack = c->next; context_stack = c->next;
@ -84,7 +97,19 @@ node:
struct parser_context *c = NULL; struct parser_context *c = NULL;
c = context_stack; c = context_stack;
_sc_node_add($1, val_list, node_list, &c->node_list); _sc_node_add($1, NULL, val_list, node_list, &c->node_list);
val_list = c->val_list;
node_list = c->node_list;
context_stack = c->next;
free(c);
}
|
T_ID T_EQ T_VAL T_OBRACE T_CBRACE {
struct parser_context *c = NULL;
c = context_stack;
_sc_node_add($1, $3, val_list, node_list, &c->node_list);
val_list = c->val_list; val_list = c->val_list;
node_list = c->node_list; node_list = c->node_list;
context_stack = c->next; context_stack = c->next;

View File

@ -2,6 +2,8 @@
#include <pthread.h> #include <pthread.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
#include <stdlib.h>
#include <assert.h>
#include "simpleconfig.h" #include "simpleconfig.h"
#include "config-stack.h" #include "config-stack.h"
@ -37,7 +39,12 @@ _sc_dump_d(struct node *node, int depth, FILE *fp)
for (x = 0; x < depth; x++) for (x = 0; x < depth; x++)
fprintf(fp, "\t"); fprintf(fp, "\t");
fprintf(fp, "%s {\n", node->id);
if (node->val) {
fprintf(fp, "%s = \"%s\" {\n", node->id, node->val);
} else {
fprintf(fp, "%s {\n", node->id);
}
for (n = node->nodes; n; n = n->next) { for (n = node->nodes; n; n = n->next) {
_sc_dump_d(n, depth+1, fp); _sc_dump_d(n, depth+1, fp);
@ -161,45 +168,120 @@ _sc_get(config_info_t *config, const char *key, char *value, size_t valuesz)
struct value *v, *values = ((struct parser_context *)config)->val_list; struct value *v, *values = ((struct parser_context *)config)->val_list;
char *ptr; char *ptr;
char *slash; char *slash;
char *bracket;
char *id;
int req_index = 0;
int curr_index = 0;
int found; int found;
if (!config) if (!config)
return 1; return -1;
assert(strlen(key) < sizeof(buf));
ptr = (char *)key; ptr = (char *)key;
top:
while ((slash = strchr(ptr, '/'))) { while ((slash = strchr(ptr, '/'))) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strncpy(buf, ptr, (slash - ptr)); strncpy(buf, ptr, (slash - ptr));
ptr = ++slash; ptr = ++slash;
id = NULL;
bracket = strchr(buf, '[');
if (bracket) {
*bracket = 0;
++bracket;
id = bracket;
bracket = strchr(bracket, ']');
if (!bracket)
return 1;
*bracket = 0;
if (id[0] == '@') {
++id;
if (!strlen(id)) {
return 1;
}
} else {
req_index = atoi(id);
if (req_index <= 0)
return 1;
id = NULL;
}
}
found = 0; found = 0;
curr_index = 0;
for (n = node; n; n = n->next) { for (n = node; n; n = n->next) {
if (!strcasecmp(n->id, buf)) {
node = n->nodes; if (strcasecmp(n->id, buf))
values = n->values; continue;
found = 1;
break; ++curr_index;
if (req_index && (curr_index != req_index)) {
continue;
} else if (id && strcasecmp(n->val, id)) {
continue;
} }
node = n->nodes;
values = n->values;
found = 1;
break;
} }
if (!found) if (!found)
return 1; return 1;
} }
if (ptr[0] != '@') if (ptr[0] != '@') {
if (node->val) {
strncpy(value, node->val, valuesz);
return 0;
}
return 1; return 1;
}
++ptr; ++ptr;
found = 0; found = 0;
id = NULL;
strncpy(buf, ptr, sizeof(buf));
bracket = strchr(buf, '[');
req_index = 0;
curr_index = 0;
if (bracket) {
*bracket = 0;
++bracket;
id = bracket;
bracket = strchr(bracket, ']');
if (!bracket)
return 1;
*bracket = 0;
req_index = atoi(id);
if (req_index <= 0)
return 1;
id = NULL;
}
for (v = values; v; v = v->next) { for (v = values; v; v = v->next) {
if (!strcasecmp(v->id, ptr)) {
if (v->val == NULL) if (strcasecmp(v->id, buf))
return 1; continue;
snprintf(value, valuesz, "%s", v->val);
return 0; ++curr_index;
} if (req_index && (curr_index != req_index))
continue;
snprintf(value, valuesz, "%s", v->val);
return 0;
} }
return 1; return 1;
@ -238,7 +320,7 @@ _sc_set(config_info_t *config, const char *key, const char *value)
id_dup = strdup(buf); id_dup = strdup(buf);
if (!id_dup) if (!id_dup)
return -1; return -1;
_sc_node_add(id_dup, NULL, NULL, nodes); _sc_node_add(id_dup, NULL, NULL, NULL, nodes);
n = *nodes; n = *nodes;
nodes = &n->nodes; nodes = &n->nodes;
values = &n->values; values = &n->values;