libodsstream
Library for mass spectrometry
Loading...
Searching...
No Matches
TsvReader Class Reference

#include <tsvreader.h>

Public Member Functions

 TsvReader (OdsDocHandlerInterface &handler)
virtual ~TsvReader ()
void parse (QFile &tsvFile)
void parse (QIODevice *p_inputstream)
void parse (QTextStream &in)
void setSeparator (TsvSeparator separator)

Private Member Functions

bool readCsvRow (QTextStream &in)
 reads a CSV row based on https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt

Private Attributes

std::size_t m_columnNumber =0
std::size_t m_rowNumber =0
OdsDocHandlerInterfacem_handler
QChar m_separator = '\t'

Detailed Description

Definition at line 43 of file tsvreader.h.

Constructor & Destructor Documentation

◆ TsvReader()

TsvReader::TsvReader ( OdsDocHandlerInterface & handler)

creates an ODS reader with a reader handler

@Param OdsDocHandlerInterface & the interface to implement to read ODS files

Definition at line 41 of file tsvreader.cpp.

41 : m_handler(handler)
42{
43}
OdsDocHandlerInterface & m_handler
Definition tsvreader.h:50

References m_handler.

◆ ~TsvReader()

TsvReader::~TsvReader ( )
virtual

Definition at line 45 of file tsvreader.cpp.

46{
47}

Member Function Documentation

◆ parse() [1/3]

void TsvReader::parse ( QFile & tsvFile)

Definition at line 67 of file tsvreader.cpp.

68{
69 qDebug() << tsvFile.fileName();
70 QFileInfo tsv_file_info(tsvFile.fileName());
71 if(!tsv_file_info.exists())
72 {
73 throw OdsException(QObject::tr("TSV file %1 does not exists")
74 .arg(tsv_file_info.absoluteFilePath()));
75 }
76 if(!tsv_file_info.isReadable())
77 {
78 throw OdsException(QObject::tr("TSV file %1 is not readable")
79 .arg(tsv_file_info.absoluteFilePath()));
80 }
81 qDebug() << tsv_file_info.absoluteFilePath();
82 m_handler.startSheet(tsv_file_info.baseName());
83 if(!tsvFile.open(QIODevice::ReadOnly | QFile::Text))
84 {
85 throw OdsException(QObject::tr("Unable to read TSV file %1")
86 .arg(tsv_file_info.absoluteFilePath()));
87 }
88 QTextStream textStreamInput(&tsvFile);
89 parse(textStreamInput);
90 tsvFile.close();
91 qDebug();
92}
void parse(QFile &tsvFile)
Definition tsvreader.cpp:67

References m_handler, and parse().

Referenced by parse().

◆ parse() [2/3]

void TsvReader::parse ( QIODevice * p_inputstream)

Definition at line 95 of file tsvreader.cpp.

96{
97
98 QTextStream textStreamInput(p_inputstream);
99 while(readCsvRow(textStreamInput))
100 {
101 }
102 m_handler.endDocument();
103}
bool readCsvRow(QTextStream &in)
reads a CSV row based on https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-q...

References m_handler, and readCsvRow().

◆ parse() [3/3]

void TsvReader::parse ( QTextStream & in)

Definition at line 106 of file tsvreader.cpp.

107{
108 while(readCsvRow(textStreamInput))
109 {
110 }
111 m_handler.endDocument();
112}

References m_handler, and readCsvRow().

◆ readCsvRow()

bool TsvReader::readCsvRow ( QTextStream & in)
private

reads a CSV row based on https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-qt

Definition at line 115 of file tsvreader.cpp.

116{
117 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
118 m_columnNumber = 0;
119 m_handler.startLine();
120 m_rowNumber++;
121 static const int delta[][5] = {
122 // , " \n ? eof
123 {1, 2, -1, 0, -1}, // 0: parsing (store char)
124 {1, 2, -1, 0, -1}, // 1: parsing (store column)
125 {3, 4, 3, 3, -2}, // 2: quote entered (no-op)
126 {3, 4, 3, 3, -2}, // 3: parsing inside quotes (store char)
127 {1, 3, -1, 0, -1}, // 4: quote exited (no-op)
128 // -1: end of row, store column, success
129 // -2: eof inside quotes
130 };
131 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
132 if(textStreamInput.atEnd())
133 {
134 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
135 return false;
136 }
137
138 int state = 0, t, previous_state;
139 QChar ch;
140 QString cell;
141
142 try
143 {
144 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
145 while(state >= 0)
146 {
147 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
148 << cell;
149 if(textStreamInput.atEnd())
150 t = 4;
151 else
152 {
153 textStreamInput >> ch;
154 if(ch == m_separator)
155 t = 0;
156 else if(ch == '\"')
157 t = 1;
158 else if(ch == '\n')
159 t = 2;
160 else
161 t = 3;
162 }
163
164 previous_state = state;
165 state = delta[state][t];
166
167 switch(state)
168 {
169 case 0:
170 case 3:
171 cell += ch;
172 break;
173 case -1:
174 case 1:
175 OdsCell odsCell;
177 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
178 << " " << cell;
179 if((previous_state == 2) || (previous_state == 3))
180 {
181 odsCell.setOfficeValueType("string");
182 odsCell.setValueString(cell);
183 }
184 else
185 {
186 odsCell.setValueOfUndefinedType(cell);
187 }
188 m_handler.setCell(odsCell);
189 cell = "";
190 break;
191 }
192 }
193
194
195 if(state == -2)
196 throw OdsException(
197 QObject::tr("End-of-file found while inside quotes."));
198 }
199 catch(OdsException &error)
200 {
201 throw OdsException(QObject::tr("Text parse error at line %1, column %2:\n"
202 "%3")
203 .arg(m_rowNumber)
204 .arg(m_columnNumber)
205 .arg(error.qwhat()));
206 }
207
208 catch(std::exception &error)
209 {
210 throw OdsException(QObject::tr("Text parse error at line %1, column %2:\n"
211 "%3")
212 .arg(m_rowNumber)
213 .arg(m_columnNumber)
214 .arg(error.what()));
215 }
216
217 m_handler.endLine();
218 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
219 return true;
220}
void setOfficeValueType(const QString &type)
Definition odscell.cpp:33
void setValueOfUndefinedType(const QString &value)
Definition odscell.cpp:190
void setValueString(const QString &value)
Definition odscell.cpp:65
virtual const QString & qwhat() const
QChar m_separator
Definition tsvreader.h:52
std::size_t m_columnNumber
Definition tsvreader.h:47
std::size_t m_rowNumber
Definition tsvreader.h:48

References m_columnNumber, m_handler, m_rowNumber, m_separator, OdsException::qwhat(), OdsCell::setOfficeValueType(), OdsCell::setValueOfUndefinedType(), OdsCell::setValueString(), and OdsException::what().

Referenced by parse(), and parse().

◆ setSeparator()

void TsvReader::setSeparator ( TsvSeparator separator)

Definition at line 49 of file tsvreader.cpp.

50{
51
52 switch(separator)
53 {
55 m_separator = '\t';
56 break;
58 m_separator = ',';
59 break;
61 m_separator = ';';
62 break;
63 }
64}

References comma, m_separator, semicolon, and tab.

Member Data Documentation

◆ m_columnNumber

std::size_t TsvReader::m_columnNumber =0
private

Definition at line 47 of file tsvreader.h.

Referenced by readCsvRow().

◆ m_handler

OdsDocHandlerInterface& TsvReader::m_handler
private

Definition at line 50 of file tsvreader.h.

Referenced by TsvReader(), parse(), parse(), parse(), and readCsvRow().

◆ m_rowNumber

std::size_t TsvReader::m_rowNumber =0
private

Definition at line 48 of file tsvreader.h.

Referenced by readCsvRow().

◆ m_separator

QChar TsvReader::m_separator = '\t'
private

Definition at line 52 of file tsvreader.h.

Referenced by readCsvRow(), and setSeparator().


The documentation for this class was generated from the following files: