add tmalloc.h header file
[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 #include "tmalloc.h"
60
61 typedef enum TemplateNodeType {
62         /* node tree types */
63         TextNode = 100,
64         VariableNode,
65         IncludeNode,
66         LoopNode,
67         ConditionNode,
68         CollectionNode,
69
70         /* value's types of variables */
71         valueInt = 200, /* smallest of any values type */
72         valueString,
73         valueTime,
74         valueBool,
75         valueDouble,
76         valuePointer
77 } TemplateNodeType;
78
79 #define TND_DEFINED                     (0x0001)
80 #define TND_NOT                         (0x0002)
81 #define TND_HTMLESCAPE          (0x0004)
82 #define TND_URLESCAPE           (0x0008)
83 #define TND_GLOBAL                      (0x0010)
84 #define TND___FIRST                     (0x0020)
85 #define TND___LAST                      (0x0040)
86 #define TND___COUNTER           (0x0080)
87 #define TND___SIZE                      (0x0100)
88 #define TND___ODD                       (0x0200)
89 #define TND___EVEN                      (0x0400)
90
91 #define TND__SPECIALMASK        (TND___FIRST | TND___LAST | TND___COUNTER | TND___SIZE | TND___ODD | TND___EVEN)
92 struct  TemplateNodeData;
93 typedef struct TemplateNodeData *TemplateNode;
94
95 typedef struct VariableValueData * VariableValue;
96 typedef struct VariableValueData {
97         TemplateNodeType        type; /* should be first, see resetTemplate/freeTemplate */
98         int                                     flags;
99         union {
100                 int                             intValue;
101                 char                    *stringValue;
102                 time_t                  timeValue;
103                 int                             boolValue;
104                 double                  doubleValue;
105                 VariableValue   ptrValue; /* special value to handle loop variables,
106                                                                          points to members of loopValues */
107         } value;
108 } VariableValueData;
109
110 typedef struct LoopInstanceData * LoopInstance;
111 typedef struct LoopInstanceData {
112         int                             nrow;
113         GList                   *rowValues;
114 } LoopInstanceData;
115
116 typedef struct  TemplateNodeData {
117         TemplateNodeType        type; /* should be first, see resetTemplate/freeTemplate */
118
119         union {
120                 /* TextNode */
121                 struct {
122                         char    *value;
123                         int             valueLength;
124                 } text;
125
126                 /* VariableNode */
127                 struct {
128                         char                    *varName;
129                         int                             varNameLength;
130                         VariableValue   value;
131                         int                             flags;
132                         char                    *formatValue;
133                         char                    *defaultValue;
134                 } variable;
135
136                 /* IncludeNode */
137                 char    *includeFile;
138
139                 /* LoopNode */
140                 struct {
141                         char                    *varName;
142                         int                     varNameLength;
143                         TemplateNode    bodyNode;
144                         int                             counter;
145                         GList                   *childrenLoop;  /* to reset instance  */
146                         GList                   *listVarValues; /* list of loop variables */
147                         GList                   *listInstance;
148                 } loop;
149
150                 /* ConditionNode */
151                 struct {
152                         int                     flags;
153                         char                    *varName;
154                         int                     varNameLength;
155                         VariableValue   value;
156                         TemplateNode    ifNode;
157                         TemplateNode    elseNode;
158                 } condition;
159
160                 /* CollectionNode */
161                 GList   *children;
162         } nodeData;
163         
164 } TemplateNodeData;
165
166 /* prints result */
167 typedef void  (*outFn)(char *, int);
168
169 typedef char*   (*urlEscapeFn)(char *, int * /* in/out */);
170 typedef char*   (*htmlEscapeFn)(char *, int * /* in/out */);
171
172 typedef struct TemplateData {
173         TemplateNode    tree;
174         MemoryContext   *templateContext;
175         SFSTree                 variables;
176         outFn                   printString;
177         urlEscapeFn             urlEscape;
178         htmlEscapeFn    htmlEscape;
179 } TemplateData;
180
181 typedef struct TemplateData *Template;
182
183 int parseTemplateFile(Template tmpl, char* filename );  /* return non-zero if error */
184 int initTemplate( Template tmpl, MemoryContext *mc, char *basedir, char *filename );
185 void freeTemplate( Template tmpl );
186 void resetTemplate( Template tmpl );
187 int printTemplate( Template tmpl );
188
189 #define TVAR_OK                 (0)
190 #define TVAR_NOTFOUND   (1)
191 #define TVAR_FORBIDDEN  (2)
192 #define TVAR_NOROW              (3)
193 #define TVAR_LOOPMARK   (4)
194
195 int setTemplateValueInt( Template tmpl, char * key, int val );
196 int setTemplateValueString( Template tmpl, char * key, char * val );
197 int setTemplateValueTime( Template tmpl, char * key, time_t val );
198 int setTemplateValueBool( Template tmpl, char * key, int val );
199 int setTemplateValueUndefined( Template tmpl, char * key );
200 int setTemplateValueDouble( Template tmpl, char * key, double val );
201 int addTemplateRow( Template tmpl, char * key );
202
203 void dumpTemplate( Template tmpl );
204 #endif