Add INFGetBollean method
[tedtools.git] / tmpltest.c
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 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <locale.h>
35
36
37 #include "tmalloc.h"
38 #include "tlog.h"
39 #include "tools.h"
40 #include "template.h"
41
42 static void
43 usage() {
44         puts(
45         "Usage:\n"
46         "tmpltest [-t TEMPLATENAME]\n"
47         );
48         exit(1);
49 }
50
51
52 static void
53 outfunc(char *str, int len) {
54         fputs( str, stdout ); 
55 }
56
57 static int counter = 0;
58
59 static VariableValue 
60 localCounter(TemplateInstance tmpl, int nargs, VariableValue *args) {
61         VariableValue out = tmalloc(sizeof(VariableValue));
62
63         out->type = valueInt;
64         out->flags = TND_DEFINED;
65         out->value.intValue = ++counter;
66
67         return out;
68 }
69
70 extern char *optarg;
71 extern int opterr;
72
73 int
74 main(int argn, char *argv[]) {
75         MemoryContext   *base;
76         char                    *name = NULL;
77         TemplateData    template;
78         TemplateInstance tinstance;
79         int                     i,j,k;
80         int                             cnt=0;
81         executeFunctionDescData funcs[] = {
82                 {"callcounter", 0, localCounter},
83                 {NULL,0,NULL}
84         };
85
86
87
88         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
89         opterr=0;
90
91         while((i=getopt(argn,argv,"ht:")) != EOF) {
92                 switch(i) {
93                         case 't':
94                                 name = tstrdup(optarg);
95                                 break;
96                         case 'h':
97                         default:
98                                 usage();
99                                 break;
100                 }
101         }
102                         
103         if (!name)
104                 usage();
105
106         setlocale(LC_ALL,"ru_RU.UTF-8");
107         base = allocMemoryContext(NULL, MC_DEBUG);
108
109         printf("initTemplate: %d\n",  initTemplate(&template, base, funcs, ".", name) );
110         /* dumpTemplate(&template); */
111
112         tinstance = newTemplateInstance(&template, NULL);
113
114         setTemplateValueInt(tinstance, "ID", 17);
115         setTemplateValueUndefined(tinstance, "emptyID");
116         setTemplateValueInt(tinstance, "zeroid", 0);
117         setTemplateValueString(tinstance, "str", "QWERTY");
118
119         addTemplateRow(tinstance, "outerLoop");
120         setTemplateValueString(tinstance, "outerLoop.data1", "ha1");
121         setTemplateValueUndefined(tinstance, "outerLoop.data2");
122
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");
130
131         addTemplateRow(tinstance, "outerLoop");
132         setTemplateValueString(tinstance, "outerLoop.data1", "ha3");
133
134         template.printString = outfunc;
135         printTemplate( tinstance );
136
137         tinstance = newTemplateInstance(&template, NULL);
138
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");
148
149         for(i=0;i<3;i++) {
150                 addTemplateRow(tinstance,"selfLoop");
151                 setTemplateValueInt(tinstance, "selfLoop.CNT", ++cnt);
152                 setTemplateValueString(tinstance, "selfLoop.NODE", "outer");
153
154                 if (i<1) {
155                         addTemplateRow(tinstance,"selfLoop.oneloop");
156                         setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
157                         setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop1");
158                 }
159                 if (i<2) {
160                         addTemplateRow(tinstance,"selfLoop.oneloop");
161                         setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
162                         setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop2");
163                 }
164
165                 addTemplateNestedLoop(tinstance, "selfLoop");
166         
167                 for(j=0;j<2;j++) {
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");
175                         }
176                         if (i==2 && j==1) {
177                                 addTemplateNestedLoop(tinstance, "selfLoop");
178                                 for(k=0;k<4;k++) {
179                                         addTemplateRow(tinstance,"selfLoop");
180                                         setTemplateValueInt(tinstance, "selfLoop.CNT", ++cnt);
181                                         setTemplateValueString(tinstance, "selfLoop.NODE", "innerst");
182                                         if ( k==2 ) {
183                                                 addTemplateRow(tinstance,"selfLoop.oneloop");
184                                                 setTemplateValueInt(tinstance, "selfLoop.oneloop.CNT", ++cnt);
185                                                 setTemplateValueString(tinstance, "selfLoop.oneloop.NODE", "subloop4");
186                                         }
187                                 }
188                                 returnTemplateNestedLoop(tinstance, "selfLoop");
189                         }
190                 }
191         
192
193                 returnTemplateNestedLoop(tinstance, "selfLoop");
194         }
195
196         fputs("================================================\n", stdout);
197         printTemplate( tinstance );
198
199
200         freeTemplate(&template);
201
202         return 0;
203 }
204
205         
206