From 7bc58226a529293c42e82eba3b37eecf7f5978c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 4 Mar 2019 18:31:00 +0100 Subject: set-attr! can now add new keys. --- guile_interface.scm.c | 15 ++++++++++++++- trie.inc.h | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/guile_interface.scm.c b/guile_interface.scm.c index 41d13c3c..761bf7c2 100644 --- a/guile_interface.scm.c +++ b/guile_interface.scm.c @@ -68,9 +68,22 @@ SCM_DEFINE (vcomponent_set_attr_x, "%vcomponent-set-attribute!", 3, 0, 0, char* key = scm_to_utf8_stringn(scm_string_upcase(attr), NULL); content_line* c = get_property (com, key); + + /* Create the position in the TRIE if it doesn't already exist */ + if (c == NULL) { + /* Insert empty key since this allows me to use the helper + * function */ + vcomponent_push_val(com, key, ""); + c = get_property (com, key); + } else { + /* If the object already exists it should be protected, + * so unprotect it + */ + scm_gc_unprotect_object(c->cur->value->key.scm); + } + free(key); - scm_gc_unprotect_object(c->cur->value->key.scm); c->cur->value->key.scm = new_value; scm_gc_protect_object(c->cur->value->key.scm); diff --git a/trie.inc.h b/trie.inc.h index 20cc93b5..ffc8ac8e 100644 --- a/trie.inc.h +++ b/trie.inc.h @@ -88,6 +88,8 @@ TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) { while (n != NULL) { if (subkey[1] == '\0') { + /* Wanted node found, + * value can however be NULL */ return n->value; } else if (subkey[0] == n->c) { n = n->child; @@ -97,7 +99,7 @@ TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) { } } - ERR("Position not found"); + /* Position not found */ return 0; } -- cgit v1.2.3