X-Git-Url: http://sigaev.ru/git/gitweb.cgi?p=tedtools.git;a=blobdiff_plain;f=template.h;h=97e9070c0413ec535629741868843017b250ade7;hp=7953de45dfe70fcff72275a5e0e3524ec8be0381;hb=HEAD;hpb=60b6a26884b77fe6c6c37e714aa7a0c1b30e9fc7 diff --git a/template.h b/template.h index 7953de4..97e9070 100644 --- a/template.h +++ b/template.h @@ -33,11 +33,12 @@ ****************************************************************************** * SYNTAX * ****************************************************************************** - * <% EXPRESSION , "FORMAT"] [# "DEFAULTVALUE"] [|(h|u)]%> + * <% EXPRESSION [, "FORMAT"] [# "DEFAULTVALUE"] [|(h|u)]%> * - format value should be as in strftime for time value and printf * for all other. Currently, bool values have only "true"/"false" * string values * <@IF EXPRESSION @> + * [<@ ELSE IF EXPRESSION | ELSIF EXPRESSION @>] * [ <@ELSE@> ] * <@ENDIF@> * @@ -69,6 +70,7 @@ * __FIRST - true for first iteration * __LAST - true for last iteration * __COUNTER - iteration's number + * __LEVEL - level of nested loop (root == 0) * __SIZE - number of iterations * __ODD - true for odd iteraion * __EVEN - true for even iteraion @@ -116,9 +118,11 @@ * in library. So, library uses mixed plain malloc and memory context * concepts (tmalloc.h). * To work with library it's needed to allocate persistent memory context - * for initTemplate() call and provide temporary memory context for usual work. - * after printTemplate is called it's needed to call resetTemplate() and - * then resetMemoryContext() for second context. + * for initTemplate(). + * To work with template it's needed to get template's instance by + * newTemplateInstance() call with separated memory context. In any time + * that memory context can be reseted or freeed and it will be enough + * to free template's instance. * ******************************************************************************/ @@ -143,6 +147,7 @@ typedef enum TemplateNodeType { PrintNode, ConstNode, NestNode, + LoopData, /* value's types of variables */ valueInt = 200, /* smallest of any values type */ @@ -163,13 +168,16 @@ typedef enum TemplateNodeType { #define TND___SIZE (0x0040) #define TND___ODD (0x0080) #define TND___EVEN (0x0100) +#define TND___LEVEL (0x0200) -#define TND_DEFINED (0x0200) +#define TND_DEFINED (0x0400) -#define TND__SPECIALMASK (TND___FIRST | TND___LAST | TND___COUNTER | TND___SIZE | TND___ODD | TND___EVEN) +#define TND__SPECIALMASK (TND___FIRST | TND___LAST | TND___COUNTER | TND___SIZE | TND___ODD | TND___EVEN | TND___LEVEL) typedef struct TemplateData *Template; +typedef struct TemplateInstanceData *TemplateInstance; + typedef struct TemplateNodeData *TemplateNode; typedef struct VariableValueData * VariableValue; @@ -177,7 +185,7 @@ typedef struct VariableValueData { TemplateNodeType type; /* should be first, see resetTemplate/freeTemplate */ int flags; union { - int intValue; + int64_t intValue; char *stringValue; time_t timeValue; int boolValue; @@ -192,7 +200,7 @@ typedef struct executeFunctionDescData *executeFunctionDesc; typedef struct executeFunctionDescData { char *name; int nargs; /* -1 - variable number */ - VariableValue (*execFn)(Template, int, VariableValue*); + VariableValue (*execFn)(TemplateInstance, int, VariableValue*); } executeFunctionDescData; typedef struct LoopInstanceData * LoopInstance; @@ -202,6 +210,7 @@ typedef struct LoopRowData *LoopRow; typedef struct LoopRowData { TemplateNode loop; LoopInstance nestedInstance; + LoopRow nextCell; VariableValueData varvals[1]; } LoopRowData; #define LRDHDRSZ (offsetof(LoopRowData, varvals)) @@ -209,9 +218,26 @@ typedef struct LoopRowData { typedef struct LoopInstanceData { int nrow; LoopInstance upperInstance; - GList *rowValues; /*list of LoopRow */ + + LoopInstance nextCell; + LoopRow headList; + LoopRow tailList; + /* GList *rowValues; */ /*list of LoopRow */ } LoopInstanceData; +typedef struct LoopTemplateInstanceData *LoopTemplateInstance; +typedef struct LoopTemplateInstanceData { + TemplateNodeType type; /* LoopData */ + TemplateNode loopNode; + LoopRow lastRow; + LoopInstance currentInstance; + LoopInstance headList; + LoopInstance tailList; + /*GList *listInstance;*/ +} LoopTemplateInstanceData; + +typedef long Offset; + typedef struct TemplateNodeData { TemplateNodeType type; /* should be first, see resetTemplate/freeTemplate */ @@ -226,7 +252,8 @@ typedef struct TemplateNodeData { struct { char *varName; int varNameLength; - VariableValue value; + Offset varValueOffset; + /* VariableValue value; */ int flags; } variable; @@ -268,16 +295,7 @@ typedef struct TemplateNodeData { TemplateNode selfNode; /* pointer to self-nested plase to insert */ GList *childrenLoop; /* to reset loop's instance */ GList *listVarValues; /* list of loop variables */ - GList *listInstance; - /* listInstace -+ - +->instance-+ - +->row - | - | - +->row->currentInstance - */ - LoopRow lastRow; - LoopInstance currentInstance; + Offset loopDataOffset; } loop; /* ConditionNode */ @@ -307,13 +325,22 @@ typedef struct TemplateData { urlEscapeFn urlEscape; htmlEscapeFn htmlEscape; executeFunctionDesc functions; + + Offset templateInstanceSize; + char *templateInstance; } TemplateData; +typedef struct TemplateInstanceData { + Template tmpl; + MemoryContext *templateContext; + char instanceData[1]; +} TemplateInstanceData; + int parseTemplateFile(Template tmpl, char* filename ); /* return non-zero if error */ int initTemplate( Template tmpl, MemoryContext *mc, executeFunctionDesc functions, char *basedir, char *filename ); void freeTemplate( Template tmpl ); -void resetTemplate( Template tmpl ); -int printTemplate( Template tmpl ); +TemplateInstance newTemplateInstance(Template tmpl, MemoryContext *mc); +int printTemplate( TemplateInstance tmpl ); #define TVAR_OK (0) #define TVAR_NOTFOUND (1) @@ -321,14 +348,14 @@ int printTemplate( Template tmpl ); #define TVAR_NOROW (3) #define TVAR_LOOPMARK (4) -int setTemplateValueInt( Template tmpl, char * key, int val ); -int setTemplateValueString( Template tmpl, char * key, char * val ); -int setTemplateValueTime( Template tmpl, char * key, time_t val ); -int setTemplateValueBool( Template tmpl, char * key, int val ); -int setTemplateValueUndefined( Template tmpl, char * key ); -int setTemplateValueDouble( Template tmpl, char * key, double val ); -int addTemplateRow( Template tmpl, char * key ); -int addTemplateNestedLoop( Template tmpl, char * key); -int returnTemplateNestedLoop( Template tmpl, char * key); +int setTemplateValueInt( TemplateInstance tmpl, char * key, int64_t val ); +int setTemplateValueString( TemplateInstance tmpl, char * key, char * val ); +int setTemplateValueTime( TemplateInstance tmpl, char * key, time_t val ); +int setTemplateValueBool( TemplateInstance tmpl, char * key, int val ); +int setTemplateValueUndefined( TemplateInstance tmpl, char * key ); +int setTemplateValueDouble( TemplateInstance tmpl, char * key, double val ); +int addTemplateRow( TemplateInstance tmpl, char * key ); +int addTemplateNestedLoop( TemplateInstance tmpl, char * key); +int returnTemplateNestedLoop( TemplateInstance tmpl, char * key); void dumpTemplate( Template tmpl ); #endif