From 2b6553f1ee3a6975b7511d01826f5807c49c132d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Mon, 18 May 2015 01:35:37 -0300 Subject: renderer: implemented if statements --- src/renderer.c | 42 ++++++++++++++++++++++++++++++++++-------- src/template-parser.c | 2 +- src/template-parser.h | 2 +- 3 files changed, 36 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/renderer.c b/src/renderer.c index 7a2fb77..314fe04 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -83,13 +83,16 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing const char *config_value = NULL; const char *config_var = NULL; char *config_value2 = NULL; + char *defined = NULL; unsigned int if_count = 0; unsigned int if_skip = 0; bool if_not = false; - bool defined = false; bool inside_block = false; + bool evaluate = false; + + int cmp = 0; b_slist_t *tmp = tmpl; while (tmp != NULL) { @@ -187,8 +190,9 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing case BLOGC_TEMPLATE_IFNDEF_STMT: if_not = true; + case BLOGC_TEMPLATE_IF_STMT: case BLOGC_TEMPLATE_IFDEF_STMT: - defined = false; + defined = NULL; if (stmt->value != NULL) { config_var = NULL; if (0 == strcmp(stmt->value, "DATE_FORMATTED")) @@ -203,16 +207,35 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing inside_block ? tmp_source : NULL), config, inside_block ? tmp_source : NULL); if (config_value2 != NULL) { - defined = true; - free(config_value2); + defined = config_value2; config_value2 = NULL; } } else - defined = blogc_get_variable(stmt->value, config, - inside_block ? tmp_source : NULL) != NULL; + defined = b_strdup(blogc_get_variable(stmt->value, + config, inside_block ? tmp_source : NULL)); + } + evaluate = false; + if (stmt->op != 0) { + if (defined != NULL && stmt->value2 != NULL) { + cmp = strcmp(defined, stmt->value2); + if (cmp != 0 && stmt->op & BLOGC_TEMPLATE_OP_NEQ) + evaluate = true; + else if (cmp == 0 && stmt->op & BLOGC_TEMPLATE_OP_EQ) + evaluate = true; + else if (cmp < 0 && stmt->op & BLOGC_TEMPLATE_OP_LT) + evaluate = true; + else if (cmp > 0 && stmt->op & BLOGC_TEMPLATE_OP_GT) + evaluate = true; + } + } + else { + if (if_not && defined == NULL) + evaluate = true; + if (!if_not && defined != NULL) + evaluate = true; } - if ((!if_not && !defined) || (if_not && defined)) { + if (!evaluate) { if_skip = if_count; // at this point we can just skip anything, counting the @@ -221,7 +244,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing while (1) { tmp = tmp->next; stmt = tmp->data; - if ((stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) || + if ((stmt->type == BLOGC_TEMPLATE_IF_STMT) || + (stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) || (stmt->type == BLOGC_TEMPLATE_IFNDEF_STMT)) { if_count++; @@ -237,6 +261,8 @@ blogc_render(b_slist_t *tmpl, b_slist_t *sources, b_trie_t *config, bool listing } } } + free(defined); + defined = NULL; if_not = false; break; diff --git a/src/template-parser.c b/src/template-parser.c index e1b1e56..8844b8f 100644 --- a/src/template-parser.c +++ b/src/template-parser.c @@ -399,7 +399,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err) 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; + tmp_op = BLOGC_TEMPLATE_OP_NEQ; } if (tmp_op == 0) { *err = blogc_error_parser(BLOGC_ERROR_TEMPLATE_PARSER, diff --git a/src/template-parser.h b/src/template-parser.h index 809e3bc..e608099 100644 --- a/src/template-parser.h +++ b/src/template-parser.h @@ -24,7 +24,7 @@ typedef enum { } blogc_template_stmt_type_t; typedef enum { - BLOGC_TEMPLATE_OP_NOT = 1 << 0, + BLOGC_TEMPLATE_OP_NEQ = 1 << 0, BLOGC_TEMPLATE_OP_EQ = 1 << 1, BLOGC_TEMPLATE_OP_LT = 1 << 2, BLOGC_TEMPLATE_OP_GT = 1 << 3, -- cgit v1.2.3-18-g5258