int store_config_setting_mod(struct katcp_dispatch *d, char *setting, char *value) { struct katcp_stack *stack; stack = create_stack_katcp(); if (stack == NULL) return -1; if (push_named_stack_katcp(d, stack, value, KATCP_TYPE_STRING) < 0){ destroy_stack_katcp(stack); return -1; } if (parse_csv_mod(d, stack, NULL) < 0){ destroy_stack_katcp(stack); return -1; } if (store_kv_dbase_katcp(d, setting, NULL, stack) < 0){ destroy_stack_katcp(stack); return -1; } return 0; }
void print_inorder_avltree(struct katcp_dispatch *d, struct avl_node *n, void (*fn_print)(struct katcp_dispatch *d, char *key, void *data), int flags) { /* TODO: this should just be a special case of complex_inorder_traverse_avltree */ struct avl_node *c; #if 0 while ((c = walk_inorder_avltree(n)) != NULL){ #ifdef DEBUG fprintf(stderr, "avl_tree: <%s>\n", c->n_key); #endif if (flags){ append_args_katcp(d, KATCP_FLAG_FIRST, "#%s", c->n_key); append_args_katcp(d, KATCP_FLAG_LAST, "with data %p", c->n_data); } if (fn_print != NULL) (*fn_print)(d, c->n_data); } #endif #if 1 struct katcp_stack *s; if (n == NULL) return; s = create_stack_katcp(); if (s == NULL) return; c = n; while (c != NULL) { if (push_stack_katcp(s, c, NULL) < 0){ #if DEBUG>2 fprintf(stderr, "avl_tree: stack push error <%s>\n", c->n_key); #endif destroy_stack_katcp(s); return; } #if DEBUG>2 fprintf(stderr, "avl_tree: stack 1 push <%s>\n", c->n_key); #endif c = c->n_left; } #if DEBUG>2 fprintf(stderr, "avl_tree: stack state: %d\n", is_empty_stack_katcp(s)); #endif while (!is_empty_stack_katcp(s)){ c = pop_data_stack_katcp(s); if (c != NULL){ #if DEBUG>2 fprintf(stderr, "avl_tree: <%s>\n", c->n_key); #endif if (flags){ append_args_katcp(d, KATCP_FLAG_FIRST, "#%s", c->n_key); append_args_katcp(d, KATCP_FLAG_LAST, "with data %p", c->n_data); } if (fn_print != NULL) (*fn_print)(d, c->n_key, c->n_data); c = c->n_right; while(c != NULL){ if (push_stack_katcp(s, c, NULL) < 0){ #if DEBUG>2 fprintf(stderr, "avl_tree: stack push error <%s>\n", c->n_key); #endif destroy_stack_katcp(s); return; } #if DEBUG>2 fprintf(stderr, "avl_tree: stack 2 push <%s>\n", c->n_key); #endif c = c->n_left; } } } destroy_stack_katcp(s); #endif }
struct avl_node *walk_inorder_avltree(struct avl_node *n) { static struct katcp_stack *s = NULL; static int state = WALK_INIT; static struct avl_node *c; struct avl_node *rtn; while (1){ switch (state) { case WALK_INIT: #if DEBUG>2 fprintf(stderr, "walk: about to init\n"); #endif s = create_stack_katcp(); if (s == NULL) return NULL; c = n; case WALK_PUSH: #if DEBUG>2 fprintf(stderr, "walk: about to push\n"); #endif if (c != NULL){ if (push_stack_katcp(s, c, NULL) < 0) { destroy_stack_katcp(s); s = NULL; state = WALK_INIT; return NULL; } c = c->n_left; } if (c == NULL) state = WALK_POP; else state = WALK_PUSH; break; case WALK_POP: #if DEBUG>2 fprintf(stderr, "walk: about to pop\n"); #endif if (!is_empty_stack_katcp(s)){ c = pop_data_stack_katcp(s); if (c != NULL){ rtn = c; c = c->n_right; state = WALK_PUSH; return rtn; } } else { destroy_stack_katcp(s); s = NULL; state = WALK_INIT; return NULL; } break; } } return NULL; }