Added CSV parsing and export of Symbian-format Event logs that have had their tables...
[qwerkisync] / CSV.h
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef CSV_H
20 #define CSV_H
21
22 #include <QHash>
23 class QChar;
24 class QFile;
25 class QString;
26 #include <QSharedPointer>
27 #include <QStringList>
28 class QTextStream;
29
30 class CSV
31 {
32 public:
33         typedef QHash<QString, uint> ColumnIndicesHash;
34         typedef QHash<uint, QString> ColumnNamesHash;
35
36         CSV();
37         CSV(QChar delimiter, int numColumnsPerRecord, const ColumnIndicesHash &headingIndices);
38         ~CSV();
39
40         void Open(QFile &file);
41         void Open(QFile &file, QChar delimiter, int numColumnsPerRecord, const ColumnIndicesHash &headingIndices);
42         bool AtEnd() const;
43         void Close();
44         QHash<QString, QString> ReadRecord();
45         const QStringList HasRequiredHeadings(const QStringList &requiredHeadings);
46
47 protected:
48         void GetHeadings(const QString &firstLine);
49         void DetermineDelimiter(const QString &firstLine);
50
51 private:
52         const QString ExtractString(const QString &originalString);
53         void UpdateHeadings(const ColumnIndicesHash &headingIndices);
54
55 public:
56         const unsigned int CurrentRecordNumber() const { return m_RecordNumber; }
57         unsigned int CurrentRecordNumber() { return m_RecordNumber; }
58
59 protected:
60         const bool IsValid() { return m_IsValid; }
61         const QChar Delimiter() const { return m_Delimiter; }
62         const int NumColumnsPerRecord() const { return m_NumColumnsPerRecord; }
63         const ColumnIndicesHash & HeadingIndices() const { return m_HeadingIndices; }
64         const ColumnNamesHash & HeadingNames() const { return m_HeadingNames; }
65         QFile * const File() { return m_File; }
66         const QSharedPointer<QTextStream> Stream() const { return m_Stream; }
67         const unsigned int &LineNumber() const { return m_LineNumber; }
68         unsigned int &LineNumber() { return m_LineNumber; }
69         const QStringList &LineValues() const { return m_LineValues; }
70         const unsigned int & RecordNumber() const { return m_RecordNumber; }
71         unsigned int & RecordNumber() { return m_RecordNumber; }
72
73 protected:
74         ColumnIndicesHash & HeadingIndices() { return m_HeadingIndices; }
75         ColumnNamesHash & HeadingNames() { return m_HeadingNames; }
76         QStringList & LineValues() { return m_LineValues; }
77
78 protected:
79         void IsValid(const bool isValid) { m_IsValid = isValid; }
80         void Delimiter(const QChar delimiter) { m_Delimiter = delimiter; }
81         void NumColumnsPerRecord(const int numColumnsPerRecord) { m_NumColumnsPerRecord = numColumnsPerRecord; }
82         void HeadingIndices(const ColumnIndicesHash headingIndices) { m_HeadingIndices = headingIndices; }
83         void HeadingNames(const ColumnNamesHash headingNames) { m_HeadingNames = headingNames; }
84         void File(QFile * const file) { m_File = file; }
85         void Stream(QTextStream * const stream) { m_Stream = QSharedPointer<QTextStream>(stream); }
86         void LineNumber(unsigned int lineNumber) { m_LineNumber = lineNumber; }
87         void RecordNumber(unsigned int recordNumber) { m_RecordNumber = recordNumber; }
88         void LineValues(QStringList &lineValues) { m_LineValues = lineValues; }
89
90 private:
91         bool m_IsValid;
92         QChar m_Delimiter;
93         int m_NumColumnsPerRecord;
94         ColumnIndicesHash m_HeadingIndices;
95         ColumnNamesHash m_HeadingNames;
96         QFile *m_File;
97         QSharedPointer<QTextStream> m_Stream;
98         unsigned int m_LineNumber;
99         unsigned int m_RecordNumber;
100         QStringList m_LineValues;
101 };
102
103 #endif // CSV_H