aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2018-01-07 16:10:25 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2018-01-07 16:10:25 -0500
commit6dab0b778c8083b53b06ca25e35f9900f474075b (patch)
tree63002d4f3a7c456270e7f5dc6ee026da4d124e77
parent59c84b0460418983b0e655fd9fbba34e19b10291 (diff)
downloadlibctemplates-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.
-rw-r--r--ctemplates.c23
-rw-r--r--t/test_1.c48
2 files changed, 52 insertions, 19 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);
diff --git a/t/test_1.c b/t/test_1.c
index 038f7bb..aa72213 100644
--- a/t/test_1.c
+++ b/t/test_1.c
@@ -39,6 +39,14 @@ char t_9[] = "Test if for existance:<TMPL_IF name=\"var\"> Exists!<TMPL_END>";
char c_9_1[] = "Test if for existance: Exists!";
char c_9_2[] = "Test if for existance:";
+char t_10[] = "Test else:<TMPL_IF name=\"var\"> One<TMPL_ELSE> Two<TMPL_END>";
+char c_10_1[] = "Test else: One";
+char c_10_2[] = "Test else: Two";
+
+char t_11[] = "Test else2:<TMPL_IF name=\"var\" value=\"thing\"> One<TMPL_ELSE> Two<TMPL_END>";
+char c_11_1[] = "Test else2: One";
+char c_11_2[] = "Test else2: Two";
+
int main(){
struct TMPL_templates* t;
struct TMPL_varlist* vl;
@@ -113,7 +121,7 @@ int main(){
vl = TMPL_alloc_varlist();
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_6_1) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 6\n");
printf("Result should have been '%s'\n was '%s'\n",c_6_1,ret);
return -1;
}
@@ -129,7 +137,7 @@ int main(){
}
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_7_1) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 7\n");
printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret);
return -1;
}
@@ -144,7 +152,7 @@ int main(){
}
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_7_2) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 7\n");
printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret);
return -1;
}
@@ -165,7 +173,7 @@ int main(){
}
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_7_3) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 7\n");
printf("Result should have been '%s'\n was '%s'\n",c_7_1,ret);
return -1;
}
@@ -178,7 +186,7 @@ int main(){
TMPL_add_var_to_varlist(vl,"var","correct");
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_8_1) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 8\n");
printf("Result should have been '%s'\n was '%s'\n",c_8_1,ret);
return -1;
}
@@ -187,7 +195,7 @@ int main(){
TMPL_add_var_to_varlist(vl,"var","incorrect");
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_8_2) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 8\n");
printf("Result should have been '%s'\n was '%s'\n",c_8_2,ret);
return -1;
}
@@ -195,19 +203,20 @@ int main(){
vl = TMPL_alloc_varlist();
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_8_3) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 8\n");
printf("Result should have been '%s'\n was '%s'\n",c_8_3,ret);
return -1;
}
TMPL_free_template(t);
TMPL_free_varlist(vl);
+ /*Test 9*/
t = TMPL_alloc_template(t_9);
vl = TMPL_alloc_varlist();
TMPL_add_var_to_varlist(vl,"var","thing");
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_9_1) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 9\n");
printf("Result should have been '%s'\n was '%s'\n",c_9_1,ret);
return -1;
}
@@ -215,13 +224,34 @@ int main(){
vl = TMPL_alloc_varlist();
ret = TMPL_render(t,vl,&dummy);
if(strcmp(ret,c_9_2) != 0){
- fprintf(stderr, "Error in test file 1, test 5\n");
+ fprintf(stderr, "Error in test file 1, test 9\n");
printf("Result should have been '%s'\n was '%s'\n",c_9_1,ret);
return -1;
}
TMPL_free_varlist(vl);
TMPL_free_template(t);
+ /*Test 10*/
+ t = TMPL_alloc_template(t_10);
+ vl = TMPL_alloc_varlist();
+ TMPL_add_var_to_varlist(vl,"var","True");
+ ret = TMPL_render(t,vl,&dummy);
+ if(strcmp(ret, c_10_1) != 0){
+ fprintf(stderr, "Error in test file 1, test 10\n");
+ printf("Result should have been '%s'\n was '%s'\n",c_10_1,ret);
+ return -1;
+ }
+ TMPL_free_varlist(vl);
+ vl = TMPL_alloc_varlist();
+ ret = TMPL_render(t,vl,&dummy);
+ if(strcmp(ret, c_10_2) != 0){
+ fprintf(stderr, "Error in test file 1, test 10\n");
+ printf("Result should have been '%s'\n was '%s'\n",c_10_2,ret);
+ return -1;
+ }
+ TMPL_free_varlist(vl);
+ TMPL_free_template(t);
+
return 0;