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 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'src/renderer.c') 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; -- cgit v1.2.3-18-g5258