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(Template 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;
80 executeFunctionDescData funcs[] = {
81 {"callcounter", 0, localCounter},
87 opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
90 while((i=getopt(argn,argv,"ht:")) != EOF) {
93 name = tstrdup(optarg);
105 setlocale(LC_ALL,"ru_RU.UTF-8");
106 base = allocMemoryContext(NULL, MC_DEBUG);
108 printf("initTemplate: %d\n", initTemplate(&template, base, funcs, ".", name) );
109 /* dumpTemplate(&template); */
111 setTemplateValueInt(&template, "ID", 17);
112 setTemplateValueUndefined(&template, "emptyID");
113 setTemplateValueInt(&template, "zeroid", 0);
114 setTemplateValueString(&template, "str", "QWERTY");
116 addTemplateRow(&template, "outerLoop");
117 setTemplateValueString(&template, "outerLoop.data1", "ha1");
118 setTemplateValueUndefined(&template, "outerLoop.data2");
120 addTemplateRow(&template, "outerLoop");
121 setTemplateValueInt(&template, "outerLoop.data1", 10);
122 setTemplateValueString(&template, "outerLoop.data2", "WOW");
123 addTemplateRow(&template, "outerLoop.innerLoop");
124 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 1");
125 addTemplateRow(&template, "outerLoop.innerLoop");
126 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 2");
128 addTemplateRow(&template, "outerLoop");
129 setTemplateValueString(&template, "outerLoop.data1", "ha3");
131 template.printString = outfunc;
132 printTemplate( &template );
134 resetTemplate(&template);
136 setTemplateValueInt(&template, "ID", 23);
137 setTemplateValueUndefined(&template, "emptyID");
138 setTemplateValueInt(&template, "zeroid", 0);
139 addTemplateRow(&template, "outerLoop");
140 setTemplateValueString(&template, "outerLoop.data1", "ha1");
141 setTemplateValueInt(&template, "outerLoop.data1", 1234);
142 setTemplateValueString(&template, "outerLoop.data2", "FOO");
143 addTemplateRow(&template, "outerLoop.innerLoop");
144 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Again 1");
147 addTemplateRow(&template,"selfLoop");
148 setTemplateValueInt(&template, "selfLoop.CNT", ++cnt);
149 setTemplateValueString(&template, "selfLoop.NODE", "outer");
152 addTemplateRow(&template,"selfLoop.oneloop");
153 setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
154 setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop1");
157 addTemplateRow(&template,"selfLoop.oneloop");
158 setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
159 setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop2");
162 addTemplateNestedLoop(&template, "selfLoop");
165 addTemplateRow(&template,"selfLoop");
166 setTemplateValueInt(&template, "selfLoop.CNT", ++cnt);
167 setTemplateValueString(&template, "selfLoop.NODE", "inner");
168 if ( i==1 && j==0 ) {
169 addTemplateRow(&template,"selfLoop.oneloop");
170 setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
171 setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop3");
174 addTemplateNestedLoop(&template, "selfLoop");
176 addTemplateRow(&template,"selfLoop");
177 setTemplateValueInt(&template, "selfLoop.CNT", ++cnt);
178 setTemplateValueString(&template, "selfLoop.NODE", "innerst");
180 addTemplateRow(&template,"selfLoop.oneloop");
181 setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
182 setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop4");
185 returnTemplateNestedLoop(&template, "selfLoop");
190 returnTemplateNestedLoop(&template, "selfLoop");
193 fputs("================================================\n", stdout);
194 printTemplate( &template );
197 resetTemplate(&template);
198 freeTemplate(&template);