diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-07 16:10:25 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2018-01-07 16:10:25 -0500 |
| commit | 6dab0b778c8083b53b06ca25e35f9900f474075b (patch) | |
| tree | 63002d4f3a7c456270e7f5dc6ee026da4d124e77 /ctemplates.c | |
| parent | 59c84b0460418983b0e655fd9fbba34e19b10291 (diff) | |
| download | libctemplates-6dab0b778c8083b53b06ca25e35f9900f474075b.tar.gz libctemplates-6dab0b778c8083b53b06ca25e35f9900f474075b.tar.bz2 libctemplates-6dab0b778c8083b53b06ca25e35f9900f474075b.zip | |
Fixed a bug with TMPL_IF not working as variable check
Fixed a bug where the <TMPL_IF> tag was not correctly checking if a
variable exists if it does not have a "testval" attribute.
Diffstat (limited to 'ctemplates.c')
| -rw-r--r-- | ctemplates.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/ctemplates.c b/ctemplates.c index e9e36ff..bc91441 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -580,12 +580,16 @@ parse_if(struct TMPL_token* head, struct TMPL_buf* errbuf){ t->TMPL_tag.ifelse.varname = name; /*Find the name to check against*/ int testval_offset = kmp(start_of_attribs,head->length, ATTRIBUTE_VALUE,ATTRIBUTE_VALUE_LENGTH); - char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH; - size_t value_length = get_quoted_string(start_of_value,head->length); - char* value = (char*)malloc(sizeof(char)*value_length); - memcpy(value,start_of_value,value_length); - value[value_length] = '\0'; - t->TMPL_tag.ifelse.testval = value; + if(testval_offset == -1){ + t->TMPL_tag.ifelse.testval = NULL; + }else{ + char* start_of_value = start_of_attribs + testval_offset + ATTRIBUTE_VALUE_LENGTH; + size_t value_length = get_quoted_string(start_of_value,head->length); + char* value = (char*)malloc(sizeof(char)*value_length); + memcpy(value,start_of_value,value_length); + value[value_length] = '\0'; + t->TMPL_tag.ifelse.testval = value; + } /*Find the true branch*/ struct TMPL_token* cursor = head->next; int nest_level = 0; @@ -916,9 +920,7 @@ render_if(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_varli int err = hashmap_get(varlist->map,varname,(void**)&vi); struct TMPL_tagnode* cursor; if(err == MAP_OK){ - if(testval == NULL){/*Use this if as an existance check*/ - - }else if(strcmp(vi->item.s, testval) == 0){ + if(testval == NULL || strcmp(vi->item.s, testval) == 0){ cursor = node->TMPL_tag.ifelse.tbranch; while(cursor != NULL){ render_any(t,cursor,varlist); @@ -1078,7 +1080,8 @@ TMPL_free_tagnode(struct TMPL_tagnode* tn){ case tag_elseif: case tag_else: free(tn->TMPL_tag.ifelse.varname); - free(tn->TMPL_tag.ifelse.testval); + if(tn->TMPL_tag.ifelse.testval != NULL) + free(tn->TMPL_tag.ifelse.testval); TMPL_free_tagnode(tn->TMPL_tag.ifelse.tbranch); if(tn->TMPL_tag.ifelse.fbranch){ TMPL_free_tagnode(tn->TMPL_tag.ifelse.fbranch); |
