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
OdsDocHandlerInterface & | m_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.

: m_handler(handler)
{
}
TsvReader::m_handler
OdsDocHandlerInterface & m_handler
Definition tsvreader.h:50

References m_handler.

## ~TsvReader()

TsvReader::~TsvReader | ( |  | ) |

virtual

Definition at line 45 of file tsvreader.cpp.

{
}

## Member Function Documentation

## parse() [1/3]

void TsvReader::parse | ( | QFile & | tsvFile | ) |

Definition at line 67 of file tsvreader.cpp.

{
qDebug() << tsvFile.fileName();
QFileInfo tsv_file_info(tsvFile.fileName());
if(!tsv_file_info.exists())
{
throw OdsException(QObject::tr("TSV file %1 does not exists")
.arg(tsv_file_info.absoluteFilePath()));
}
if(!tsv_file_info.isReadable())
{
throw OdsException(QObject::tr("TSV file %1 is not readable")
.arg(tsv_file_info.absoluteFilePath()));
}
qDebug() << tsv_file_info.absoluteFilePath();
m_handler.startSheet(tsv_file_info.baseName());
if(!tsvFile.open(QIODevice::ReadOnly | QFile::Text))
{
throw OdsException(QObject::tr("Unable to read TSV file %1")
.arg(tsv_file_info.absoluteFilePath()));
}
QTextStream textStreamInput(&tsvFile);
parse(textStreamInput);
tsvFile.close();
qDebug();
}
TsvReader::parse
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.

{
QTextStream textStreamInput(p_inputstream);
while(readCsvRow(textStreamInput))
{
}
m_handler.endDocument();
}
TsvReader::readCsvRow
bool readCsvRow(QTextStream &in)
reads a CSV row based on https://stackoverflow.com/questions/27318631/parsing-through-a-csv-file-in-q...
Definition tsvreader.cpp:115

References m_handler, and readCsvRow().

## parse() [3/3]

void TsvReader::parse | ( | QTextStream & | in | ) |

Definition at line 106 of file tsvreader.cpp.

{
while(readCsvRow(textStreamInput))
{
}
m_handler.endDocument();
}

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.

{
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
m_columnNumber = 0;
m_handler.startLine();
m_rowNumber++;
static const int delta[][5] = {
// , " \n ? eof
{1, 2, -1, 0, -1}, // 0: parsing (store char)
{1, 2, -1, 0, -1}, // 1: parsing (store column)
{3, 4, 3, 3, -2}, // 2: quote entered (no-op)
{3, 4, 3, 3, -2}, // 3: parsing inside quotes (store char)
{1, 3, -1, 0, -1}, // 4: quote exited (no-op)
// -1: end of row, store column, success
// -2: eof inside quotes
};
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
if(textStreamInput.atEnd())
{
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
return false;
}
int state = 0, t, previous_state;
QChar ch;
QString cell;
try
{
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
while(state >= 0)
{
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
<< cell;
if(textStreamInput.atEnd())
t = 4;
else
{
textStreamInput >> ch;
if(ch == m_separator)
t = 0;
else if(ch == '\"')
t = 1;
else if(ch == '\n')
t = 2;
else
t = 3;
}
previous_state = state;
state = delta[state][t];
switch(state)
{
case 0:
case 3:
cell += ch;
break;
case -1:
case 1:
OdsCell odsCell;
m_columnNumber++;
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
<< " " << cell;
if((previous_state == 2) || (previous_state == 3))
{
odsCell.setOfficeValueType("string");
odsCell.setValueString(cell);
}
else
{
odsCell.setValueOfUndefinedType(cell);
}
m_handler.setCell(odsCell);
cell = "";
break;
}
}
if(state == -2)
throw OdsException(
QObject::tr("End-of-file found while inside quotes."));
}
catch(OdsException &error)
{
throw OdsException(QObject::tr("Text parse error at line %1, column %2:\n"
"%3")
.arg(m_rowNumber)
.arg(m_columnNumber)
.arg(error.qwhat()));
}
catch(std::exception &error)
{
throw OdsException(QObject::tr("Text parse error at line %1, column %2:\n"
"%3")
.arg(m_rowNumber)
.arg(m_columnNumber)
.arg(error.what()));
}
m_handler.endLine();
qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
return true;
}
OdsCell::setOfficeValueType
void setOfficeValueType(const QString &type)
Definition odscell.cpp:33
OdsCell::setValueOfUndefinedType
void setValueOfUndefinedType(const QString &value)
Definition odscell.cpp:190
OdsCell::setValueString
void setValueString(const QString &value)
Definition odscell.cpp:65
OdsException::qwhat
virtual const QString & qwhat() const
Definition odsexception.h:53
TsvReader::m_separator
QChar m_separator
Definition tsvreader.h:52
TsvReader::m_columnNumber
std::size_t m_columnNumber
Definition tsvreader.h:47
TsvReader::m_rowNumber
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.

{
switch(separator)
{
case TsvSeparator::tab:
m_separator = '\t';
break;
case TsvSeparator::comma:
m_separator = ',';
break;
case TsvSeparator::semicolon:
m_separator = ';';
break;
}
}
TsvSeparator::semicolon
@ semicolon
Definition config.h:11
TsvSeparator::comma
@ comma
Definition config.h:11
TsvSeparator::tab
@ tab
Definition config.h:11

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:

- odsstream/tsvreader.h
- odsstream/tsvreader.cpp