diff options
| author | 2016-06-13 18:42:42 -0400 | |
|---|---|---|
| committer | 2016-06-13 18:42:42 -0400 | |
| commit | 5d9314f134ddd3dc4c853e398ac90ba247fb2e4f (patch) | |
| tree | 5c457fc188036988d7abd29a3eb09931e406510f /SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp | |
| parent | 8e640722c62692818ab840d50b3758f89a41a54e (diff) | |
Imported Upstream version 3.1.0upstream/3.1.0
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp')
| -rw-r--r-- | SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp | 251 |
1 files changed, 117 insertions, 134 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp b/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp index c89074f..15bf4e8 100644 --- a/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp +++ b/SQLiteStudio3/coreSQLiteStudio/csvserializer.cpp @@ -3,103 +3,85 @@ // TODO write unit tests for CsvSerializer -QString CsvSerializer::serialize(const QList<QStringList>& data, const CsvFormat& format) -{ - QStringList outputRows; - - foreach (const QStringList& dataRow, data) - outputRows << serialize(dataRow, format); - - return outputRows.join(format.rowSeparator); -} - -QString CsvSerializer::serialize(const QStringList& data, const CsvFormat& format) -{ - QString value; - bool hasQuote; - QStringList outputCells; - foreach (const QString& rowValue, data) - { - value = rowValue; - - hasQuote = value.contains("\""); - if (hasQuote) - value.replace("\"", "\"\""); - - if (hasQuote || value.contains(format.columnSeparator) || value.contains(format.rowSeparator)) - value = "\""+value+"\""; - - outputCells << value; - } - - return outputCells.join(format.columnSeparator); -} - -template <class T> -bool isCsvColumnSeparator(const T& data, int pos, const CsvFormat& format) +template <class C> +bool isCsvColumnSeparator(QTextStream& data, const C& theChar, const CsvFormat& format) { - if (!format.strictColumnSeparator && format.columnSeparator.contains(data[pos])) - return true; + if (!format.strictColumnSeparator) + return format.columnSeparator.contains(theChar); + // Strict checking (characters in defined order make a separator) + qint64 origPos = data.pos(); + data.seek(origPos - 1); + C nextChar; for (const QChar& c : format.columnSeparator) { - if (c != data[pos++]) + data >> nextChar; + if (c != nextChar) + { + data.seek(origPos); return false; + } } return true; } -template <class T> -bool isCsvRowSeparator(const T& data, int& pos, const CsvFormat& format) +template <class C> +bool isCsvRowSeparator(QTextStream& data, const C& theChar, const CsvFormat& format) { - if (!format.strictRowSeparator && format.rowSeparator.contains(data[pos])) - return true; + if (!format.strictRowSeparator) + return format.rowSeparator.contains(theChar); - int localPos = pos; + // Strict checking (characters in defined order make a separator) + qint64 origPos = data.pos(); + data.seek(origPos - 1); + C nextChar; for (const QChar& c : format.rowSeparator) { - if (localPos >= data.size() || c != data[localPos++]) + data >> nextChar; + if (data.atEnd() || c != nextChar) + { + data.seek(origPos); return false; + } } - pos = localPos - 1; return true; } -template <class T> -QList<QList<T>> typedDeserialize(const T& data, const CsvFormat& format) +template <class T, class C> +QList<QList<T>> typedDeserialize(QTextStream& data, const CsvFormat& format, bool oneEntry) { QList<QList<T>> rows; QList<T> cells; - int pos = 0; - int lgt = data.length(); bool quotes = false; bool sepAsLast = false; T field = ""; - QChar c; + C c0; + C c1; - while (pos < lgt) + while (!data.atEnd()) { - c = data[pos]; + data >> c0; sepAsLast = false; - if (!quotes && c == '"' ) + if (!quotes && c0 == '"' ) { quotes = true; } - else if (quotes && c == '"' ) + else if (quotes && c0 == '"' ) { - if (pos + 1 < data.length()) + if (!data.atEnd()) { - if (data[pos+1] == '"' ) + data >> c1; + if (c1 == '"' ) { - field += c; - pos++; + field += c0; } else { quotes = false; + data.seek(data.pos() - 1); } } else @@ -110,24 +92,32 @@ QList<QList<T>> typedDeserialize(const T& data, const CsvFormat& format) quotes = false; } } - else if (!quotes && isCsvColumnSeparator(data, pos, format)) + else if (!quotes) { - cells << field; - field.clear(); - sepAsLast = true; - } - else if (!quotes && isCsvRowSeparator(data, pos, format)) - { - cells << field; - rows << cells; - cells.clear(); - field.clear(); + if (isCsvColumnSeparator(data, c0, format)) + { + cells << field; + field = ""; + sepAsLast = true; + } + else if (isCsvRowSeparator(data, c0, format)) + { + cells << field; + rows << cells; + cells.clear(); + field = ""; + if (oneEntry) + break; + } + else + { + field += c0; + } } else { - field += c; + field += c0; } - pos++; } if (field.size() > 0 || sepAsLast) @@ -139,14 +129,61 @@ QList<QList<T>> typedDeserialize(const T& data, const CsvFormat& format) return rows; } -QList<QList<QByteArray> > CsvSerializer::deserialize(const QByteArray& data, const CsvFormat& format) +QString CsvSerializer::serialize(const QList<QStringList>& data, const CsvFormat& format) { - return typedDeserialize<QByteArray>(data, format); + QStringList outputRows; + + foreach (const QStringList& dataRow, data) + outputRows << serialize(dataRow, format); + + return outputRows.join(format.rowSeparator); } -QList<QStringList> CsvSerializer::deserialize(const QString& data, const CsvFormat& format) +QString CsvSerializer::serialize(const QStringList& data, const CsvFormat& format) +{ + QString value; + bool hasQuote; + QStringList outputCells; + foreach (const QString& rowValue, data) + { + value = rowValue; + + hasQuote = value.contains("\""); + if (hasQuote) + value.replace("\"", "\"\""); + + if (hasQuote || value.contains(format.columnSeparator) || value.contains(format.rowSeparator)) + value = "\""+value+"\""; + + outputCells << value; + } + + return outputCells.join(format.columnSeparator); +} + +QStringList CsvSerializer::deserializeOneEntry(QTextStream& data, const CsvFormat& format) +{ + QList<QStringList> deserialized = CsvSerializer::deserialize(data, format, true); + if (deserialized.size() > 0) + return deserialized.first(); + + return QStringList(); +} + +QList<QStringList> CsvSerializer::deserialize(QTextStream& data, const CsvFormat& format) +{ + return CsvSerializer::deserialize(data, format, false); +} + +QList<QList<QByteArray>> CsvSerializer::deserialize(const QByteArray& data, const CsvFormat& format) { - QList<QList<QString>> deserialized = typedDeserialize<QString>(data, format); + QTextStream stream(data, QIODevice::ReadWrite); + return typedDeserialize<QByteArray,char>(stream, format, false); +} + +QList<QStringList> CsvSerializer::deserialize(QTextStream& data, const CsvFormat& format, bool oneEntry) +{ + QList<QList<QString>> deserialized = typedDeserialize<QString, QChar>(data, format, oneEntry); QList<QStringList> finalList; for (const QList<QString>& resPart : deserialized) @@ -155,64 +192,10 @@ QList<QStringList> CsvSerializer::deserialize(const QString& data, const CsvForm return finalList; } +QList<QStringList> CsvSerializer::deserialize(const QString& data, const CsvFormat& format) +{ + QString dataString = data; + QTextStream stream(&dataString, QIODevice::ReadWrite); + return deserialize(stream, format, false); +} -//QList<QStringList> CsvSerializer::deserialize(const QByteArray& data, const CsvFormat& format) -//{ -// QList<QStringList> rows; -// QStringList cells; - -// int pos = 0; -// int lgt = data.length(); -// bool quotes = false; -// bool sepAsLast = false; -// QString field = ""; -// QChar c; - -// while (pos < lgt) -// { -// c = data[pos]; -// sepAsLast = false; -// if (!quotes && c == '"' ) -// { -// quotes = true; -// } -// else if (quotes && c == '"' ) -// { -// if (pos + 1 < data.length() && data[pos+1] == '"' ) -// { -// field += c; -// pos++; -// } -// else -// { -// quotes = false; -// } -// } -// else if (!quotes && format.columnSeparator.contains(c)) -// { -// cells << field; -// field.clear(); -// sepAsLast = true; -// } -// else if (!quotes && format.rowSeparator.contains(c)) -// { -// cells << field; -// rows << cells; -// cells.clear(); -// field.clear(); -// } -// else -// { -// field += c; -// } -// pos++; -// } - -// if (field.size() > 0 || sepAsLast) -// cells << field; - -// if (cells.size() > 0) -// rows << cells; - -// return rows; -//} |
