File indexing completed on 2024-05-19 05:00:55

0001 
0002 // Own
0003 #include "template.h"
0004 
0005 // Qt
0006 #include <QFile>
0007 
0008 bool CSSTemplate::expandToFile(const QString &outputFilename, const QMap<QString, QString> &dict)
0009 {
0010     QFile inf(m_templateFilename);
0011     if (!inf.open(QIODevice::ReadOnly)) {
0012         return false;
0013     }
0014     QTextStream is(&inf);
0015 
0016     QFile outf(outputFilename);
0017     if (!outf.open(QIODevice::WriteOnly)) {
0018         return false;
0019     }
0020     QTextStream os(&outf);
0021 
0022     doExpand(is, os, dict);
0023 
0024     inf.close();
0025     outf.close();
0026     return true;
0027 }
0028 
0029 QString CSSTemplate::expandToString(const QMap<QString, QString> &dict)
0030 {
0031     QFile inf(m_templateFilename);
0032     if (!inf.open(QIODevice::ReadOnly)) {
0033         return QString();
0034     }
0035     QTextStream is(&inf);
0036 
0037     QString out;
0038     QTextStream os(&out);
0039 
0040     doExpand(is, os, dict);
0041 
0042     inf.close();
0043 
0044     return out;
0045 }
0046 
0047 // bool CSSTemplate::expand(const QString &destname, const QMap<QString,QString> &dict)
0048 void CSSTemplate::doExpand(QTextStream &is, QTextStream &os, const QMap<QString, QString> &dict)
0049 {
0050     QString line;
0051     while (!is.atEnd()) {
0052         line = is.readLine();
0053 
0054         int start = line.indexOf('$');
0055         if (start >= 0) {
0056             int end = line.indexOf('$', start + 1);
0057             if (end >= 0) {
0058                 QString expr = line.mid(start + 1, end - start - 1);
0059                 QString res = dict[expr];
0060 
0061                 line.replace(start, end - start + 1, res);
0062             }
0063         }
0064         os << line << Qt::endl;
0065     }
0066 }