From: konrad Date: Sun, 7 Dec 2008 13:20:00 +0000 (+0000) Subject: introduce conditions into odf X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=1743a10fffaf62c8a5cbade44b58d8d866061b93;p=web%2Fkonrad%2Fsmoke.git introduce conditions into odf git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@207 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/doc/prog_odttemplate.html b/doc/prog_odttemplate.html index b025cfb..7291d71 100644 --- a/doc/prog_odttemplate.html +++ b/doc/prog_odttemplate.html @@ -51,6 +51,37 @@ Loop variable names are enclosed in "@", and contain the loop name and the varia Loops cannot be nested. +

Conditional Output

+ +The output of parsed lines can be suppressed: + +
+#IF:MYVAR < 66
+output if the value is smaller than 66
+#ELSE
+output otherwise
+#ENDIF
+
+ +A condition consists of exactly two values (both may be variables) and an operator. More complex expressions are not possible at the moment. If the condition evaluates to false, then the output and calculation of all lines until #ELSE or #ENDIF is suppressed.

+ +At the moment the #ELSE statement reverses the output suppression between #IF and #ENDIF. Multiple #ELSEs are possible at the moment, but should not be relied upon in case the logic changes in subsequent versions.

+ +Conditions cannot be nested, but conditions can be embedded in loops or be put around loops. If a condition is put around a loop, it does not prevent the processor from evaluating the loop, only the output and internal calculations are suppressed.

+ + + + + + + + + + + +
OperatorDescription
<lighter than
<=lighter or equal
>greater than
>=greater or equal
<>not equal
!=not equal
=equal
==equal
+ +

Calculations

Numeric variables allow some very simple calculations in place: @@ -67,13 +98,15 @@ For integer variables this works on the plain integer value. For money values it More complex calculations can be done with the #CALC statement:
-#CALC:TVAR/MONEY:@VAR1@ * @VAR2@ + 4
+#CALC:TVAR/MONEY:VAR1 * VAR2 + 4
 
-The above creates the temporary variable "@#TVAR@" that is the product of @VAR1@ and @VAR2@'s content plus 4. It is marked as a monetary value.

+The above creates the temporary variable "@#TVAR@" that is the product of @VAR1@ and @VAR2@'s content plus 4. It is marked as a monetary value. Please note that the variables are not enclosed in "@".

Calculations are restricted to the operators below and are always done from left to right - more complex formulas require several lines. Tokens must be space separated. If no type or a non-existing type is requested INT is assumed. All calculations are done using 64bit signed integers. If the processor comes across an illegal token it sets the variable to "error".

+Literals (above: "4") must be integers.

+ @@ -122,13 +155,16 @@ Per default all variables are localized before they get inserted into the docume File name: eventsummary.odtt

-Loop: TICKETS - this loop iterates over the ticket price categories.

+Loops:
+TICKETS - this loop iterates over the ticket price categories.
+ORDERS - this loop iterates over all orders that contain tickets for this event.

Variables:

OperatorDescription
+adding
+ @@ -170,9 +206,10 @@ File name: bill.odtt

Loops:
TICKETS - this loop iterates over each ticket in the order.
-ADDRESSLINES - this loop iterates over the lines of the address.

+ACCTICKETS - this loop iterates over ticket categories in the order. Each category contains tickets for the same event with the same price.
+ADDRESSLINES - this loop iterates over the lines of the address. (obsolete)
+VOUCHERS - this loop iterates over all vouchers of the order.

-(TODO: loop for vouchers)

Variables:

VariableDescription
@TITLE@title of the event
@ARTIST@artist of the event
@ROOM@room of the event
@START@start date and time in the currently active locale
@CAPACITY@maximum amount of tickets that can be sold
@RESERVED@tickets that are currently reserved for a seller
@@ -218,8 +255,8 @@ Variables: - - + + diff --git a/src/eventsummary.cpp b/src/eventsummary.cpp index 9f524d9..1b7b166 100644 --- a/src/eventsummary.cpp +++ b/src/eventsummary.cpp @@ -213,6 +213,9 @@ void MEventSummary::getVariable(QString varname,MOdtRenderer::VarType&av,QVarian if(varname=="ARTIST") value=event.artist(); else + if(varname=="ROOM") + value=event.room(); + else if(varname=="START"){ value=event.startTime(); av=MOdtRenderer::DateTimeVar; diff --git a/src/odtrender.cpp b/src/odtrender.cpp index f10961d..a27cd0c 100644 --- a/src/odtrender.cpp +++ b/src/odtrender.cpp @@ -58,6 +58,7 @@ class MOdtRendererPrivate QUnZip temp; QFile tfile; QString newline; + bool inif,iftrue; struct LocalVar{ MOdtRenderer::VarType type; @@ -77,6 +78,7 @@ MOdtRendererPrivate::MOdtRendererPrivate(QString file,MOdtRenderer*p) { parent=p; newline=" "; + inif=iftrue=true; //open ZIP if(tfile.open(QIODevice::ReadOnly))temp.open(&tfile); //TODO: make sure this is a valid ZIP file, preferably with some ODT content @@ -200,7 +202,6 @@ QString MOdtRendererPrivate::render(QString s) //...line by line for(int k=0;k")iftrue=op1>op2;else + if(stl[1]=="=" || stl[1]=="=")iftrue=op1==op2;else + if(stl[1]=="<=")iftrue=op1<=op2;else + if(stl[1]==">=")iftrue=op1>=op2;else + if(stl[1]=="<>" || stl[1]=="!=")iftrue=op1!=op2; + else { + qDebug("??????????IfError: unknown operator"); + return ""; + } + return ""; + }else + if(line.trimmed()=="#ELSE"){ + iftrue=!iftrue; + return ""; + }else + if(line.trimmed()=="#ENDIF"){ + inif=false; + return ""; + } + //check if state + if(inif && !iftrue)return ""; + //check for other statements if(line.trimmed().startsWith("#SETNEWLINE:")){ //set a new newline translation newline=line.trimmed().mid(12).trimmed(); if(newline=="")newline=" "; return ""; - } + }else if(line.trimmed().startsWith("#CALC:")){ //do a calculation //get full statement @@ -252,22 +289,34 @@ QString MOdtRendererPrivate::renderLine(QString line,QString loop,int lpos) QStringList stl=stmt.mid(p+1).trimmed().split(" "); //go through qint64 res=intToken(stl[0],loop,lpos); - qDebug("??????????CalcInit: %lli",res); +// qDebug("??????????CalcInit: %lli",res); for(int i=1;i=stl.size()){ setLocalVarError(var); - qDebug("??????????CalcError: missing operand"); + qDebug("??????????CalcError: missing last operand"); return ""; } - qDebug("??????????Calc: operator '%s' operand '%s' -> '%lli'",stl[i].toAscii().data(),stl[i+1].toAscii().data(),intToken(stl[i+1],loop,lpos)); - if(stl[i]=="+")res+=intToken(stl[i+1],loop,lpos);else - if(stl[i]=="-")res-=intToken(stl[i+1],loop,lpos);else - if(stl[i]=="*")res*=intToken(stl[i+1],loop,lpos);else - if(stl[i]=="/")res/=intToken(stl[i+1],loop,lpos);else - if(stl[i]=="%")res%=intToken(stl[i+1],loop,lpos); - else { + qint64 op2=intToken(stl[i+1],loop,lpos); +// qDebug("??????????Calc: operator '%s' operand '%s' -> '%lli'",stl[i].toAscii().data(),stl[i+1].toAscii().data(),op2); + if(stl[i]=="+")res+=op2;else + if(stl[i]=="-")res-=op2;else + if(stl[i]=="*")res*=op2;else + if(stl[i]=="/"){ + if(op2!=0)res/=op2; + else{ + qDebug("??????????CalcError: division operand %s is zero",stl[i+1].toAscii().data()); + return ""; + } + }else + if(stl[i]=="%"){ + if(op2!=0)res%=op2; + else{ + qDebug("??????????CalcError: modulo operand %s is zero",stl[i+1].toAscii().data()); + return ""; + } + }else { setLocalVarError(var); - qDebug("??????????CalcError: unknown operator"); + qDebug("??????????CalcError: unknown operator %s",stl[i].toAscii().data()); return ""; } } @@ -332,7 +381,7 @@ QString MOdtRendererPrivate::renderLine(QString line,QString loop,int lpos) ret+="@"+vname; } //return transformed line - return ret; + return ret + "\n"; } static inline QString formatVar(QVariant r,MOdtRenderer::VarType tp,bool loc,int offset) @@ -428,6 +477,10 @@ QString MOdtRendererPrivate::getLoopVariable(QString loopname,int iteration,QStr qint64 MOdtRendererPrivate::intToken(QString vname,QString loop,int lpos) { + //check for literals + bool islit; + qint64 lit=vname.toLongLong(&islit); + if(islit)return lit; //split the variable QStringList vnl=vname.split(":"); if(vnl.size()<2){
@ACCTICKETS:ENDTIME@the end date and time of the event for this ticket type
@ACCTICKETS:ROOM@the room/place of the event for this ticket type
@ADDRESSLINES@amount of lines that the address has
@ADDRESSLINES:LINE@current line in the address loop
@ADDRESSLINES@amount of lines that the address has (obsolete)
@ADDRESSLINES:LINE@current line in the address loop (obsolete)
@VOUCHERS@the amount of vouchers in the order
@VOUCHERS:PRICE@the price of this voucher