#include "eettemplate.h" #include #include EetTemplate::EetTemplate(QObject *parent) : QObject(parent) { } EetTemplate::EetTemplate(const QString &tmp, QObject *parent) : QObject(parent) { QFile tmpFile(tmp); tmpFile.open(QFile::ReadOnly | QFile::Text); QTextStream stream(&tmpFile); m_template = stream.readAll(); } QString EetTemplate::fillTemplate(EetRequest *request) { return fillTemplateInternal(request, nullptr); } QString EetTemplate::fillTemplate(QMap map) { return fillTemplateInternal(nullptr, &map); } QString EetTemplate::fillTemplateInternal(EetRequest *request, QMap *map) { QString ret; QString phStart; if (request != nullptr) { phStart = "@{"; } else { phStart = "${"; } for (int i = 0; i < m_template.size(); i++) { QChar c = m_template[i]; if (!m_template.mid(i).startsWith(phStart)) { ret += c; continue; } int p = m_template.indexOf("}", i + 2); if (p < 0) { return ret; } QString var = m_template.mid(i + 2, p - i - 2); QString value; if (request != nullptr) { value = request->property(var.toStdString().c_str()).toString(); } else { value = map->value(var, NULL_VAL); } if (value != NULL_VAL) { QString attrib = QString("%1=\"%2\"").arg(var, value); ret += attrib; } i = p; } return ret; }