diff --git a/include/util.h b/include/util.h index 74db6fc..92f04bb 100644 --- a/include/util.h +++ b/include/util.h @@ -187,5 +187,19 @@ void* libab_unwrap_param(libab_ref_vec* vec, size_t index); * @param buffer_size the size of the to buffer. */ void libab_sanitize(char* to, const char* from, size_t buffer_size); +/** + * Creates a new instance of libabacus allocated on the heap. + * @param into the pointer into which to store the newly allocated libab instance. + * @param parse_function the function used to parse numbers. + * @param free_function the function used to free parsed numbers. + */ +libab_result libab_create_instance(libab** into, + void* (*parse_function)(const char*), + void (*free_function)(void*)); +/** + * Destroys the given libabacus instance allocated on the stack. + * @param into the reference to destroy. + */ +void libab_destroy_instance(libab* into); #endif diff --git a/src/util.c b/src/util.c index 8fc8218..df2b3b4 100644 --- a/src/util.c +++ b/src/util.c @@ -481,3 +481,23 @@ void libab_sanitize(char* to, const char* from, size_t buffer_size) { } to[index] = '\0'; } + +libab_result libab_create_instance(libab** into, + void* (*parse_function)(const char*), + void (*free_function)(void*)) { + libab_result result = LIBAB_SUCCESS; + if((*into = malloc(sizeof(**into)))) { + result = libab_init(*into, parse_function, free_function); + if(result != LIBAB_SUCCESS) { + free(*into); + *into = NULL; + } + } else { + result = LIBAB_MALLOC; + } + return result; +} +void libab_destroy_instance(libab* into) { + libab_free(into); + free(into); +}