From a71ffab0d0131cac8904dff715a1fc863ef07c48 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Mon, 9 Jan 2023 18:45:29 -0600 Subject: [PATCH] Fix ffdb_trie_list --- trie.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/trie.c b/trie.c index aafd914..0ebfd93 100644 --- a/trie.c +++ b/trie.c @@ -2,6 +2,7 @@ #include "json/json.h" #include "collections/array.h" +#include "format.h" #include #include @@ -29,6 +30,7 @@ struct trie_entry int count; } edges; char* filename; + char* prefix; }; static void trie_entry_free_composite( struct trie_entry* e ) { @@ -38,6 +40,7 @@ static void trie_entry_free_composite( struct trie_entry* e ) } free(e->edges.items); free(e->filename); + free(e->prefix); } static void trie_entry_free( struct trie_entry* e ) { @@ -310,6 +313,8 @@ static char* lookup_key( struct trie_entry* e, const char* key ) snprintf( filename, sizeof(filename), "%s%s", e->filename, ed->label ); struct trie_entry branch; + memset(&branch,0,sizeof(branch)); + trie_entry_load_from_file( filename, &branch ); char* result = lookup_key( &branch, remaining ); trie_entry_free_composite(&branch); @@ -391,7 +396,9 @@ static void load_items( struct trie_entry* e, int offset, int limit, struct stri // Load items from the branch struct trie_entry branch; + memset(&branch,0,sizeof(branch)); trie_entry_load_from_file( filename, &branch ); + branch.prefix = aformat( "%s%s", e->prefix, ed->label ); load_items( &branch, offset, branch_count, keys, values ); trie_entry_free_composite( &branch ); @@ -400,7 +407,7 @@ static void load_items( struct trie_entry* e, int offset, int limit, struct stri } else if( limit > 0 ) { // leaf - include if( keys ) { - char* str = strdup(ed->label); + char* str = aformat( "%s%s", e->prefix, ed->label ); array_append( keys, sizeof(str), &str ); } if( values ) { @@ -415,6 +422,7 @@ static void load_items( struct trie_entry* e, int offset, int limit, struct stri void ffdb_trie_list( const char* filename, int offset, int limit, void* keys_ptr, void* values_ptr ) { struct trie_entry* root = load_root_node( filename ); + root->prefix = strdup(""); load_items( root, offset, limit, keys_ptr, values_ptr ); trie_entry_free(root); }