-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVRow_QFile.cpp
60 lines (49 loc) · 1.36 KB
/
CSVRow_QFile.cpp
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
#include <QString>
#include <QTextStream>
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include "CSVRow_QFile.h"
/* -----------------------------------------------
A class for reading from a .csv file
This class is due to Martin York on Stack Overflow
https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c
-------------------------------------------------*/
QString const& CSVRow_QFile::operator[](std::size_t index) const
{
return m_data[index];
}
std::size_t CSVRow_QFile::size() const
{
return m_data.size();
}
void CSVRow_QFile::readNextRow(QTextStream& str)
{
QString line;
str.readLineInto(&line);
QTextStream lineStream(&line);
QString cell;
m_data.clear();
while(!lineStream.atEnd())
{
lineStream.readLineInto(&cell);
QStringList stringList = cell.split(',');
for (int k = 0; k < stringList.length(); k++) {
m_data.push_back(cell);
}
}
// This checks for a trailing comma with no data after it.
if (lineStream.atEnd() && cell.isEmpty())
{
// If there was a trailing comma then add an empty element.
m_data.push_back("");
}
}
QTextStream& operator>>(QTextStream& str, CSVRow_QFile& data)
{
data.readNextRow(str);
return str;
}