Compare commits
2 Commits
bb07382b59
...
945426e245
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | 945426e245 | |
Danila Fedorin | 3b72569b33 |
|
@ -74,9 +74,26 @@ struct eval_config_s {
|
|||
ll states;
|
||||
};
|
||||
|
||||
/**
|
||||
* Information about a state that has been
|
||||
* added with eval_config_add.
|
||||
*/
|
||||
struct eval_state_s {
|
||||
/**
|
||||
* The string that was used to compile
|
||||
* this state.
|
||||
*/
|
||||
const char* source;
|
||||
/**
|
||||
* The pattern that was generated with this state.
|
||||
*/
|
||||
pattern* pattern;
|
||||
};
|
||||
|
||||
typedef struct match_s match;
|
||||
typedef struct eval_s eval;
|
||||
typedef struct eval_config_s eval_config;
|
||||
typedef struct eval_state_s eval_state;
|
||||
|
||||
/**
|
||||
* Initializes the evaluation configuration.
|
||||
|
@ -97,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.
|
||||
|
|
49
src/eval.c
49
src/eval.c
|
@ -16,30 +16,59 @@ void eval_config_init(eval_config* config){
|
|||
}
|
||||
liblex_result eval_config_add(eval_config* config, const char* ptrn, int pattern_id){
|
||||
liblex_result result;
|
||||
pattern* new_pattern = malloc(sizeof(*new_pattern));
|
||||
if(new_pattern){
|
||||
pattern* new_pattern;
|
||||
eval_state* new_state;
|
||||
new_pattern = malloc(sizeof(*new_pattern));
|
||||
new_state = malloc(sizeof(*new_pattern));
|
||||
if(new_pattern && new_state){
|
||||
result = pattern_compile(new_pattern, ptrn, pattern_id);
|
||||
if(result == LIBLEX_SUCCESS){
|
||||
result = (ll_append(&config->states, new_pattern) == LIBDS_SUCCESS) ?
|
||||
new_state->source = ptrn;
|
||||
new_state->pattern = new_pattern;
|
||||
result = (ll_append(&config->states, new_state) == LIBDS_SUCCESS) ?
|
||||
LIBLEX_SUCCESS : LIBLEX_MALLOC;
|
||||
}
|
||||
if(result != LIBLEX_SUCCESS){
|
||||
pattern_free(new_pattern);
|
||||
free(new_pattern);
|
||||
}
|
||||
} else {
|
||||
result = LIBLEX_MALLOC;
|
||||
}
|
||||
if(result != LIBLEX_SUCCESS) {
|
||||
free(new_pattern);
|
||||
free(new_state);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int _eval_config_foreach_free(void *data, va_list args){
|
||||
pattern* ptrn = data;
|
||||
int result = pattern_free(ptrn) == LIBLEX_SUCCESS ? 0 : EVAL_FOREACH_MALLOC;
|
||||
free(ptrn);
|
||||
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);
|
||||
|
@ -79,9 +108,9 @@ liblex_result _eval_pairmap_add_node(ht *table, pattern_node *node){
|
|||
}
|
||||
|
||||
int _eval_foreach_add_node(void *data, va_list args){
|
||||
pattern* new_pattern = data;
|
||||
eval_state* state = data;
|
||||
ht* pairmap = va_arg(args, ht*);
|
||||
pattern_node* pattern_head = new_pattern->head;
|
||||
pattern_node* pattern_head = state->pattern->head;
|
||||
return _eval_pairmap_add_node(pairmap, pattern_head) == LIBLEX_SUCCESS? 0 : EVAL_FOREACH_MALLOC;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue