From 4c438a0d0b1cf978911accef7424a1e64e3dac1a Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Fri, 6 Apr 2018 12:39:07 -0400 Subject: Fixed a bug with if/elseif Elseif now works exactly like if, where it will check for the existance of a variable if it does not have a value field. --- ctemplates.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'ctemplates.c') diff --git a/ctemplates.c b/ctemplates.c index e79b676..8d4bbd6 100644 --- a/ctemplates.c +++ b/ctemplates.c @@ -522,17 +522,21 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){ t->TMPL_tag.ifelse.varname = name; 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; + } struct TMPL_token* cursor = head->next; int nest_level = 0; struct TMPL_token* fstart = NULL; while(nest_level > 0 || cursor->type != tag_end){ - if(nest_level == 0 && fstart == NULL){ + if(fstart == NULL){ if(cursor->type == tag_elseif){ fstart = cursor; }else if(cursor->type == tag_else){ @@ -559,8 +563,8 @@ parse_elseif(struct TMPL_token* head, struct TMPL_buf* errbuf){ } t->TMPL_tag.ifelse.tbranch = parse(head->next,errbuf); - t->next = NULL; - + t->next = parse(cursor->next,errbuf); + t->type = tag_elseif; return t; -- cgit v1.2.3-70-g09d2