#include "csvreader.h" #include "parser.h" namespace usql { CsvReader::CsvReader(bool skip_hdr, char field_sep, char quote_ch, char line_sep, char line_sep2) { skip_header = skip_hdr; field_separator = field_sep; quote_character = quote_ch; line_separator = line_sep; line_separator2 = line_sep2; header_skiped = !skip_hdr; } int CsvReader::parseCSV2(const std::string &csvSource, std::vector &cols_def, void (Table::*function)(const std::vector&, const std::vector&), Table& a) { int row_cnt = 0; bool inQuote(false); bool newLine(false); std::string field; std::vector line; line.reserve(32); std::string::const_iterator aChar = csvSource.begin(); while (aChar != csvSource.end()) { if (*aChar == quote_character) { newLine = false; inQuote = !inQuote; } else if (*aChar == field_separator) { newLine = false; if (inQuote == true) { field += *aChar; } else { line.push_back(field); field.clear(); } } else if (*aChar == line_separator || *aChar == line_separator2) { if (inQuote == true) { field += *aChar; } else { if (newLine == false) { line.push_back(field); if (header_skiped) { (a.*function)(cols_def, line); row_cnt++; } header_skiped = true; field.clear(); line.clear(); newLine = true; } } } else { newLine = false; field.push_back(*aChar); } aChar++; } if (!field.empty()) line.push_back(field); if (header_skiped) { (a.*function)(cols_def, line); row_cnt++; header_skiped = true; } return row_cnt; } } // namespace