soubor build-conv-../example.txt
Nejaka hlavicka
@repeat
Jedna polozka (@name, @ red, @blue, @green)
@next
Konec
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void load (QString file_name);
void put (QString s);
void scan ();
void save (QString file_name);
private slots:
void on_actionRun_triggered();
private:
Ui::MainWindow *ui;
QString answer;
};
#endif // MAINWINDOW_H
do .ui pridat QPlainTextEdit input, output
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
load ("example.txt");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::load (QString file_name)
{
QFile file (file_name);
if (file.open (QIODevice::ReadOnly | QIODevice::Text))
{
QByteArray a = file.readAll ();
// QString s = QString::fromLatin1(a.data ());
ui->input->setPlainText (a);
}
}
void MainWindow::put (QString s)
{
if (!silent)
answer += s;
}
//ahoj
#include
void MainWindow::scan()
{
answer = "";
silent = false;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.sqlite");
db.open ();
QString table = "colors";
QSqlQuery query = db.exec("SELECT * FROM " + table);
QString txt = ui->input->toPlainText();
int len = txt.length();
int inx = 0;
int start = 0;
while (inx < len)
{
QChar c = txt [inx ++];
if (c == '\r')
/* nothing */ ;
else if (c != '@')
put (c);
else
{
QString id = "";
bool stop = false;
while (inx < len && ! stop)
{
c = txt [inx];
if (c.isLetter())
{
id += c;
inx ++;
}
else stop = true;
}
if (id == "")
put ("@");
else if (id == "repeat")
{
start = inx;
bool ok = query.next ();
silent = ! ok;
}
else if (id == "next")
{
bool ok = query.next ();
if (ok)
inx = start;
else
silent = false;
}
else
{
QString s = query.value (id).toString();
put (s);
}
}
}
ui->output->setPlainText(answer);
}
void MainWindow::on_actionRun_triggered()
{
scan ();
}
#include
void MainWindow::on_actionOpen_triggered()
{
QString s = QFileDialog::getOpenFileName (this, "File with template");
if (s != "")
load (s);
}