diff options
Diffstat (limited to 'sqliteutil.cpp')
-rw-r--r-- | sqliteutil.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/sqliteutil.cpp b/sqliteutil.cpp new file mode 100644 index 0000000..93b91de --- /dev/null +++ b/sqliteutil.cpp @@ -0,0 +1,55 @@ +#include <sstream> +#include <cstring> +#include "sqliteutil.h" + + +namespace sqlite { + std::vector<std::vector<std::string>> parseCSV(std::string output) { + std::istringstream ss{output}; + std::vector<std::vector<std::string>> table; + + std::string line; + while (std::getline(ss, line)) { + while (!line.empty() && strchr("\r\n", line.back()) != nullptr) + line.pop_back(); + + table.emplace_back(); + + if (line.empty()) continue; + + std::vector<std::string> &row = table.back(); + row.emplace_back(); + bool inString = false; + for (size_t i = 0; i < line.size(); i++) { + switch (line[i]) { + case '"': + if (inString) { + if (i + 1 < line.size() && line[i+1] == '"') { + row.back().push_back('"'); + i++; + } else { + inString = false; + } + } else { + inString = true; + } + break; + + case ',': + if (inString) { + row.back().push_back(','); + } else { + row.emplace_back(); + } + break; + + default: + row.back().push_back(line[i]); + break; + } + } + } + + return table; + } +} |