aboutsummaryrefslogtreecommitdiff
path: root/ctemplates.c
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2018-01-05 20:07:03 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2018-01-05 20:07:03 -0500
commit59c84b0460418983b0e655fd9fbba34e19b10291 (patch)
tree21dce8b7a1f6c3f4e2eaab3180866f609c5d56d5 /ctemplates.c
parent9c7882627fd28c060f53d86e194fb864302a347c (diff)
downloadlibctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.tar.gz
libctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.tar.bz2
libctemplates-59c84b0460418983b0e655fd9fbba34e19b10291.zip
Started writing unit tests
Added some tests for good input. Also fixed a bug where an empty loop would crash.
Diffstat (limited to 'ctemplates.c')
-rw-r--r--ctemplates.c48
1 files changed, 39 insertions, 9 deletions
diff --git a/ctemplates.c b/ctemplates.c
index d858aa9..e9e36ff 100644
--- a/ctemplates.c
+++ b/ctemplates.c
@@ -747,7 +747,8 @@ TMPL_free_loop(struct TMPL_loop* tl){
if(tl->next != NULL){
TMPL_free_loop(tl->next);
}
- TMPL_free_varlist(tl->varlist);
+ if(tl->varlist != NULL)
+ TMPL_free_varlist(tl->varlist);
free(tl);
}
@@ -874,6 +875,7 @@ render_text(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_var
int
print_pair(any_t indent, any_t b){
struct TMPL_varitem* vi = (struct TMPL_varitem*)b;
+ printf("Print pair, varitem is %p, type is %d\n",(void*)vi,(int)vi->type);
int* ip = (int*)indent;
int ind = *ip;
if(vi->type == vartype_var){
@@ -885,7 +887,9 @@ print_pair(any_t indent, any_t b){
struct TMPL_loop* cursor;
printf("{\n");
ind++;
- for(cursor = vi->item.l; cursor != NULL; cursor = cursor->next){
+ /*If the loop doesn't have any varlists added, the cursor's varlist will be null*/
+ for(cursor = vi->item.l; cursor != NULL && cursor->varlist != NULL; cursor = cursor->next){
+ printf("First iteration though, cursor was %p\n",(void*)cursor);
print_varlist_helper(cursor->varlist,ind);
}
printf("}\n");
@@ -911,12 +915,20 @@ render_if(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_varli
struct TMPL_varitem* vi;
int err = hashmap_get(varlist->map,varname,(void**)&vi);
struct TMPL_tagnode* cursor;
- if(err == MAP_OK && strcmp(vi->item.s,testval) == 0){
- cursor = node->TMPL_tag.ifelse.tbranch;
- while(cursor != NULL){
- render_any(t,cursor,varlist);
- cursor = cursor->next;
+ if(err == MAP_OK){
+ if(testval == NULL){/*Use this if as an existance check*/
+
+ }else if(strcmp(vi->item.s, testval) == 0){
+ cursor = node->TMPL_tag.ifelse.tbranch;
+ while(cursor != NULL){
+ render_any(t,cursor,varlist);
+ cursor = cursor->next;
+ }
+
+ }else{
+
}
+
}else if(node->TMPL_tag.ifelse.fbranch != NULL){
cursor = node->TMPL_tag.ifelse.fbranch;
while(cursor != NULL){
@@ -934,10 +946,27 @@ render_elseif(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_v
}
int
+resolve_name(struct TMPL_varlist* varlist, char* name,struct TMPL_varitem** item){
+ return hashmap_get(varlist->map,name,(void*)item);
+ /*
+ struct TMPL_varlist* cursor = varlist;
+ int err;
+ do{
+ err = hashmap_get(cursor->map,name,(void*)item);
+ cursor = cursor->parent;
+ }while(cursor != NULL && err == MAP_MISSING);
+ if(err == MAP_MISSING){
+ return MAP_MISSING;
+ }
+ return MAP_OK;
+ */
+}
+
+int
render_loop(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_varlist* varlist){
char* loopname = node->TMPL_tag.loop.loopname;
struct TMPL_varitem* loop;
- int err = hashmap_get(varlist->map,loopname,(void*)&loop);
+ int err = resolve_name(varlist,loopname,&loop);
if(err != MAP_OK){
return -1;
}
@@ -946,7 +975,8 @@ render_loop(struct TMPL_templates* t, struct TMPL_tagnode* node, struct TMPL_var
nt->errout = t->errout;
nt->roottag = node->TMPL_tag.loop.body;
struct TMPL_loop* cursor;
- for(cursor = loop->item.l; cursor != NULL; cursor = cursor->next){
+ /*If the loop has no items, it's varlist will be null*/
+ for(cursor = loop->item.l; cursor != NULL && cursor->varlist != NULL; cursor = cursor->next){
if(err != 0){
}
TMPL_render_helper(nt,cursor->varlist);