X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=CSV.h;fp=CSV.h;h=98483b48af85d313f83fd298c1394c57791e944a;hb=43c287cf53b85a8a347ae12ce8d938b256357ce3;hp=0000000000000000000000000000000000000000;hpb=1672d85141d3ee1ac7f2996c122f3f9ed974b702;p=qwerkisync diff --git a/CSV.h b/CSV.h new file mode 100644 index 0000000..98483b4 --- /dev/null +++ b/CSV.h @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2011, Jamie Thompson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see + * . + */ + +#ifndef CSV_H +#define CSV_H + +#include +class QChar; +class QFile; +class QString; +#include +#include +class QTextStream; + +class CSV +{ +public: + typedef QHash ColumnIndicesHash; + typedef QHash ColumnNamesHash; + + CSV(); + CSV(QChar delimiter, int numColumnsPerRecord, const ColumnIndicesHash &headingIndices); + ~CSV(); + + void Open(QFile &file); + void Open(QFile &file, QChar delimiter, int numColumnsPerRecord, const ColumnIndicesHash &headingIndices); + bool AtEnd() const; + void Close(); + QHash ReadRecord(); + const QStringList HasRequiredHeadings(const QStringList &requiredHeadings); + +protected: + void GetHeadings(const QString &firstLine); + void DetermineDelimiter(const QString &firstLine); + +private: + const QString ExtractString(const QString &originalString); + void UpdateHeadings(const ColumnIndicesHash &headingIndices); + +public: + const unsigned int CurrentRecordNumber() const { return m_RecordNumber; } + unsigned int CurrentRecordNumber() { return m_RecordNumber; } + +protected: + const bool IsValid() { return m_IsValid; } + const QChar Delimiter() const { return m_Delimiter; } + const int NumColumnsPerRecord() const { return m_NumColumnsPerRecord; } + const ColumnIndicesHash & HeadingIndices() const { return m_HeadingIndices; } + const ColumnNamesHash & HeadingNames() const { return m_HeadingNames; } + QFile * const File() { return m_File; } + const QSharedPointer Stream() const { return m_Stream; } + const unsigned int &LineNumber() const { return m_LineNumber; } + unsigned int &LineNumber() { return m_LineNumber; } + const QStringList &LineValues() const { return m_LineValues; } + const unsigned int & RecordNumber() const { return m_RecordNumber; } + unsigned int & RecordNumber() { return m_RecordNumber; } + +protected: + ColumnIndicesHash & HeadingIndices() { return m_HeadingIndices; } + ColumnNamesHash & HeadingNames() { return m_HeadingNames; } + QStringList & LineValues() { return m_LineValues; } + +protected: + void IsValid(const bool isValid) { m_IsValid = isValid; } + void Delimiter(const QChar delimiter) { m_Delimiter = delimiter; } + void NumColumnsPerRecord(const int numColumnsPerRecord) { m_NumColumnsPerRecord = numColumnsPerRecord; } + void HeadingIndices(const ColumnIndicesHash headingIndices) { m_HeadingIndices = headingIndices; } + void HeadingNames(const ColumnNamesHash headingNames) { m_HeadingNames = headingNames; } + void File(QFile * const file) { m_File = file; } + void Stream(QTextStream * const stream) { m_Stream = QSharedPointer(stream); } + void LineNumber(unsigned int lineNumber) { m_LineNumber = lineNumber; } + void RecordNumber(unsigned int recordNumber) { m_RecordNumber = recordNumber; } + void LineValues(QStringList &lineValues) { m_LineValues = lineValues; } + +private: + bool m_IsValid; + QChar m_Delimiter; + int m_NumColumnsPerRecord; + ColumnIndicesHash m_HeadingIndices; + ColumnNamesHash m_HeadingNames; + QFile *m_File; + QSharedPointer m_Stream; + unsigned int m_LineNumber; + unsigned int m_RecordNumber; + QStringList m_LineValues; +}; + +#endif // CSV_H