Add utility function to store a value into a table.

This commit is contained in:
Danila Fedorin 2018-04-26 20:59:12 -07:00
parent edcd5fc5ae
commit 51e9a11a1f
2 changed files with 31 additions and 0 deletions

View File

@ -88,5 +88,14 @@ libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type);
* @return the result of the allocations. * @return the result of the allocations.
*/ */
libab_result libab_create_function_list(libab_ref* into, libab_ref* type); 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 #endif

View File

@ -200,3 +200,25 @@ libab_result libab_create_function_list(libab_ref* into, libab_ref* type) {
return result; 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;
}