2 * Copyright (c) 2008 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /******************************************************************************
31 * Html template library *
32 ******************************************************************************
33 ******************************************************************************
35 ******************************************************************************
36 * <% EXPRESSION , "FORMAT"] [# "DEFAULTVALUE"] [|(h|u)]%>
37 * - format value should be as in strftime for time value and printf
38 * for all other. Currently, bool values have only "true"/"false"
44 * Expression is classical with support following:
46 * variable defined from C-code. Mark '^' means global
47 * variable, not local in loop.
48 * - expression (+|-|*|/|%) expression
49 * ariphmetic operations
50 * - expression ( || | && ) expression
52 * logical OR, AND and NOT
53 * - expression ( < | <= | == | >= | > | != | <> ) expression
55 * - LENGTH(expression)
56 * computes length of string
57 * - DEFINED(expression)
58 * returns true if expression is defined
59 * - expression ? expression : expression
61 * - USERDEFINEDFUNCTION( [expression[,expression[...]]] )
62 * User defined function call. Function should be defined at
67 * Loop has predefined variables:
68 * __FIRST - true for first iteration
69 * __LAST - true for last iteration
70 * __COUNTER - iteration's number
71 * __SIZE - number of iterations
72 * __ODD - true for odd iteraion
73 * __EVEN - true for even iteraion
79 ******************************************************************************
81 ******************************************************************************
82 * - setTemplateValueInt
83 * setTemplateValueString
84 * setTemplateValueTime
85 * setTemplateValueBool
86 * setTemplateValueDouble
87 * setTemplateValueUndefined
88 * Sets varibale's value
90 * Add one iteration to the pointed loop. Local variable in loop should
91 * pointed with predecence loop's mark(s) separated by dot. Example:
100 * addTemplateRow("outerLoop");
101 * setTemplateValueBool("outerLoop.var1");
102 * addTemplateRow("innerLoop");
103 * setTemplateValueBool("outerLoop.innerLoop.var2");
105 ******************************************************************************
106 * Memory management *
107 ******************************************************************************
108 * Unfortunatly, I'm too lazy to unify memory usage by several pieces
109 * in library. So, library uses mixed plain malloc and memory context
110 * concepts (tmalloc.h).
111 * To work with library it's needed to allocate persitent memory context
112 * for initTemplate() call and temprorary memory context for usual work.
113 * after printTemplate is called it's needed to call resetTemplate() and
114 * then resetMemoryContext() for second context.
116 ******************************************************************************/
118 #ifndef __TEMPLATE_H__
119 #define __TEMPLATE_H__
121 #include <sys/types.h>
127 typedef enum TemplateNodeType {
128 /* node tree types */
139 /* value's types of variables */
140 valueInt = 200, /* smallest of any values type */
148 #define TND_HTMLESCAPE (0x0001)
149 #define TND_URLESCAPE (0x0002)
150 #define TND_GLOBAL (0x0004)
152 #define TND___FIRST (0x0008)
153 #define TND___LAST (0x0010)
154 #define TND___COUNTER (0x0020)
155 #define TND___SIZE (0x0040)
156 #define TND___ODD (0x0080)
157 #define TND___EVEN (0x0100)
159 #define TND_DEFINED (0x0200)
161 #define TND__SPECIALMASK (TND___FIRST | TND___LAST | TND___COUNTER | TND___SIZE | TND___ODD | TND___EVEN)
163 typedef struct TemplateData *Template;
165 typedef struct TemplateNodeData *TemplateNode;
167 typedef struct VariableValueData * VariableValue;
168 typedef struct VariableValueData {
169 TemplateNodeType type; /* should be first, see resetTemplate/freeTemplate */
177 VariableValue ptrValue; /* special value to handle loop variables,
178 points to members of loopValues */
183 typedef struct executeFunctionDescData *executeFunctionDesc;
184 typedef struct executeFunctionDescData {
186 int nargs; /* -1 - variable number */
187 VariableValue (*execFn)(Template, int, VariableValue*);
188 } executeFunctionDescData;
190 typedef struct LoopInstanceData * LoopInstance;
191 typedef struct LoopInstanceData {
196 typedef struct TemplateNodeData {
197 TemplateNodeType type; /* should be first, see resetTemplate/freeTemplate */
216 VariableValue *argsValue;
219 executeFunctionDesc function;
220 GList *argsNode; /* list of arguments nodes */
224 VariableValueData value;
228 TemplateNode expressionNode;
241 TemplateNode bodyNode;
242 GList *childrenLoop; /* to reset loop's instance */
243 GList *listVarValues; /* list of loop variables */
249 TemplateNode expressionNode;
251 TemplateNode elseNode;
261 typedef void (*outFn)(char *, int);
263 typedef char* (*urlEscapeFn)(char *, int * /* in/out */);
264 typedef char* (*htmlEscapeFn)(char *, int * /* in/out */);
266 typedef struct TemplateData {
268 MemoryContext *templateContext;
271 urlEscapeFn urlEscape;
272 htmlEscapeFn htmlEscape;
273 executeFunctionDesc functions;
276 int parseTemplateFile(Template tmpl, char* filename ); /* return non-zero if error */
277 int initTemplate( Template tmpl, MemoryContext *mc, executeFunctionDesc functions, char *basedir, char *filename );
278 void freeTemplate( Template tmpl );
279 void resetTemplate( Template tmpl );
280 int printTemplate( Template tmpl );
283 #define TVAR_NOTFOUND (1)
284 #define TVAR_FORBIDDEN (2)
285 #define TVAR_NOROW (3)
286 #define TVAR_LOOPMARK (4)
288 int setTemplateValueInt( Template tmpl, char * key, int val );
289 int setTemplateValueString( Template tmpl, char * key, char * val );
290 int setTemplateValueTime( Template tmpl, char * key, time_t val );
291 int setTemplateValueBool( Template tmpl, char * key, int val );
292 int setTemplateValueUndefined( Template tmpl, char * key );
293 int setTemplateValueDouble( Template tmpl, char * key, double val );
294 int addTemplateRow( Template tmpl, char * key );
296 void dumpTemplate( Template tmpl );