From edcd5fc5ae3778b8798e0308ed0610a0179eb5df Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 26 Apr 2018 20:14:21 -0700 Subject: [PATCH] Add the ability to store values into the table. --- include/table.h | 10 ++++++++++ src/table.c | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/table.h b/include/table.h index 33297fe..60d9001 100644 --- a/include/table.h +++ b/include/table.h @@ -52,6 +52,7 @@ struct libab_table_entry_s { libab_operator op; libab_function function; libab_basetype* basetype; + libab_ref value; } data_u; }; @@ -111,6 +112,15 @@ libab_function* libab_table_search_function(libab_table* table, */ libab_basetype* libab_table_search_basetype(libab_table* table, const char* string); +/** + * Searches for the given value in the table. + * @param table the table to search. + * @param string the table entry key. + * @param ref the reference to store the result into. + */ +void libab_table_search_value(libab_table* table, + const char* string, + libab_ref* ref); /** * Stores the given entry in the table under the given key. * @param table the table to store the entry into. diff --git a/src/table.c b/src/table.c index c25d2b3..f1f8f9d 100644 --- a/src/table.c +++ b/src/table.c @@ -77,6 +77,23 @@ libab_basetype* libab_table_search_basetype(libab_table* table, return entry ? entry->data_u.basetype : NULL; } +int _table_compare_value(const void* left, const void* right) { + const libab_table_entry* entry = right; + return entry->variant == ENTRY_VALUE; +} + +void libab_table_search_value(libab_table* table, + const char* string, + libab_ref* ref) { + libab_table_entry* entry = + libab_table_search_filter(table, string, NULL, _table_compare_value); + if(entry) { + libab_ref_copy(&entry->data_u.value, ref); + } else { + libab_ref_null(ref); + } +} + libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry) { return libab_trie_put(&table->trie, string, entry); @@ -103,5 +120,7 @@ void libab_table_entry_free(libab_table_entry* entry) { libab_function_free(&entry->data_u.function); } else if (entry->variant == ENTRY_BASETYPE) { libab_basetype_free(entry->data_u.basetype); + } else if (entry->variant == ENTRY_VALUE) { + libab_ref_free(&entry->data_u.value); } }