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 | |
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')
-rw-r--r-- | src/template-parser.c | 49 | ||||
-rw-r--r-- | src/template-parser.h | 9 |
2 files changed, 35 insertions, 23 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); } diff --git a/src/template-parser.h b/src/template-parser.h index b9213e2..809e3bc 100644 --- a/src/template-parser.h +++ b/src/template-parser.h @@ -23,11 +23,18 @@ typedef enum { BLOGC_TEMPLATE_CONTENT_STMT, } blogc_template_stmt_type_t; +typedef enum { + BLOGC_TEMPLATE_OP_NOT = 1 << 0, + BLOGC_TEMPLATE_OP_EQ = 1 << 1, + BLOGC_TEMPLATE_OP_LT = 1 << 2, + BLOGC_TEMPLATE_OP_GT = 1 << 3, +} blogc_template_stmt_operator_t; + typedef struct { blogc_template_stmt_type_t type; char *value; char *value2; - char *op; + blogc_template_stmt_operator_t op; } blogc_template_stmt_t; b_slist_t* blogc_template_parse(const char *src, size_t src_len, |