/** @file @author Stefan Frings */ #include "template.h" #include using namespace stefanfrings; Template::Template(const QString source, const QString sourceName) : QString(source) { this->sourceName=sourceName; this->warnings=false; } Template::Template(QFile& file, const QTextCodec* textCodec) { this->warnings=false; sourceName=QFileInfo(file.fileName()).baseName(); if (!file.isOpen()) { file.open(QFile::ReadOnly | QFile::Text); } QByteArray data=file.readAll(); file.close(); if (data.size()==0 || file.error()) { qCritical("Template: cannot read from %s, %s",qPrintable(sourceName),qPrintable(file.errorString())); } else { append(textCodec->toUnicode(data)); } } int Template::setVariable(const QString name, const QString value) { int count=0; QString variable="{"+name+"}"; int start=indexOf(variable); while (start>=0) { replace(start, variable.length(), value); count++; start=indexOf(variable,start+value.length()); } if (count==0 && warnings) { qWarning("Template: missing variable %s in %s",qPrintable(variable),qPrintable(sourceName)); } return count; } int Template::setCondition(const QString name, const bool value) { int count=0; QString startTag=QString("{if %1}").arg(name); QString elseTag=QString("{else %1}").arg(name); QString endTag=QString("{end %1}").arg(name); // search for if-else-end int start=indexOf(startTag); while (start>=0) { int end=indexOf(endTag,start+startTag.length()); if (end>=0) { count++; int ellse=indexOf(elseTag,start+startTag.length()); if (ellse>start && ellse=0) { int end=indexOf(endTag,start+startTag2.length()); if (end>=0) { count++; int ellse=indexOf(elseTag,start+startTag2.length()); if (ellse>start && ellse=0); int count=0; QString startTag="{loop "+name+"}"; QString elseTag="{else "+name+"}"; QString endTag="{end "+name+"}"; // search for loop-else-end int start=indexOf(startTag); while (start>=0) { int end=indexOf(endTag,start+startTag.length()); if (end>=0) { count++; int ellse=indexOf(elseTag,start+startTag.length()); if (ellse>start && ellse0) { QString loopPart=mid(start+startTag.length(), ellse-start-startTag.length()); QString insertMe; for (int i=0; i0) { // and no else part QString loopPart=mid(start+startTag.length(), end-start-startTag.length()); QString insertMe; for (int i=0; i