diff options
-rw-r--r-- | src/template-parser.c | 49 | ||||
-rw-r--r-- | src/template-parser.h | 9 | ||||
-rw-r--r-- | tests/check_template_parser.c | 6 |
3 files changed, 38 insertions, 26 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, diff --git a/tests/check_template_parser.c b/tests/check_template_parser.c index 8ce6e6d..b018a89 100644 --- a/tests/check_template_parser.c +++ b/tests/check_template_parser.c @@ -35,11 +35,11 @@ blogc_assert_template_stmt(b_slist_t *l, const char *value, static void blogc_assert_template_if_stmt(b_slist_t *l, const char *variable, - const char *operator, const char *operand) + blogc_template_stmt_operator_t operator, const char *operand) { blogc_template_stmt_t *stmt = l->data; assert_string_equal(stmt->value, variable); - assert_string_equal(stmt->op, operator); + assert_int_equal(stmt->op, operator); assert_string_equal(stmt->value2, operand); assert_int_equal(stmt->type, BLOGC_TEMPLATE_IF_STMT); } @@ -106,7 +106,7 @@ test_template_parse(void **state) blogc_assert_template_stmt(tmp->next->next->next->next->next->next->next->next->next, "\n", BLOGC_TEMPLATE_CONTENT_STMT); tmp = tmp->next->next->next->next->next->next->next->next->next->next; - blogc_assert_template_if_stmt(tmp, "BOLA", "==", "1\\\"0"); + blogc_assert_template_if_stmt(tmp, "BOLA", BLOGC_TEMPLATE_OP_EQ, "1\\\"0"); blogc_assert_template_stmt(tmp->next, "aee", BLOGC_TEMPLATE_CONTENT_STMT); blogc_assert_template_stmt(tmp->next->next, NULL, BLOGC_TEMPLATE_ENDIF_STMT); |