Here's an example.
Of course, I made this in just a couple of minutes to demonstrate how you can parse this file.
There are tons of places that can benefit from improvements, but that is up to you.
And I only used a few elements of your file, not all, that too is up to you.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
struct BoardElement {
};
struct FrameElement {
QList<BoardElement *> boardElements;
};
Q_OBJECT
public:
~MainWindow();
protected:
private slots:
void startParsing();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
struct BoardElement {
QString id;
};
struct FrameElement {
QList<BoardElement *> boardElements;
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private slots:
void startParsing();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startParsing()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::startParsing()
{
QFile file("./file.txt");
if (!file.
open(QFile::ReadOnly)) return;
FrameElement *frameElement;
BoardElement *boardElement;
int depth = 0;
int currentState = 0;
//states tell the parser which element it is parsing.
// 1 = Frame element
// 2 = BoardList element
// 3 = Board element
// 4 = Id element
// 5 = ame element (between "")
QList<int> stateList;
do {
ts >> ch;
if (ch == '(') {
++depth;
if (element == "Frame" || element == "frame") {
stateList.append(1);
frameElement = new FrameElement;
qDebug() << "Found a Frame element";
}
if (element == "BoardList" || element == "boardlist") {
stateList.append(2);
qDebug() << "Found a BoardList element";
}
if (element == "Board" || element == "board") {
stateList.append(3);
if (!frameElement) {
qDebug() << "Parsing error: no frame element found!";
return;
}
boardElement = new BoardElement;
frameElement->boardElements.append(boardElement);
qDebug() << "Found a Board element";
}
if (element == "Id" || element == "id" || element == "ID") {
stateList.append(4);
qDebug() << "Found an Id element";
}
element = "";
}
else if (ch == ')') {
--depth;
if (element.startsWith("\"")) {
if (stateList.count() < 2) {
//parsing error, the list should have at least 2 items
return;
}
int currentState = stateList.at(stateList.count() - 2); // Go back two states, the previous is the id, the one before the id
// tells wich object receives the name
qDebug() << "Found a name element in state " << currentState;
element = element.section("\"",1,1);
if (currentState == 3) {
boardElement->id = element;
qDebug() << "Set the board element id to:" << boardElement->id;
}
}
if (!stateList.isEmpty())
stateList.removeLast();
element = "";
}
else if (ch == '\r' || ch == '\n' || ch == ' ') {
// do nothing
// If you are able to use spaces in names, then create a boolean value to check if you're parsing a name.
// If you do (boolean = true), do not swallow spaces but add them to the parsed string.
// If you don't (boolean = false), swallow the spaces
}
else {
element += ch;
}
} while (!ts.atEnd());
file.close();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startParsing()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::startParsing()
{
QFile file("./file.txt");
if (!file.open(QFile::ReadOnly))
return;
QTextStream ts(&file);
FrameElement *frameElement;
BoardElement *boardElement;
QChar ch;
int depth = 0;
int currentState = 0;
//states tell the parser which element it is parsing.
// 1 = Frame element
// 2 = BoardList element
// 3 = Board element
// 4 = Id element
// 5 = ame element (between "")
QList<int> stateList;
QString element = "";
do {
ts >> ch;
if (ch == '(') {
++depth;
if (element == "Frame" || element == "frame") {
stateList.append(1);
frameElement = new FrameElement;
qDebug() << "Found a Frame element";
}
if (element == "BoardList" || element == "boardlist") {
stateList.append(2);
qDebug() << "Found a BoardList element";
}
if (element == "Board" || element == "board") {
stateList.append(3);
if (!frameElement) {
qDebug() << "Parsing error: no frame element found!";
return;
}
boardElement = new BoardElement;
frameElement->boardElements.append(boardElement);
qDebug() << "Found a Board element";
}
if (element == "Id" || element == "id" || element == "ID") {
stateList.append(4);
qDebug() << "Found an Id element";
}
element = "";
}
else if (ch == ')') {
--depth;
if (element.startsWith("\"")) {
if (stateList.count() < 2) {
//parsing error, the list should have at least 2 items
return;
}
int currentState = stateList.at(stateList.count() - 2); // Go back two states, the previous is the id, the one before the id
// tells wich object receives the name
qDebug() << "Found a name element in state " << currentState;
element = element.section("\"",1,1);
if (currentState == 3) {
boardElement->id = element;
qDebug() << "Set the board element id to:" << boardElement->id;
}
}
if (!stateList.isEmpty())
stateList.removeLast();
element = "";
}
else if (ch == '\r' || ch == '\n' || ch == ' ') {
// do nothing
// If you are able to use spaces in names, then create a boolean value to check if you're parsing a name.
// If you do (boolean = true), do not swallow spaces but add them to the parsed string.
// If you don't (boolean = false), swallow the spaces
}
else {
element += ch;
}
} while (!ts.atEnd());
file.close();
}
To copy to clipboard, switch view to plain text mode
Bookmarks