From 51e9a11a1f62fb8bc8a0dc3f8015d98f7b2b8778 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 26 Apr 2018 20:59:12 -0700 Subject: [PATCH] Add utility function to store a value into a table. --- include/util.h | 9 +++++++++ src/util.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/util.h b/include/util.h index c8f7d87..a9a1aed 100644 --- a/include/util.h +++ b/include/util.h @@ -88,5 +88,14 @@ libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type); * @return the result of the allocations. */ libab_result libab_create_function_list(libab_ref* into, libab_ref* type); +/** + * Creates a new table entry that holds the given value. + * @param table the table to store the entry into. + * @param key the key under which to store the value. + * @param value the value to store into the table. + * @param result the result of the operation. + */ +libab_result libab_put_table_value(libab_table* table, + const char* key, libab_ref* value); #endif diff --git a/src/util.c b/src/util.c index 970b8ff..b0e6897 100644 --- a/src/util.c +++ b/src/util.c @@ -200,3 +200,25 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type) { return result; } + +libab_result libab_put_table_value(libab_table* table, + const char* key, libab_ref* value) { + libab_table_entry* entry; + libab_result result = LIBAB_SUCCESS; + if((entry = malloc(sizeof(*entry)))) { + entry->variant = ENTRY_VALUE; + libab_ref_copy(value, &entry->data_u.value); + } else { + result = LIBAB_MALLOC; + } + + if(result == LIBAB_SUCCESS) { + result = libab_table_put(table, key, entry); + if(result != LIBAB_SUCCESS) { + libab_ref_free(&entry->data_u.value); + free(entry); + } + } + + return result; +}