diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-05-17 23:43:31 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-05-17 23:43:31 -0300 |
commit | 64e772c1125acadad8167fd5fd25b380f333ab86 (patch) | |
tree | f905c15fe3d3d703c4a7f8531a30482848132e6d /src/template-parser.c | |
parent | d7f5252a0d7c4c428bfbd560ce78b81073d3d6ec (diff) | |
download | blogc-64e772c1125acadad8167fd5fd25b380f333ab86.tar.gz blogc-64e772c1125acadad8167fd5fd25b380f333ab86.tar.bz2 blogc-64e772c1125acadad8167fd5fd25b380f333ab86.zip |
template-parser: convert "if" operator into enum
Diffstat (limited to 'src/template-parser.c')
-rw-r--r-- | src/template-parser.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/src/template-parser.c b/src/template-parser.c index 47cb762..e1b1e56 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -61,6 +61,8 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) size_t start2 = 0; size_t end2 = 0; + blogc_template_stmt_operator_t tmp_op = 0; + unsigned int if_count = 0; b_slist_t *stmts = NULL; @@ -81,7 +83,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = b_strndup(src + start, src_len - start); - stmt->op = NULL; + stmt->op = 0; stmt->value2 = NULL; stmts = b_slist_append(stmts, stmt); stmt = NULL; @@ -102,7 +104,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = b_strndup(src + start, end - start); - stmt->op = NULL; + stmt->op = 0; stmt->value2 = NULL; stmts = b_slist_append(stmts, stmt); stmt = NULL; @@ -381,37 +383,41 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) case TEMPLATE_CLOSE_BRACKET: if (c == '}') { + tmp_op = 0; if (op_end > op_start) { - if ((!((op_end - op_start == 1) && - (0 == strncmp("<", src + op_start, op_end - op_start)))) && - (!((op_end - op_start == 1) && - (0 == strncmp(">", src + op_start, op_end - op_start)))) && - (!((op_end - op_start == 2) && - (0 == strncmp("<=", src + op_start, op_end - op_start)))) && - (!((op_end - op_start == 2) && - (0 == strncmp(">=", src + op_start, op_end - op_start)))) && - (!((op_end - op_start == 2) && - (0 == strncmp("==", src + op_start, op_end - op_start)))) && - (!((op_end - op_start == 2) && - (0 == strncmp("!=", src + op_start, op_end - op_start))))) - { + if (op_end - op_start == 1) { + if (0 == strncmp("<", src + op_start, 1)) + tmp_op = BLOGC_TEMPLATE_OP_LT; + else if (0 == strncmp(">", src + op_start, 1)) + tmp_op = BLOGC_TEMPLATE_OP_GT; + } + else if (op_end - op_start == 2) { + if (0 == strncmp("<=", src + op_start, 2)) + tmp_op = BLOGC_TEMPLATE_OP_LT | BLOGC_TEMPLATE_OP_EQ; + else if (0 == strncmp(">=", src + op_start, 2)) + tmp_op = BLOGC_TEMPLATE_OP_GT | BLOGC_TEMPLATE_OP_EQ; + else if (0 == strncmp("==", src + op_start, 2)) + tmp_op = BLOGC_TEMPLATE_OP_EQ; + else if (0 == strncmp("!=", src + op_start, 2)) + tmp_op = BLOGC_TEMPLATE_OP_NOT | BLOGC_TEMPLATE_OP_EQ; + } + if (tmp_op == 0) { *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, src, src_len, op_start, "Invalid 'if' operator. Must be '<', '>', " "'<=', '>=', '==' or '!='."); + op_start = 0; + op_end = 0; break; } + op_start = 0; + op_end = 0; } stmt = b_malloc(sizeof(blogc_template_stmt_t)); stmt->type = type; stmt->value = NULL; - stmt->op = NULL; + stmt->op = tmp_op; stmt->value2 = NULL; - if (op_end > op_start) { - stmt->op = b_strndup(src + op_start, op_end - op_start); - op_start = 0; - op_end = 0; - } if (end > start) stmt->value = b_strndup(src + start, end - start); if (end2 > start2) { @@ -473,7 +479,6 @@ blogc_template_free_stmts(b_slist_t *stmts) if (data == NULL) continue; free(data->value); - free(data->op); free(data->value2); free(data); } |