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.
46 "tmpltest [-t TEMPLATENAME]\n"
53 outfunc(char *str, int len) {
57 static int counter = 0;
60 localCounter(TemplateInstance tmpl, int nargs, VariableValue *args) {
61 VariableValue out = tmalloc(sizeof(VariableValue));
64 out->flags = TND_DEFINED;
65 out->value.intValue = ++counter;
74 main(int argn, char *argv[]) {
77 TemplateData template;
78 TemplateInstance tinstance;
81 executeFunctionDescData funcs[] = {
82 {"callcounter", 0, localCounter},
88 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
91 while((i=getopt(argn,argv,"ht:")) != EOF) {
94 name = tstrdup(optarg);
106 setlocale(LC_ALL,"ru_RU.UTF-8");
107 base = allocMemoryContext(NULL, MC_DEBUG);
109 printf("initTemplate: %d\n", initTemplate(&template, base, funcs, ".", name) );
110 /* dumpTemplate(&template); */
112 tinstance = newTemplateInstance(&template, NULL);
114 setTemplateValueInt(tinstance, "ID", 17);
115 setTemplateValueUndefined(tinstance, "emptyID");
116 setTemplateValueInt(tinstance, "zeroid", 0);
117 setTemplateValueString(tinstance, "str", "QWERTY");
119 addTemplateRow(tinstance, "outerLoop");
120 setTemplateValueString(tinstance, "outerLoop.data1", "ha1");
121 setTemplateValueUndefined(tinstance, "outerLoop.data2");
123 addTemplateRow(tinstance, "outerLoop");
124 setTemplateValueInt(tinstance, "outerLoop.data1", 10);
125 setTemplateValueString(tinstance, "outerLoop.data2", "WOW");
126 addTemplateRow(tinstance, "outerLoop.innerLoop");
127 setTemplateValueString(tinstance, "outerLoop.innerLoop.camenty", "Number 1");
128 addTemplateRow(tinstance, "outerLoop.innerLoop");
129 setTemplateValueString(tinstance, "outerLoop.innerLoop.camenty", "Number 2");
131 addTemplateRow(tinstance, "outerLoop");
132 setTemplateValueString(tinstance, "outerLoop.data1", "ha3");
134 template.printString = outfunc;
135 printTemplate( tinstance );
137 tinstance = newTemplateInstance(&template, NULL);
139 setTemplateValueInt(tinstance, "ID", 23);
140 setTemplateValueUndefined(tinstance, "emptyID");
141 setTemplateValueInt(tinstance, "zeroid", 0);
142 addTemplateRow(tinstance, "outerLoop");
143 setTemplateValueString(tinstance, "outerLoop.data1", "ha1");
144 setTemplateValueInt(tinstance, "outerLoop.data1", 1234);
145 setTemplateValueString(tinstance, "outerLoop.data2", "FOO");
146 addTemplateRow(tinstance, "outerLoop.innerLoop");
147 setTemplateValueString(tinstance, "outerLoop.innerLoop.camenty", "Again 1");
150 addTemplateRow(tinstance,"selfLoop");
151 setTemplateValueInt(tinstance, "selfLoop.CNT", ++cnt);
152 setTemplateValueString(tinstance, "selfLoop.NODE", "outer");
155 addTemplateRow(tinstance,"selfLoop.oneloop");
156 setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
157 setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop1");
160 addTemplateRow(tinstance,"selfLoop.oneloop");
161 setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
162 setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop2");
165 addTemplateNestedLoop(tinstance, "selfLoop");
168 addTemplateRow(tinstance,"selfLoop");
169 setTemplateValueInt(tinstance, "selfLoop.CNT", ++cnt);
170 setTemplateValueString(tinstance, "selfLoop.NODE", "inner");
171 if ( i==1 && j==0 ) {
172 addTemplateRow(tinstance,"selfLoop.oneloop");
173 setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
174 setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop3");
177 addTemplateNestedLoop(tinstance, "selfLoop");
179 addTemplateRow(tinstance,"selfLoop");
180 setTemplateValueInt(tinstance, "selfLoop.CNT", ++cnt);
181 setTemplateValueString(tinstance, "selfLoop.NODE", "innerst");
183 addTemplateRow(tinstance,"selfLoop.oneloop");
184 setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
185 setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop4");
188 returnTemplateNestedLoop(tinstance, "selfLoop");
193 returnTemplateNestedLoop(tinstance, "selfLoop");
196 fputs("================================================\n", stdout);
197 printTemplate( tinstance );
200 freeTemplate(&template);