Last active
November 1, 2022 14:44
-
-
Save JC3/4ec7f6f2883aec7087bff342494bd791 to your computer and use it in GitHub Desktop.
Qt: CSV row parsing (Excel style)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example usage: | |
QFile csv(filename); | |
csv.open(QFile::ReadOnly | QFile::Text); | |
QTextStream in(&csv); | |
QStringList row; | |
while (readCSVRow(in, &row)) | |
qDebug() << row; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
/** | |
* Parses Excel style CSV's, handling quotes and double-quotes appropriately, and | |
* allows newlines in fields. Handles Windows and Unix line endings properly as | |
* long as your file is opened with QFile::Text. Doesn't support binary-mode | |
* untranslated line-endings. | |
* | |
* @param in A valid QTextStream. | |
* @param row Receives columns. Cleared first. Do not pass nullptr. | |
* @return True on success, false on EOF. | |
* @throws std::runtime_error on parse error. | |
*/ | |
bool readCSVRow (QTextStream &in, QStringList *row) { | |
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 | |
}; | |
row->clear(); | |
if (in.atEnd()) | |
return false; | |
int state = 0, t; | |
char ch; | |
QString cell; | |
while (state >= 0) { | |
if (in.atEnd()) | |
t = 4; | |
else { | |
in >> ch; | |
if (ch == ',') t = 0; | |
else if (ch == '\"') t = 1; | |
else if (ch == '\n') t = 2; | |
else t = 3; | |
} | |
state = delta[state][t]; | |
if (state == 0 || state == 3) { | |
cell += ch; | |
} else if (state == -1 || state == 1) { | |
row->append(cell); | |
cell = ""; | |
} | |
} | |
if (state == -2) | |
throw runtime_error("End-of-file found while inside quotes."); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Info at this SO post.