c5aef19c4adc4f34f196d94a4b55c857a9ab94fb
[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;
79         executeFunctionDescData funcs[] = {
80                 {"callcounter", 0, localCounter},
81                 {NULL,0,NULL}
82         };
83
84
85
86         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
87         opterr=0;
88
89         while((i=getopt(argn,argv,"ht:")) != EOF) {
90                 switch(i) {
91                         case 't':
92                                 name = tstrdup(optarg);
93                                 break;
94                         case 'h':
95                         default:
96                                 usage();
97                                 break;
98                 }
99         }
100                         
101         if (!name)
102                 usage();
103
104         setlocale(LC_ALL,"ru_RU.UTF-8");
105         base = allocMemoryContext(NULL, MC_DEBUG);
106
107         printf("initTemplate: %d\n",  initTemplate(&template, base, funcs, ".", name) );
108         /* dumpTemplate(&template); */
109
110         setTemplateValueInt(&template, "ID", 17);
111         setTemplateValueUndefined(&template, "emptyID");
112         setTemplateValueInt(&template, "zeroid", 0);
113         setTemplateValueString(&template, "str", "QWERTY");
114
115         addTemplateRow(&template, "outerLoop");
116         setTemplateValueString(&template, "outerLoop.data1", "ha1");
117         setTemplateValueUndefined(&template, "outerLoop.data2");
118
119         addTemplateRow(&template, "outerLoop");
120         setTemplateValueInt(&template, "outerLoop.data1", 10);
121         setTemplateValueString(&template, "outerLoop.data2", "WOW");
122                 addTemplateRow(&template, "outerLoop.innerLoop");
123                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 1");
124                 addTemplateRow(&template, "outerLoop.innerLoop");
125                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 2");
126
127         addTemplateRow(&template, "outerLoop");
128         setTemplateValueString(&template, "outerLoop.data1", "ha3");
129
130         template.printString = outfunc;
131         printTemplate( &template );
132
133         resetTemplate(&template);
134
135         setTemplateValueInt(&template, "ID", 23);
136         setTemplateValueUndefined(&template, "emptyID");
137         setTemplateValueInt(&template, "zeroid", 0);
138         addTemplateRow(&template, "outerLoop");
139         setTemplateValueString(&template, "outerLoop.data1", "ha1");
140         setTemplateValueInt(&template, "outerLoop.data1", 1234);
141         setTemplateValueString(&template, "outerLoop.data2", "FOO");
142         addTemplateRow(&template, "outerLoop.innerLoop");
143                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Again 1");
144
145         fputs("================================================\n", stdout);
146         printTemplate( &template );
147
148         resetTemplate(&template);
149         freeTemplate(&template);
150
151         return 0;
152 }
153
154         
155