summaryrefslogtreecommitdiff
path: root/tern.c
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2018-02-06 22:42:16 -0800
committerMichael Pavone <pavone@retrodev.com>2018-02-06 22:42:16 -0800
commitd728432b713702b6a564e3532930e380dd5d3f28 (patch)
treec661376b671f72ab24a32241a85de63565726daf /tern.c
parentb1f354708009f7c79c1f4b31783cdd67893d7c20 (diff)
Key binding menu is now functional
--HG-- branch : nuklear_ui
Diffstat (limited to 'tern.c')
-rw-r--r--tern.c54
1 files changed, 53 insertions, 1 deletions
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");
}