--- cfg_node.c 2002/07/10 19:26:32 1.7
+++ cfg_node.c 2002/07/12 19:59:33 1.8
@@ -48,10 +48,10 @@
n->lbroth = NULL;
n->rbroth = NULL;
n->child1 = NULL;
+ n->srcname = NULL;
+ n->srcpos = 0;
n->type = CFG_NODE_TYPE_ARG;
n->token = NULL;
- n->name = NULL;
- n->offset = 0;
cfg_data_init(&n->data);
*node = n;
return CFG_OK;
@@ -63,8 +63,8 @@
return CFG_ERR_ARG;
if (node->token != NULL)
free(node->token);
- if (node->name != NULL)
- free(node->name);
+ if (node->srcname != NULL)
+ free(node->srcname);
free(node);
return CFG_OK;
}
@@ -81,16 +81,10 @@
n->lbroth = node->lbroth;
n->rbroth = node->rbroth;
n->child1 = node->child1;
+ n->srcname = (node->srcname != NULL ? strdup(node->srcname) : NULL);
+ n->srcpos = node->srcpos;
n->type = node->type;
- if (node->token != NULL)
- n->token = strdup(node->token);
- else
- n->token = NULL;
- if (node->name != NULL)
- n->name = strdup(node->name);
- else
- n->name = NULL;
- n->offset = node->offset;
+ n->token = (node->token != NULL ? strdup(node->token) : NULL);
cfg_data_copy(&node->data, &n->data);
*node2 = n;
return CFG_OK;
@@ -104,14 +98,13 @@
return CFG_ERR_ARG;
va_start(ap, attr);
switch (attr) {
- case CFG_NODE_ATTR_TYPE: {
- node->type = (cfg_node_type_t)va_arg(ap, cfg_node_type_t);
- break;
- }
case CFG_NODE_ATTR_PARENT: {
node->parent = (cfg_node_t *)va_arg(ap, cfg_node_t *);
break;
}
+ case CFG_NODE_ATTR_LBROTH: {
+ return CFG_ERR_USE;
+ }
case CFG_NODE_ATTR_RBROTH: {
node->rbroth = (cfg_node_t *)va_arg(ap, cfg_node_t *);
break;
@@ -120,8 +113,40 @@
node->child1 = (cfg_node_t *)va_arg(ap, cfg_node_t *);
break;
}
+ case CFG_NODE_ATTR_CHILDL: {
+ return CFG_ERR_USE;
+ }
+ case CFG_NODE_ATTR_CHILDS: {
+ return CFG_ERR_USE;
+ }
+ case CFG_NODE_ATTR_NODES: {
+ return CFG_ERR_USE;
+ }
+ case CFG_NODE_ATTR_DEPTH: {
+ return CFG_ERR_USE;
+ }
+ case CFG_NODE_ATTR_SRCNAME: {
+ if (node->srcname != NULL)
+ free(node->srcname);
+ node->srcname = (char *)va_arg(ap, char *);
+ if (node->srcname != NULL)
+ node->srcname = strdup(node->srcname);
+ break;
+ }
+ case CFG_NODE_ATTR_SRCPOS: {
+ node->srcpos = (int)va_arg(ap, int);
+ break;
+ }
+ case CFG_NODE_ATTR_TYPE: {
+ node->type = (cfg_node_type_t)va_arg(ap, cfg_node_type_t);
+ break;
+ }
case CFG_NODE_ATTR_TOKEN: {
+ if (node->token != NULL)
+ free(node->token);
node->token = (char *)va_arg(ap, char *);
+ if (node->token != NULL)
+ node->token = strdup(node->token);
break;
}
case CFG_NODE_ATTR_DATA: {
@@ -134,6 +159,22 @@
return CFG_OK;
}
+static int cfg_node_get_nodes(cfg_node_t *node)
+{
+ int n;
+
+ n = 0;
+ if (node != NULL) {
+ n++;
+ if ((node = node->child1) != NULL) {
+ n += cfg_node_get_nodes(node);
+ if ((node = node->rbroth) != NULL)
+ n += cfg_node_get_nodes(node);
+ }
+ }
+ return n;
+}
+
cfg_rc_t cfg_node_get(cfg_t *cfg, cfg_node_t *node, cfg_node_attr_t attr, ...)
{
va_list ap;
@@ -142,33 +183,117 @@
return CFG_ERR_ARG;
va_start(ap, attr);
switch (attr) {
- case CFG_NODE_ATTR_TYPE: {
- cfg_node_type_t *type = (cfg_node_type_t *)va_arg(ap, void *);
- *type = node->type;
- break;
- }
case CFG_NODE_ATTR_PARENT: {
cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
+ if (n == NULL)
+ return CFG_ERR_ARG;
*n = node->parent;
break;
}
+ case CFG_NODE_ATTR_LBROTH: {
+ cfg_node_t **np = (cfg_node_t **)va_arg(ap, void *);
+ cfg_node_t *n;
+ if (np == NULL)
+ return CFG_ERR_ARG;
+ *np = NULL;
+ if ((n = node->parent) != NULL) {
+ if ((n = n->child1) != NULL) {
+ while (n->rbroth != node && n->rbroth != NULL)
+ n = n->rbroth;
+ if (n->rbroth == node)
+ *np = n;
+ }
+ }
+ break;
+ }
case CFG_NODE_ATTR_RBROTH: {
cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
+ if (n == NULL)
+ return CFG_ERR_ARG;
*n = node->rbroth;
break;
}
case CFG_NODE_ATTR_CHILD1: {
cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
+ if (n == NULL)
+ return CFG_ERR_ARG;
*n = node->child1;
break;
}
+ case CFG_NODE_ATTR_CHILDL: {
+ cfg_node_t **n = (cfg_node_t **)va_arg(ap, void *);
+ if (n == NULL)
+ return CFG_ERR_ARG;
+ if ((*n = node->child1) != NULL)
+ while ((*n)->rbroth != NULL)
+ *n = (*n)->rbroth;
+ break;
+ }
+ case CFG_NODE_ATTR_CHILDS: {
+ int *c = (int *)va_arg(ap, int *);
+ cfg_node_t *n;
+ if (c == NULL)
+ return CFG_ERR_ARG;
+ *c = 0;
+ if ((n = node->child1) != NULL) {
+ (*c)++;
+ while (n->rbroth != NULL) {
+ n = n->rbroth;
+ (*c)++;
+ }
+ }
+ break;
+ }
+ case CFG_NODE_ATTR_NODES: {
+ int *k = (int *)va_arg(ap, int *);
+ if (k == NULL)
+ return CFG_ERR_ARG;
+ *k = cfg_node_get_nodes(node);
+ break;
+ }
+ case CFG_NODE_ATTR_DEPTH: {
+ int *k = (int *)va_arg(ap, int *);
+ cfg_node_t *n;
+ if (k == NULL)
+ return CFG_ERR_ARG;
+ *k = 0;
+ n = node;
+ while ((n = n->parent) != NULL)
+ (*k)++;
+ break;
+ }
+ case CFG_NODE_ATTR_SRCNAME: {
+ char **name = (char **)va_arg(ap, char **);
+ if (name == NULL)
+ return CFG_ERR_ARG;
+ *name = node->srcname;
+ break;
+ }
+ case CFG_NODE_ATTR_SRCPOS: {
+ int *pos = (int *)va_arg(ap, int *);
+ if (pos == NULL)
+ return CFG_ERR_ARG;
+ *pos = node->srcpos;
+ break;
+ }
+ case CFG_NODE_ATTR_TYPE: {
+ cfg_node_type_t *type = (cfg_node_type_t *)va_arg(ap, void *);
+ if (type == NULL)
+ return CFG_ERR_ARG;
+ *type = node->type;
+ break;
+ }
case CFG_NODE_ATTR_TOKEN: {
char **token = (char **)va_arg(ap, char **);
+ if (token == NULL)
+ return CFG_ERR_ARG;
*token = node->token;
break;
}
case CFG_NODE_ATTR_DATA: {
cfg_data_t **data = (cfg_data_t **)va_arg(ap, void *);
+ if (data == NULL)
+ return CFG_ERR_ARG;
*data = &(node->data);
break;
}
@@ -187,65 +312,40 @@
return CFG_OK;
}
-cfg_rc_t cfg_node_goto(cfg_t *cfg, cfg_node_t *node, const char *spec, cfg_node_t **node2)
+cfg_rc_t cfg_node_select(cfg_t *cfg, cfg_node_t *node, cfg_node_t **node2, const char *fmt, ...)
{
-
- if (cfg == NULL || node == NULL || spec == NULL || node2 == NULL)
- return CFG_ERR_ARG;
- /* FIXME */
#if 0
cfg_node_t *n;
- *node2 = NULL;
- switch (id) {
- case CFG_NODE_GOTO_PARENT: {
- *node2 = node->parent;
- break;
- }
- case CFG_NODE_GOTO_LBROTH: {
- if ((n = node->parent) != NULL) {
- if ((n = n->child1) != NULL) {
- while (n->rbroth != node && n->rbroth != NULL)
- n = n->rbroth;
- if (n->rbroth == node)
- *node2 = n;
- }
- }
- break;
- }
- case CFG_NODE_GOTO_RBROTH: {
- *node2 = node->rbroth;
- break;
- }
- case CFG_NODE_GOTO_CHILD1: {
- *node2 = node->child1;
- break;
- }
- case CFG_NODE_GOTO_CHILDL: {
- if ((n = node->child1) != NULL) {
- while (n->rbroth != NULL)
- n = n->rbroth;
- *node2 = n;
- }
- break;
- }
+ va_list ap;
+ char *cpB;
+ char *cpE;
+ char *spec;
+
+ if (cfg == NULL || node == NULL || node2 == NULL || spec == NULL)
+ return CFG_ERR_ARG;
+
+ /* on-the-fly create or just take over specification string */
+ va_start(ap, fmt);
+ spec = l2_util_vasprintf(fmt, ap);
+ va_end(ap);
+
+ /* enter the parsing loop */
+ cpE = spec;
+ while (*cpE != '\0') {
+ /* determine begin of parameter name */
+ cpB = cpE;
+ if ((n = strspn(cpB, " \t\r\n")) > 0)
+ cpB += n;
+
+ /* determine end of parameter name */
+ cpE = cpB;
+ if ((n = strspn(cpB, " \t\r\n")) > 0)
+ cpB += n;
}
- if (*node2 == NULL)
- return CFG_ERR_GOT;
#endif
return CFG_OK;
}
-cfg_rc_t
-cfg_node_select(
- cfg_t *cfg,
- cfg_node_t *node,
- const char *dotpath,
- cfg_node_t **node2)
-{
- /* FIXME */
- return CFG_OK;
-}
-
cfg_rc_t
cfg_node_find(
cfg_t *cfg,
|