Add simple template library
[tedtools.git] / template.h
1 /*
2  * Copyright (c) 2008 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 /******************************************************************************
31  *                                  SYNTAX                                    *
32  ******************************************************************************
33  * <% [^]VARNAME[, "FORMAT"] [||"DEFAULTVALUE"] [|(h|u)]%>
34  *    - '^' mark means global variable.
35  *    - format value should be as in strftime for time value and printf
36  *      for all other. Currently, bool values have only "true"/"false"
37  *      string values
38  * <@IF [NOT] [DEFINED] [^]VARNAME@>
39  * <@ELSE@>
40  * <@ENDIF@>
41  *
42  * <@LOOP MARKNAME@>
43  *
44  * <@ENDLOOP@>
45  *
46  * <& FILENAME &>
47  *
48  * <# comment #>
49  *
50  ******************************************************************************/
51
52 #ifndef __TEMPLATE_H__
53 #define __TEMPLATE_H__
54
55 #include <sys/types.h>
56 #include <time.h>
57 #include "glist.h"
58 #include "sfxstr.h"
59
60 typedef enum TemplateNodeType {
61         /* node tree types */
62         TextNode = 100,
63         VariableNode,
64         IncludeNode,
65         LoopNode,
66         ConditionNode,
67         CollectionNode,
68
69         /* value's types of variables */
70         valueInt = 200, /* smallest of any values type */
71         valueString,
72         valueTime,
73         valueBool,
74         valueDouble,
75         valuePointer
76 } TemplateNodeType;
77
78 #define TND_DEFINED                     (0x0001)
79 #define TND_NOT                         (0x0002)
80 #define TND_HTMLESCAPE          (0x0004)
81 #define TND_URLESCAPE           (0x0008)
82 #define TND_GLOBAL                      (0x0010)
83 #define TND___FIRST                     (0x0020)
84 #define TND___LAST                      (0x0040)
85 #define TND___COUNTER           (0x0080)
86 #define TND___SIZE                      (0x0100)
87 #define TND___ODD                       (0x0200)
88 #define TND___EVEN                      (0x0400)
89
90 #define TND__SPECIALMASK        (TND___FIRST | TND___LAST | TND___COUNTER | TND___SIZE | TND___ODD | TND___EVEN)
91 struct  TemplateNodeData;
92 typedef struct TemplateNodeData *TemplateNode;
93
94 typedef struct VariableValueData * VariableValue;
95 typedef struct VariableValueData {
96         TemplateNodeType        type; /* should be first, see resetTemplate/freeTemplate */
97         int                                     flags;
98         union {
99                 int                             intValue;
100                 char                    *stringValue;
101                 time_t                  timeValue;
102                 int                             boolValue;
103                 double                  doubleValue;
104                 VariableValue   ptrValue; /* special value to handle loop variables,
105                                                                          points to members of loopValues */
106         } value;
107 } VariableValueData;
108
109 typedef struct LoopInstanceData * LoopInstance;
110 typedef struct LoopInstanceData {
111         int                             nrow;
112         GList                   *rowValues;
113 } LoopInstanceData;
114
115 typedef struct  TemplateNodeData {
116         TemplateNodeType        type; /* should be first, see resetTemplate/freeTemplate */
117
118         union {
119                 /* TextNode */
120                 struct {
121                         char    *value;
122                         int             valueLength;
123                 } text;
124
125                 /* VariableNode */
126                 struct {
127                         char                    *varName;
128                         int                             varNameLength;
129                         VariableValue   value;
130                         int                             flags;
131                         char                    *formatValue;
132                         char                    *defaultValue;
133                 } variable;
134
135                 /* IncludeNode */
136                 char    *includeFile;
137
138                 /* LoopNode */
139                 struct {
140                         char                    *varName;
141                         int                     varNameLength;
142                         TemplateNode    bodyNode;
143                         int                             counter;
144                         GList                   *childrenLoop;  /* to reset instance  */
145                         GList                   *listVarValues; /* list of loop variables */
146                         GList                   *listInstance;
147                 } loop;
148
149                 /* ConditionNode */
150                 struct {
151                         int                     flags;
152                         char                    *varName;
153                         int                     varNameLength;
154                         VariableValue   value;
155                         TemplateNode    ifNode;
156                         TemplateNode    elseNode;
157                 } condition;
158
159                 /* CollectionNode */
160                 GList   *children;
161         } nodeData;
162         
163 } TemplateNodeData;
164
165 /* prints result */
166 typedef void  (*outFn)(char *, int);
167
168 typedef char*   (*urlEscapeFn)(char *, int * /* in/out */);
169 typedef char*   (*htmlEscapeFn)(char *, int * /* in/out */);
170
171 typedef struct TemplateData {
172         TemplateNode    tree;
173         MemoryContext   *templateContext;
174         SFSTree                 variables;
175         outFn                   printString;
176         urlEscapeFn             urlEscape;
177         htmlEscapeFn    htmlEscape;
178 } TemplateData;
179
180 typedef struct TemplateData *Template;
181
182 int parseTemplateFile(Template tmpl, char* filename );  /* return non-zero if error */
183 int initTemplate( Template tmpl, MemoryContext *mc, char *basedir, char *filename );
184 void freeTemplate( Template tmpl );
185 void resetTemplate( Template tmpl );
186 int printTemplate( Template tmpl );
187
188 #define TVAR_OK                 (0)
189 #define TVAR_NOTFOUND   (1)
190 #define TVAR_FORBIDDEN  (2)
191 #define TVAR_NOROW              (3)
192 #define TVAR_LOOPMARK   (4)
193
194 int setTemplateValueInt( Template tmpl, char * key, int val );
195 int setTemplateValueString( Template tmpl, char * key, char * val );
196 int setTemplateValueTime( Template tmpl, char * key, time_t val );
197 int setTemplateValueBool( Template tmpl, char * key, int val );
198 int setTemplateValueUndefined( Template tmpl, char * key );
199 int setTemplateValueDouble( Template tmpl, char * key, double val );
200 int addTemplateRow( Template tmpl, char * key );
201
202 void dumpTemplate( Template tmpl );
203 #endif