Use c99 instead of ansi due to support int64 in template. Add __LEVEL built-in variab...
[tedtools.git] / template.c
index b20fb89..9e22f18 100644 (file)
@@ -81,7 +81,7 @@ isVariable(VariableValue value) {
        } else {
                switch (value->type) {
                        case valueInt:
-                               return value->value.intValue;
+                               return (value->value.intValue == 0) ? 0 : 1;
                        case valueString:
                                if ( value->value.stringValue == NULL || *value->value.stringValue == '\0' )
                                        return 0;
@@ -513,6 +513,9 @@ checkSpecialVariable(int flags, char *varName) {
        } else if ( strcmp(varName, "__size") == 0 ) {
                flags &= ~TND_GLOBAL; /* special vars cannot be global */
                flags |= TND___SIZE;
+       } else if ( strcmp(varName, "__level") == 0 ) {
+               flags &= ~TND_GLOBAL; /* special vars cannot be global */
+               flags |= TND___LEVEL;
        }
 
        return flags;
@@ -1013,7 +1016,7 @@ setTemplateValue( TemplateInstance tmpl, char *key) {
 
 
 int
-setTemplateValueInt( TemplateInstance tmpl, char * key, int val ) {
+setTemplateValueInt( TemplateInstance tmpl, char * key, int64_t val ) {
        storage.flags = TND_DEFINED;
        storage.type = valueInt;
        storage.value.intValue = val;
@@ -1085,7 +1088,7 @@ printVal( TemplateInstance tmpl, VariableValue value, int *len, char *format ) {
 
        switch (value->type) {
                case valueInt:
-                       printedlen = snprintf(NULL, 0, (format) ? format : "%d", value->value.intValue);
+                       printedlen = snprintf(NULL, 0, (format) ? format : "%lld", value->value.intValue);
                        break;
                case valueString:
                        if ( value->value.stringValue == NULL || *value->value.stringValue == '\0' )
@@ -1108,7 +1111,7 @@ printVal( TemplateInstance tmpl, VariableValue value, int *len, char *format ) {
 
        switch (value->type) {
                case valueInt:
-                       printedlen = snprintf(res, printedlen+1, (format) ? format : "%d", value->value.intValue);
+                       printedlen = snprintf(res, printedlen+1, (format) ? format : "%lld", value->value.intValue);
                        break;
                case valueString:
                        printedlen = snprintf(res, printedlen+1, (format) ? format : "%s", value->value.stringValue);
@@ -1179,7 +1182,7 @@ executeExpression( TemplateInstance tmpl, TemplateNode node ) {
 }
 
 static void
-printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance ) {
+printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance, int level ) {
        GListCell       *cell;
        VariableValue   value;
        int                             i;
@@ -1238,6 +1241,10 @@ printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance )
                                                        realValue->type = valueInt;
                                                        realValue->flags |= TND_DEFINED;
                                                        realValue->value.intValue = i+1;
+                                               } else if ( value->flags & TND___LEVEL ) {
+                                                       realValue->type = valueInt;
+                                                       realValue->flags |= TND_DEFINED;
+                                                       realValue->value.intValue = level;
                                                } else if ( value->flags & TND___SIZE ) {
                                                        realValue->type = valueInt;
                                                        realValue->flags |= TND_DEFINED;
@@ -1258,7 +1265,7 @@ printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance )
                                        if ( node->nodeData.loop.selfNode ) 
                                                node->nodeData.loop.selfNode->nodeData.nest.savedRowData = rowData;
 
-                                       printNode( tmpl, node->nodeData.loop.bodyNode, NULL );
+                                       printNode( tmpl, node->nodeData.loop.bodyNode, NULL, level );
                                }
                        }
                        break;
@@ -1287,7 +1294,7 @@ printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance )
                                        }
                                }
 
-                               printNode( tmpl, node->nodeData.nest.loop, savedRowData->nestedInstance );
+                               printNode( tmpl, node->nodeData.nest.loop, savedRowData->nestedInstance, level+1 );
 
                                /*
                                 * Restore saved datas
@@ -1317,13 +1324,13 @@ printNode( TemplateInstance tmpl, TemplateNode node, LoopInstance loopInstance )
                        value = executeExpression( tmpl, node->nodeData.condition.expressionNode );
 
                        if ( isVariable(value) ) 
-                               printNode( tmpl, node->nodeData.condition.ifNode, loopInstance );
+                               printNode( tmpl, node->nodeData.condition.ifNode, loopInstance, level );
                        else
-                               printNode( tmpl, node->nodeData.condition.elseNode, loopInstance );
+                               printNode( tmpl, node->nodeData.condition.elseNode, loopInstance, level );
                        break;
                case    CollectionNode:
                        GListForeach( cell, node->nodeData.children ) 
-                               printNode( tmpl, (TemplateNode)GLCELL_DATA(cell), loopInstance );
+                               printNode( tmpl, (TemplateNode)GLCELL_DATA(cell), loopInstance, level );
                        break;
                case    PrintNode:
                        value = executeExpression( tmpl, node->nodeData.condition.expressionNode ); 
@@ -1368,7 +1375,7 @@ printTemplate( TemplateInstance tmpl ) {
        if (!tmpl->tmpl->printString)
                return 1;
 
-       printNode(tmpl, tmpl->tmpl->tree, NULL);
+       printNode(tmpl, tmpl->tmpl->tree, NULL, 0);
 
        return 0;
 }