Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / konqhtml / css / template.cpp
blob6d8d93accf41581c466c83992675e323b3d8ff7f
2 // Own
3 #include "template.h"
5 // Qt
6 #include <QtCore/QFile>
7 #include <QtCore/QTextStream>
10 bool CSSTemplate::expandToFile(const QString& outputFilename, const QMap<QString,QString> &dict)
12 QFile inf(m_templateFilename);
13 if (!inf.open(QIODevice::ReadOnly)) return false;
14 QTextStream is(&inf);
16 QFile outf(outputFilename);
17 if (!outf.open(QIODevice::WriteOnly)) return false;
18 QTextStream os(&outf);
20 doExpand(is, os, dict);
22 inf.close();
23 outf.close();
24 return true;
27 QString CSSTemplate::expandToString(const QMap<QString,QString> &dict)
29 QFile inf(m_templateFilename);
30 if (!inf.open(QIODevice::ReadOnly)) return QString();
31 QTextStream is(&inf);
33 QString out;
34 QTextStream os(&out);
36 doExpand(is, os, dict);
38 inf.close();
40 return out;
43 // bool CSSTemplate::expand(const QString &destname, const QMap<QString,QString> &dict)
44 void CSSTemplate::doExpand(QTextStream &is, QTextStream &os, const QMap<QString,QString> &dict)
46 QString line;
47 while (!is.atEnd())
49 line = is.readLine();
51 int start = line.indexOf('$');
52 if (start >= 0)
54 int end = line.indexOf('$', start+1);
55 if (end >= 0)
57 QString expr = line.mid(start+1, end-start-1);
58 QString res = dict[expr];
60 line.replace(start, end-start+1, res);
63 os << line << endl;