-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (84 loc) · 2.48 KB
/
Copy pathmain.cpp
File metadata and controls
105 lines (84 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <algorithm>
#include <QVariant>
#include <QList>
#include <QStringList>
#include <QDir>
#include <QDebug>
#include "qtcsv/variantdata.h"
#include "qtcsv/reader.h"
#include "qtcsv/writer.h"
void WriteToFile(const QString& filePath);
void ReadAndPrint(const QString& filePath);
void ReadAndProcess(const QString& filePath);
int main()
{
QString filePath = QDir::currentPath() + "/info.csv";
WriteToFile(filePath);
ReadAndPrint(filePath);
ReadAndProcess(filePath);
return 0;
}
void WriteToFile(const QString& filePath)
{
qDebug() << "=== Write to csv-file ==";
QVariant first(2);
QList<QVariant> second;
second << QVariant("pi") << 3.14159265359;
QStringList fourth;
fourth << "one" << "two";
QtCSV::VariantData varData;
varData << first << second;
varData.addEmptyRow();
varData.addRow(fourth);
if ( false == QtCSV::Writer::write(filePath, varData) )
{
qDebug() << "Failed to write to a file";
}
qDebug() << "... Write is OK";
}
void ReadAndPrint(const QString& filePath)
{
qDebug() << "=== Read csv-file and print it content to terminal ==";
QList<QStringList> readData = QtCSV::Reader::readToList(filePath);
for ( int i = 0; i < readData.size(); ++i )
{
qDebug() << readData.at(i).join(",");
}
}
void ReadAndProcess(const QString& filePath)
{
qDebug() << "=== Read csv-file and process it content ==";
// Create processor that:
// - replate empty lines by some data
// - revert elements in a row and save them into internal container
class RevertProcessor : public QtCSV::Reader::AbstractProcessor
{
public:
QList< QStringList > data;
virtual void preProcessRawLine(QString& line)
{
if (line.isEmpty())
{
line = "Say 'No' to empty lines!";
}
}
virtual bool processRowElements(const QStringList& elements)
{
QList<QString> revertedElements(elements);
std::reverse(revertedElements.begin(), revertedElements.end());
data.push_back(QStringList(revertedElements));
return true;
}
};
RevertProcessor processor;
if (false == QtCSV::Reader::readToProcessor(filePath, processor))
{
qDebug() << "Failed to read file";
return;
}
// Print rows with reverted elements
for ( int i = 0; i < processor.data.size(); ++i )
{
qDebug() << processor.data.at(i).join(",");
}
}