a605f0be7f0cc3cdfa61a5ad1b01bfae0fc32436
[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(Template 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         int                     i,j,k;
79         int                             cnt=0;
80         executeFunctionDescData funcs[] = {
81                 {"callcounter", 0, localCounter},
82                 {NULL,0,NULL}
83         };
84
85
86
87         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
88         opterr=0;
89
90         while((i=getopt(argn,argv,"ht:")) != EOF) {
91                 switch(i) {
92                         case 't':
93                                 name = tstrdup(optarg);
94                                 break;
95                         case 'h':
96                         default:
97                                 usage();
98                                 break;
99                 }
100         }
101                         
102         if (!name)
103                 usage();
104
105         setlocale(LC_ALL,"ru_RU.UTF-8");
106         base = allocMemoryContext(NULL, MC_DEBUG);
107
108         printf("initTemplate: %d\n",  initTemplate(&template, base, funcs, ".", name) );
109         /* dumpTemplate(&template); */
110
111         setTemplateValueInt(&template, "ID", 17);
112         setTemplateValueUndefined(&template, "emptyID");
113         setTemplateValueInt(&template, "zeroid", 0);
114         setTemplateValueString(&template, "str", "QWERTY");
115
116         addTemplateRow(&template, "outerLoop");
117         setTemplateValueString(&template, "outerLoop.data1", "ha1");
118         setTemplateValueUndefined(&template, "outerLoop.data2");
119
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");
127
128         addTemplateRow(&template, "outerLoop");
129         setTemplateValueString(&template, "outerLoop.data1", "ha3");
130
131         template.printString = outfunc;
132         printTemplate( &template );
133
134         resetTemplate(&template);
135
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");
145
146         for(i=0;i<3;i++) {
147                 addTemplateRow(&template,"selfLoop");
148                 setTemplateValueInt(&template, "selfLoop.CNT", ++cnt);
149                 setTemplateValueString(&template, "selfLoop.NODE", "outer");
150
151                 if (i<1) {
152                         addTemplateRow(&template,"selfLoop.oneloop");
153                         setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
154                         setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop1");
155                 }
156                 if (i<2) {
157                         addTemplateRow(&template,"selfLoop.oneloop");
158                         setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
159                         setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop2");
160                 }
161
162                 addTemplateNestedLoop(&template, "selfLoop");
163         
164                 for(j=0;j<2;j++) {
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");
172                         }
173                         if (i==2 && j==1) {
174                                 addTemplateNestedLoop(&template, "selfLoop");
175                                 for(k=0;k<4;k++) {
176                                         addTemplateRow(&template,"selfLoop");
177                                         setTemplateValueInt(&template, "selfLoop.CNT", ++cnt);
178                                         setTemplateValueString(&template, "selfLoop.NODE", "innerst");
179                                         if ( k==2 ) {
180                                                 addTemplateRow(&template,"selfLoop.oneloop");
181                                                 setTemplateValueInt(&template, "selfLoop.oneloop.CNT", ++cnt);
182                                                 setTemplateValueString(&template, "selfLoop.oneloop.NODE", "subloop4");
183                                         }
184                                 }
185                                 returnTemplateNestedLoop(&template, "selfLoop");
186                         }
187                 }
188         
189
190                 returnTemplateNestedLoop(&template, "selfLoop");
191         }
192
193         fputs("================================================\n", stdout);
194         printTemplate( &template );
195
196
197         resetTemplate(&template);
198         freeTemplate(&template);
199
200         return 0;
201 }
202
203         
204