From d728432b713702b6a564e3532930e380dd5d3f28 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Tue, 6 Feb 2018 22:42:16 -0800 Subject: Key binding menu is now functional --HG-- branch : nuklear_ui --- tern.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) (limited to 'tern.c') diff --git a/tern.c b/tern.c index 7e8e9ac..b4c26d9 100644 --- a/tern.c +++ b/tern.c @@ -138,6 +138,39 @@ tern_node *tern_find_node(tern_node *head, char const *key) return NULL; } +uint8_t tern_delete(tern_node **head, char const *key, tern_val *out) +{ + tern_node *cur = *head, **last = head; + while (cur) + { + if (cur->el == *key) { + if (*key) { + last = &cur->straight.next; + cur = cur->straight.next; + key++; + } else { + break; + } + } else if (*key < cur->el) { + last = &cur->left; + cur = cur->left; + } else { + last = &cur->right; + cur = cur->right; + } + } + if (!cur) { + return TVAL_NONE; + } + *last = cur->right; + uint8_t valtype = cur->valtype; + if (out) { + *out = cur->straight.value; + } + free(cur); + return valtype; +} + tern_val tern_find_path_default(tern_node *head, char const *key, tern_val def, uint8_t req_valtype) { tern_val ret; @@ -193,6 +226,25 @@ tern_node *tern_insert_path(tern_node *head, char const *key, tern_val val, uint } } +uint8_t tern_delete_path(tern_node **head, char const *key, tern_val *out) +{ + const char *next_key = key + strlen(key) + 1; + if (*next_key) { + tern_node *child = tern_find_node(*head, key); + if (!child) { + return TVAL_NONE; + } + tern_node *tmp = child; + uint8_t valtype = tern_delete_path(&tmp, next_key, out); + if (tmp != child) { + *head = tern_insert_node(*head, key, tmp); + } + return valtype; + } else { + return tern_delete(head, key, out); + } +} + uint32_t tern_count(tern_node *head) { uint32_t count = 0; @@ -220,7 +272,7 @@ void tern_foreach_int(tern_node *head, iter_fun fun, void *data, char *keybuf, i if (head->left) { tern_foreach_int(head->left, fun, data, keybuf, pos); } - if (head->el) { + if (head->el && head->straight.next) { if (pos == MAX_ITER_KEY) { fatal_error("tern_foreach_int: exceeded maximum key size"); } -- cgit v1.2.3