aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile27
-rw-r--r--build/.gitignore0
-rw-r--r--include/ctemplates.h60
-rw-r--r--src/ctemplates.c (renamed from ctemplates.c)22
-rw-r--r--src/ctemplates.h (renamed from ctemplates.h)2
-rw-r--r--src/fbuf.c (renamed from fbuf.c)0
-rw-r--r--src/fbuf.h (renamed from fbuf.h)0
-rw-r--r--src/hashmap.c (renamed from hashmap.c)0
-rw-r--r--src/hashmap.h (renamed from hashmap.h)0
-rw-r--r--src/kmp.c (renamed from kmp.c)0
-rw-r--r--src/kmp.h (renamed from kmp.h)0
11 files changed, 90 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 6f46156..d6580b2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-CFLAGS = -I. -L. -std=c99 -pedantic
+CFLAGS = -Isrc/ -L. -std=c99 -pedantic
CC?=gcc
LIBNAME = libctemplates.a
BIN_POSTFIX = .exe
@@ -9,26 +9,21 @@ else
CFLAGS += -O3
endif
-TEST_1_NAME = t/test_1$(BIN_POSTFIX)
-
-$(LIBNAME): ctemplates.o fbuf.o kmp.o hashmap.o
- ar rc $(LIBNAME) ctemplates.o fbuf.o kmp.o hashmap.o
- ranlib $(LIBNAME)
+objs = ctemplates fbuf hashmap kmp
+objfiles = $(objs:%=build/%.o)
-fbuf.o : fbuf.c fbuf.h
- $(CC) $(CFLAGS) -c -o fbuf.o fbuf.c
-kmp.o : kmp.c kmp.h
- $(CC) $(CFLAGS) -c -o kmp.o kmp.c
+TEST_1_NAME = t/test_1$(BIN_POSTFIX)
-ctemplates.o: ctemplates.c ctemplates.h
- $(CC) $(CFLAGS) -c -o ctemplates.o ctemplates.c
+$(LIBNAME): $(objfiles)
+ ar rc $@ $^
+ ranlib $@
-hashmap.o: hashmap.c hashmap.h
- $(CC) $(CFLAGS) -c -o hashmap.o hashmap.c
+$(objfiles) : build/%.o : src/%.c src/%.h
+ $(CC) $(CFLAGS) -c -o $@ $<
clean:
- rm -f *.o *.a template
+ rm -f build/*.o *.a template
$(TEST_1_NAME): t/test_1.c $(LIBNAME)
$(CC) $(CFLAGS) -o $(TEST_1_NAME) t/test_1.c -lctemplates
@@ -39,4 +34,4 @@ test: $(TEST_1_NAME)
install: $(LIBNAME)
cp $(LIBNAME) /usr/local/lib
mkdir -p /usr/local/include/ctemplates
- cp *.h /usr/local/include/ctemplates/
+ cp include/ctemplates.h /usr/local/include/ctemplates/
diff --git a/build/.gitignore b/build/.gitignore
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/build/.gitignore
diff --git a/include/ctemplates.h b/include/ctemplates.h
new file mode 100644
index 0000000..a8855a8
--- /dev/null
+++ b/include/ctemplates.h
@@ -0,0 +1,60 @@
+/*
+ * C TemplateS Library 0.1 -
+ * Forked from C Template Library 1.0 by Stephen C. Losen.
+ *
+ * Copyright 2017 Alexander M. Pickering Distributed under the terms
+ * of the GNU General Public License (GPL)
+ */
+
+#ifndef _CTEMPLATE_H
+#define _CTEMPLATE_H
+
+#define MAX_TEMPLATE_LENGTH 2147384647
+
+#define ERRBUF_HINTLEN 50
+
+//Length of error messages
+#define ERR_MSG_LEN 500
+
+struct TMPL_varlist{
+ map_t map;
+}TMPL_varlist;
+
+struct TMPL_loop{
+ struct TMPL_varlist* varlist;
+ size_t loop_len;
+ struct TMPL_loop* next;
+ struct TMPL_loop* tail;
+};
+
+/* Holdes all the data needed for a template*/
+struct TMPL_templates{
+ struct TMPL_buf* out;
+ struct TMPL_buf* errout;
+ struct TMPL_tagnode* roottag;
+ int linenum;
+ int error;
+} TMPL_templates;
+
+
+void TMPL_add_var_to_varlist(struct TMPL_varlist* vl, char* name, char* value);
+void TMPL_add_loop_to_varlist(struct TMPL_varlist* vl, char* name, struct TMPL_loop* loop);
+void TMPL_add_varlist_to_loop(struct TMPL_loop* l, struct TMPL_varlist* vl);
+
+
+struct TMPL_varlist* TMPL_alloc_varlist(void);
+void TMPL_free_varlist(struct TMPL_varlist* t);
+
+struct TMPL_templates* TMPL_alloc_template(char* t);
+void TMPL_free_template(struct TMPL_templates* t);
+
+struct TMPL_loop* TMPL_alloc_loop(void);
+void TMPL_free_loop(struct TMPL_loop* tl);
+
+char* TMPL_render(struct TMPL_templates* t, struct TMPL_varlist* varlist, size_t* size_p);
+
+/*A debug function*/
+void print_varlist(struct TMPL_varlist* vl);
+void print_ast(struct TMPL_templates* root);
+
+#endif
diff --git a/ctemplates.c b/src/ctemplates.c
index aa7c650..c154dcb 100644
--- a/ctemplates.c
+++ b/src/ctemplates.c
@@ -58,7 +58,7 @@ void print_ast_helper(
struct TMPL_tagnode* cursor,
int level
);
-void print_ast(struct TMPL_tagnode* root);
+void print_ast(struct TMPL_templates* root);
struct TMPL_token* TMPL_tokenize(char* tmplstr, size_t strlen);
struct TMPL_tagnode* alloc_tagnode(void);
size_t get_quoted_string(char* start, size_t len);
@@ -425,8 +425,8 @@ print_ast_helper(struct TMPL_tagnode* cursor, int level){
}
}
void
-print_ast(struct TMPL_tagnode* root){
- print_ast_helper(root,0);
+print_ast(struct TMPL_templates* t){
+ print_ast_helper(t->roottag,0);
}
struct TMPL_token*
@@ -980,7 +980,21 @@ 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);
+ char *varstring;
+ switch(vi->type){
+ case 0:
+ varstring = "Null";
+ break;
+ case 1:
+ varstring = "Loop";
+ break;
+ case 2:
+ varstring = "Var";
+ break;
+ default:
+ varstring = "Error";
+ }
+ printf("Print pair, varitem is %p, type is %d (%s)\n",(void*)vi,(int)vi->type, varstring);
int* ip = (int*)indent;
int ind = *ip;
if(vi->type == vartype_var){
diff --git a/ctemplates.h b/src/ctemplates.h
index d1b6bba..658a9f3 100644
--- a/ctemplates.h
+++ b/src/ctemplates.h
@@ -203,6 +203,6 @@ void TMPL_free_tagnode(struct TMPL_tagnode* tn);
/*A debug function*/
void print_varlist(struct TMPL_varlist* vl);
-void print_ast(struct TMPL_tagnode* root);
+void print_ast(struct TMPL_templates* root);
#endif
diff --git a/fbuf.c b/src/fbuf.c
index 9ce4163..9ce4163 100644
--- a/fbuf.c
+++ b/src/fbuf.c
diff --git a/fbuf.h b/src/fbuf.h
index 212979b..212979b 100644
--- a/fbuf.h
+++ b/src/fbuf.h
diff --git a/hashmap.c b/src/hashmap.c
index f91e743..f91e743 100644
--- a/hashmap.c
+++ b/src/hashmap.c
diff --git a/hashmap.h b/src/hashmap.h
index 16c76dd..16c76dd 100644
--- a/hashmap.h
+++ b/src/hashmap.h
diff --git a/kmp.c b/src/kmp.c
index d5923bf..d5923bf 100644
--- a/kmp.c
+++ b/src/kmp.c
diff --git a/kmp.h b/src/kmp.h
index 24c609e..24c609e 100644
--- a/kmp.h
+++ b/src/kmp.h