From 945426e2452df05c20db5bfe9fd4ddf1b6c8de57 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 5 Feb 2018 22:42:02 -0800 Subject: [PATCH] Add code to de-register patterns. --- include/eval.h | 9 +++++++++ src/eval.c | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/eval.h b/include/eval.h index 30e39ec..d5f9c8b 100644 --- a/include/eval.h +++ b/include/eval.h @@ -114,6 +114,15 @@ liblex_result eval_config_free(eval_config* config); * @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure. */ liblex_result eval_config_add(eval_config* config, const char* pattern, int pattern_id); +/** + * Removes a pattern from this configuration that has been previously + * registered. + * @param config the configuration to add the pattern to. + * @param pattern the pattern to remove. + * @param pattern_id the id associated with the pattern. + * @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure. + */ +liblex_result eval_config_remove(eval_config* config, const char* pattern, int pattern_id); /** * Evaluates / finds a single word. * @param string the string to evaluate. diff --git a/src/eval.c b/src/eval.c index 890487f..0a062be 100644 --- a/src/eval.c +++ b/src/eval.c @@ -41,14 +41,34 @@ liblex_result eval_config_add(eval_config* config, const char* ptrn, int pattern return result; } -int _eval_config_foreach_free(void *data, va_list args){ - eval_state* state = data; - int result = pattern_free(state->pattern) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC; +liblex_result _eval_config_free_state(eval_state* state) { + liblex_result result = pattern_free(state->pattern); free(state->pattern); free(state); return result; } +liblex_result eval_config_remove(eval_config* config, const char* pattern, int pattern_id) { + liblex_result result = LIBLEX_SUCCESS; + ll_node** head = &(config->states.head); + while(*head && result == LIBLEX_SUCCESS) { + eval_state* current_state = (*head)->data; + if(strcmp(current_state->source, pattern) == 0 && current_state->pattern->head->pattern_id == pattern_id) { + ll_node* to_delete = *head; + (*head) = (*head)->next; + free(to_delete); + result = _eval_config_free_state(current_state); + } else { + head = &((*head)->next); + } + } + return result; +} + +int _eval_config_foreach_free(void* data, va_list args){ + return _eval_config_free_state(data) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC; +} + liblex_result eval_config_free(eval_config* config){ liblex_result result = foreach_errors[ll_foreach(&config->states, NULL, compare_always, _eval_config_foreach_free)]; ll_free(&config->states);