forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvparser.h
More file actions
82 lines (66 loc) · 1.71 KB
/
csvparser.h
File metadata and controls
82 lines (66 loc) · 1.71 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
#ifndef CSVPARSER_H
#define CSVPARSER_H
#include <QChar>
#include <QVector>
#include <QStringList>
class QTextStream;
/*!
* \brief The CSVProgress class
*
* This is an abstract class, an implementation of which you can pass to the CSVParser to get progress updates.
*/
class CSVProgress
{
public:
virtual ~CSVProgress() { }
virtual void start() = 0;
virtual bool update(size_t pos) = 0;
virtual void end() = 0;
};
class CSVParser
{
public:
typedef QVector<QStringList> TCSVResult;
CSVParser(bool trimfields = true, const QChar& fieldseparator = ',', const QChar& quotechar = '"');
~CSVParser();
/*!
* \brief parse the given stream
* \param stream Stream with the CSV parser
* \param nMaxRecords Max records too read, -1 if unlimited
* \return True if parsing worked.
*/
bool parse(QTextStream& stream, qint64 nMaxRecords = -1);
/*!
* \brief csv
* \return The parse result
*/
const TCSVResult& csv() const { return m_vCSVData; }
/*!
* \brief columns
* \return Number of columns parsed
*/
size_t columns() const { return m_nColumns; }
void setCSVProgress(CSVProgress* csvp) { m_pCSVProgress = csvp; }
private:
enum ParseStates
{
StateNormal,
StateInQuote,
StateEndQuote
};
inline void addRow(QStringList& r)
{
m_vCSVData.append(r);
m_nColumns = std::max<size_t>(r.size(), m_nColumns);
r.clear();
}
private:
bool m_bTrimFields;
QChar m_cFieldSeparator;
QChar m_cQuoteChar;
CSVProgress* m_pCSVProgress;
TCSVResult m_vCSVData;
size_t m_nColumns;
size_t m_nBufferSize; //! internal buffer read size
};
#endif // CSVPARSER_H