You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB
C++

8 years ago
#include "eettemplate.h"
#include <QFile>
#include <QTextStream>
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<QString, QString> map)
{
return fillTemplateInternal(nullptr, &map);
}
QString EetTemplate::fillTemplateInternal(EetRequest *request, QMap<QString, QString> *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;
}