aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README.md4
-rw-r--r--include/ctemplates.h5
-rw-r--r--src/ctemplates.c45
-rw-r--r--src/ctemplates.h2
5 files changed, 34 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index f772350..bbe43f5 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ LIBNAME = libctemplates.a
BIN_POSTFIX = .exe
ifeq ($(DEBUG),true)
- CFLAGS += -g -O0 -Wall -Werror
+ CFLAGS += -g -O0 -Wall -Wextra -Werror
else
CFLAGS += -O3
endif
diff --git a/README.md b/README.md
index 28bf1b5..55b2ed6 100644
--- a/README.md
+++ b/README.md
@@ -52,12 +52,12 @@ libctemplates.a, for example:
### Functions
- struct TMPL_templates* TMPL_alloc_template(char* template_string)
+ int TMPL_alloc_template(char* template_string, struct TMPL_templates **t)
Creates a template from the given string. This can be pretty expensive
so try to only do it once for each template you need, and call render()
as many times as you need on that template. If there was a problem building
-the template, the returned `template->error` will be non-zero, and you can
+the template, the returned value will be non-zero, and you can
retrive an error message with `TMPL_err()`
void TMPL_free_template(struct TMPL_templates* template)
diff --git a/include/ctemplates.h b/include/ctemplates.h
index 7c8e57c..e0598ad 100644
--- a/include/ctemplates.h
+++ b/include/ctemplates.h
@@ -45,8 +45,8 @@ struct TMPL_varlist* TMPL_alloc_varlist(void);
/* Frees a varlist (and recursively, any loops under it, any varlists under those loops, ect.) */
void TMPL_free_varlist(struct TMPL_varlist* t);
-/* Allocate a new template. If allocation fails, the struct retunred will be NULL */
-struct TMPL_templates* TMPL_alloc_template(const char* t);
+/* Allocate a new template. If allocation fails, the retunred value will be non-zero*/
+int TMPL_alloc_template(const char* tmplstr, struct TMPL_templates **t);
/* Free a template. Do not use the string returned by TMPL_render() the template is freed */
void TMPL_free_template(struct TMPL_templates* t);
@@ -61,6 +61,7 @@ If size_p is non-null, the size of the resulting string will be placed in it.
If any errors happened the t->error will be non-zero and the error can be
retreived with TMPL_err(t,&size)*/
char* TMPL_render(struct TMPL_templates* t, struct TMPL_varlist* varlist, size_t* size_p);
+/*Returns a string error that might give more information*/
const char* TMPL_err(struct TMPL_templates* t,size_t* size);
/* A debug functions */
diff --git a/src/ctemplates.c b/src/ctemplates.c
index 070b51b..52f4c25 100644
--- a/src/ctemplates.c
+++ b/src/ctemplates.c
@@ -147,7 +147,10 @@ char* TMPL_render(
struct TMPL_varlist* varlist,
size_t* size_p
);
-struct TMPL_templates* TMPL_alloc_template(const char* tmplstr);
+int TMPL_alloc_template(
+ const char* tmplstr,
+ struct TMPL_templates **t
+ );
void TMPL_free_template(struct TMPL_templates* t);
void TMPL_free_tagnode(struct TMPL_tagnode* tn);
char* TMPL_get_error(struct TMPL_templates* t);
@@ -208,7 +211,7 @@ print_ast_helper(struct TMPL_templates* t, struct TMPL_tagnode* cursor, int leve
case tag_text:
printf("TAG Text \"");
if(cursor->TMPL_tag.text.len < 10){
- int i;
+ size_t i;
for(i = 0; i < cursor->TMPL_tag.text.len; i++)
putchar(*(cursor->TMPL_tag.text.start + i));
printf("\"\n");
@@ -253,11 +256,11 @@ print_ast_helper(struct TMPL_templates* t, struct TMPL_tagnode* cursor, int leve
printf("TAG End\n");
break;
case tag_loop:
- printf("TAG Loop (%s)(%p), body:\n",cursor->TMPL_tag.loop.loopname, cursor);
+ printf("TAG Loop (%s)(%p), body:\n",cursor->TMPL_tag.loop.loopname, (void*)cursor);
print_ast_helper(t,cursor->TMPL_tag.loop.body,level+1);
break;
case tag_break:
- printf("TAG Break (%d) -> (%p)\n",cursor->TMPL_tag.breakcont.level, cursor->TMPL_tag.breakcont.into);
+ printf("TAG Break (%d) -> (%p)\n",cursor->TMPL_tag.breakcont.level, (void*)cursor->TMPL_tag.breakcont.into);
break;
case tag_continue:
printf("TAG Continue (%d)\n",cursor->TMPL_tag.breakcont.level);
@@ -626,6 +629,10 @@ parse_loop(struct TMPL_token* head, struct TMPL_buf* errbuf){
const char end_str[] = "";
struct TMPL_tagnode* parse_end(struct TMPL_token* head, struct TMPL_buf* errbuf){
struct TMPL_tagnode* t = alloc_tagnode();
+ if(t == NULL){
+ bputs(errbuf,"Parse error: Failed to create a tag node");
+ return NULL;
+ }
head->into = t;
t->TMPL_tag.text.start = end_str;
t->TMPL_tag.text.len = 0;
@@ -815,6 +822,7 @@ TMPL_alloc_varlist(){
int
TMPL_free_hashmapitems(any_t a, any_t b){
+ (void)(a);/*Unused parameter*/
struct TMPL_varitem* vi = (struct TMPL_varitem*)b;
TMPL_free_varitem(vi);
return MAP_OK;
@@ -952,6 +960,12 @@ compile(const char* tmplstr){
return ret;
}
+void
+advance_cursor(struct TMPL_templates* t){
+ if(t->cursor != NULL && t->cursor->type != tag_end)
+ t->cursor = t->cursor->next;
+}
+
int
render_variable(struct TMPL_templates* t, struct TMPL_varlist* varlist){
struct TMPL_varitem* vi;
@@ -960,7 +974,6 @@ render_variable(struct TMPL_templates* t, struct TMPL_varlist* varlist){
if(err == MAP_OK){
bputs(t->out,vi->item.s);
}else if(err == MAP_MISSING){
- size_t has_default = t->cursor->TMPL_tag.var.default_len;
if(t->cursor->TMPL_tag.var.defaultval != NULL){
bputs(t->out,t->cursor->TMPL_tag.var.defaultval);
}else{
@@ -976,14 +989,10 @@ render_variable(struct TMPL_templates* t, struct TMPL_varlist* varlist){
return 0;
}
-void
-advance_cursor(struct TMPL_templates* t){
- if(t->cursor != NULL && t->cursor->type != tag_end)
- t->cursor = t->cursor->next;
-}
int
render_text(struct TMPL_templates* t, struct TMPL_varlist* varlist){
+ (void)(varlist); /*Unused parameter*/
struct TMPL_buf* buf = t->out;
char* text = (char*)t->cursor->TMPL_tag.text.start;
size_t length = t->cursor->TMPL_tag.text.len;
@@ -1053,7 +1062,6 @@ render_if(struct TMPL_templates* t, struct TMPL_varlist* varlist){
nt->breaks = t->breaks;
nt->continues = t->continues;
nt->jumping = 0;
- struct TMPL_tagnode* cursor;
if(err == MAP_OK){
//These two if statements can't be combined because of
//the condition for the elseif
@@ -1110,6 +1118,7 @@ resolve_name(struct TMPL_varlist* varlist, char* name,struct TMPL_varitem** item
int
render_break(struct TMPL_templates* t, struct TMPL_varlist* varlist){
+ (void)(varlist); /*Unused parameter*/
t->breaks += t->cursor->TMPL_tag.breakcont.level;
t->jumping = 1;
/*t->cursor = t->cursor->TMPL_tag.breakcont.into->next;*/
@@ -1119,6 +1128,7 @@ render_break(struct TMPL_templates* t, struct TMPL_varlist* varlist){
int
render_continue(struct TMPL_templates* t, struct TMPL_varlist* varlist){
+ (void)(varlist); /*Unused parameter*/
t->continues = t->continues + t->cursor->TMPL_tag.breakcont.level;
t->jumping = 1;
return 0;
@@ -1144,7 +1154,6 @@ render_loop(struct TMPL_templates* t, struct TMPL_varlist* varlist){
struct TMPL_loop* cursor;
/*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){
- size_t dummy;
nt->cursor = nt->roottag;
if(err != 0){
}
@@ -1190,8 +1199,7 @@ render_loop(struct TMPL_templates* t, struct TMPL_varlist* varlist){
int
render_any(struct TMPL_templates* t, struct TMPL_varlist* varlist){
/*Interpret the template*/
- if(t->jumping) return;
- size_t dummy;
+ if(t->jumping) return 0;
int err;
switch(t->cursor->type){
case tag_text:
@@ -1252,15 +1260,16 @@ TMPL_err(struct TMPL_templates* t, size_t* size){
return bstringify(t->errout,size);
}
-struct TMPL_templates*
-TMPL_alloc_template(const char* tmplstr){
+int
+TMPL_alloc_template(const char* tmplstr, struct TMPL_templates **t){
struct TMPL_templates* n = compile(tmplstr);
if(n != NULL){
- return n;
+ *t = n;
+ return 0;
}else{
TMPL_free_template(n);
- return NULL;
+ return -1;
}
}
diff --git a/src/ctemplates.h b/src/ctemplates.h
index da56b97..91762a8 100644
--- a/src/ctemplates.h
+++ b/src/ctemplates.h
@@ -160,7 +160,7 @@ void TMPL_free_varlist(struct TMPL_varlist* t);
struct TMPL_varitem* TMPL_alloc_varitem(void);
void TMPL_free_varitem(struct TMPL_varitem* vi);
-struct TMPL_templates* TMPL_alloc_template(const char* t);
+int TMPL_alloc_template(const char* tmplstr, struct TMPL_templates **t);
void TMPL_free_template(struct TMPL_templates* t);
struct TMPL_loop* TMPL_alloc_loop(void);