PDA

View Full Version : Help with parsing a QString



bikonja
7th June 2015, 16:21
Hey folks.

Consider i have this QString "EMU 978 EUR 1 7,537897 7,560579 7,583261"

and i need to use the 7,560579 column for further calculations.

any suggestions on how to do it properly?

Lesiok
7th June 2015, 16:34
Read about QString::split

bikonja
7th June 2015, 17:27
thx, that helped. now if i can trouble you some more :D

so i have this parser class im making


#include <QFile>
#include <QTextStream>
#include <QString>
#include <QTextStream>
#include <QMessageBox>
#include <QDebug>
#include <QStringList>
class parser : public QObject

{
Q_OBJECT
public:
explicit parser(QObject *parent = 0);

void process_line(QString line);
int read_line();
void init_map();
void init_list();

private:
QMap<QString,float> map;
QStringList valute;
};

and the .cpp


#include "parser.h"

parser::parser(QObject *parent) :
QObject(parent)
{
init_map();
init_list();
}
void parser::process_line(QString line)
{
QString str="EUR";
QStringList temporary_list;
if(line.contains(str,Qt::CaseInsensitive)) this is the part im having trouble with now**
{
qDebug()<<"found euro";
temporary_list=line.split(" ", QString::SkipEmptyParts);
map.insert("EUR",temporary_list[6].toFloat());

}

}
int parser::read_line()
{

QFile file("C:/downloaded.txt");

if(!file.exists())
{
QMessageBox msgBox;
msgBox.setText("There is no such file");
msgBox.exec();
return 1;
}

if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox msgBox;
msgBox.setText("Error while opening file");
return 1;
}

QTextStream in_stream(&file);
QString line=in_stream.readLine();


while (!line.isNull())
{
process_line(line);
line = in_stream.readLine();
}


return 0;
}

void parser::init_map()
{
map.insert("AUD",0);
map.insert("CAD",0);
map.insert("CZK",0);
map.insert("DKK",0);
map.insert("NOK",0);
map.insert("SEK",0);
map.insert("CHF",0);
map.insert("GBP",0);
map.insert("USD",0);
map.insert("EUR",0);
map.insert("PLN",0);
}

void parser::init_list()
{
valute << "AUD" << "CAD" << "CZK" << "DKK" << "NOK" << "SEK"
<< "CHF" << "GBP" << "USD" << "EUR" << "PLN";
}

i have a txt file of over 60 lines from which i have to read the values.
so i would like my process_line function to check the QString it receives if there is any matching strings from my QStringlist in there.
and if so to insert the value to my map.

im not sure how to write that in my if statement, and im pretty sure also i have the wrong aproach here. so if you could point me in the right direction again, would be very much appreciated.

Lesiok
7th June 2015, 18:00
But what is a problem ?

bikonja
7th June 2015, 18:06
i dont know how the write the loop.

i did this one with checking only for "EUR" in my line.

to be more precise with some pseudo coding

if(line contains any of the strings from my value QStringList)
split the line into a new qstringlist
instert value from index no.6 to the matching key/value pair in my map

Radek
7th June 2015, 18:56
QStringList::contains()
QStringList::indexOf()

Lesiok
7th June 2015, 18:59
Simply :

void parser::process_line(QString line)
{
QStringList str;
str << "EUR" << "USD" << "GBP";
for( int i = 0; i < str.size(); i++ )
{
if(line.contains(str.at(i),Qt::CaseInsensitive))
{
QStringList temporary_list;
qDebug()<<"found " << str.at(i);
temporary_list=line.split(" ", QString::SkipEmptyParts);
map.insert(str.at(i)",temporary_list[6].toFloat());
}
}
}