/* * Copyright (c) 2008 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include "tmalloc.h" #include "tlog.h" #include "tools.h" #include "template.h" static void usage() { puts( "Usage:\n" "tmpltest [-t TEMPLATENAME]\n" ); exit(1); } static void outfunc(char *str, int len) { fputs( str, stdout ); } static int counter = 0; static VariableValue localCounter(Template tmpl, int nargs, VariableValue *args) { VariableValue out = tmalloc(sizeof(VariableValue)); out->type = valueInt; out->flags = TND_DEFINED; out->value.intValue = ++counter; return out; } extern char *optarg; extern int opterr; int main(int argn, char *argv[]) { MemoryContext *base; char *name = NULL; TemplateData template; int i; executeFunctionDescData funcs[] = { {"callcounter", 0, localCounter}, {NULL,0,NULL} }; opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL); opterr=0; while((i=getopt(argn,argv,"ht:")) != EOF) { switch(i) { case 't': name = tstrdup(optarg); break; case 'h': default: usage(); break; } } if (!name) usage(); setlocale(LC_ALL,"ru_RU.UTF-8"); base = allocMemoryContext(NULL, MC_DEBUG); printf("initTemplate: %d\n", initTemplate(&template, base, funcs, ".", name) ); /* dumpTemplate(&template); */ setTemplateValueInt(&template, "ID", 17); setTemplateValueUndefined(&template, "emptyID"); setTemplateValueInt(&template, "zeroid", 0); setTemplateValueString(&template, "str", "QWERTY"); addTemplateRow(&template, "outerLoop"); setTemplateValueString(&template, "outerLoop.data1", "ha1"); setTemplateValueUndefined(&template, "outerLoop.data2"); addTemplateRow(&template, "outerLoop"); setTemplateValueInt(&template, "outerLoop.data1", 10); setTemplateValueString(&template, "outerLoop.data2", "WOW"); addTemplateRow(&template, "outerLoop.innerLoop"); setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 1"); addTemplateRow(&template, "outerLoop.innerLoop"); setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 2"); addTemplateRow(&template, "outerLoop"); setTemplateValueString(&template, "outerLoop.data1", "ha3"); template.printString = outfunc; printTemplate( &template ); resetTemplate(&template); setTemplateValueInt(&template, "ID", 23); setTemplateValueUndefined(&template, "emptyID"); setTemplateValueInt(&template, "zeroid", 0); addTemplateRow(&template, "outerLoop"); setTemplateValueString(&template, "outerLoop.data1", "ha1"); setTemplateValueInt(&template, "outerLoop.data1", 1234); setTemplateValueString(&template, "outerLoop.data2", "FOO"); addTemplateRow(&template, "outerLoop.innerLoop"); setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Again 1"); fputs("================================================\n", stdout); printTemplate( &template ); resetTemplate(&template); freeTemplate(&template); return 0; }