Add code to de-register patterns.

This commit is contained in:
Danila Fedorin 2018-02-05 22:42:02 -08:00
parent 3b72569b33
commit 945426e245
2 changed files with 32 additions and 3 deletions

View File

@ -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.

View File

@ -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);