small refactoring

This commit is contained in:
2021-12-19 12:58:17 +01:00
parent 04c0ed3f03
commit 35cba3b0c4
8 changed files with 753 additions and 746 deletions

View File

@@ -1,4 +1,4 @@
#include <errno.h>
#include <cerrno>
#include "exception.h"
#include "csvreader.h"
@@ -7,15 +7,15 @@
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;
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;
}
header_skiped = !skip_hdr;
}
int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &cols_def, Table &table) {
@@ -41,7 +41,7 @@ int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &co
size_t len = 0;
int read_chars;
long read_chars;
while ((read_chars = getline(&line_str, &len, fp)) != -1) {
if (skip_header && !header_skiped) {
header_skiped = true;
@@ -59,7 +59,7 @@ int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &co
if (*aChar == quote_character) {
inQuote = !inQuote;
} else if (*aChar == field_separator) {
if (inQuote == true) {
if (inQuote) {
field += *aChar;
} else {
line.push_back(field);
@@ -80,9 +80,6 @@ int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &co
field.clear();
line.clear();
// DEBUG
// if (row_cnt > 50000) break;
//
}
fclose(fp);
@@ -93,53 +90,53 @@ int CsvReader::parseCSV(const std::string &filename, std::vector<ColDefNode> &co
return row_cnt;
}
int CsvReader::parseCSV2(const std::string &csvSource, std::vector<ColDefNode> &cols_def, Table& table) {
int row_cnt = 0;
bool inQuote(false);
bool newLine(false);
std::string field;
int CsvReader::parseCSV2(const std::string &csvSource, std::vector<ColDefNode> &cols_def, Table& table) {
int row_cnt = 0;
bool inQuote(false);
bool newLine(false);
std::string field;
std::vector<std::string> line;
line.reserve(32);
std::vector<std::string> 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) {
table.create_row_from_vector(cols_def, line);
row_cnt++;
}
header_skiped = true;
field.clear();
line.clear();
newLine = true;
}
}
} else {
newLine = false;
field.push_back(*aChar);
}
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) {
field += *aChar;
} else {
line.push_back(field);
field.clear();
}
} else if (*aChar == line_separator || *aChar == line_separator2) {
if (inQuote) {
field += *aChar;
} else {
if (!newLine) {
line.push_back(field);
if (header_skiped) {
table.create_row_from_vector(cols_def, line);
row_cnt++;
}
header_skiped = true;
field.clear();
line.clear();
newLine = true;
}
}
} else {
newLine = false;
field.push_back(*aChar);
}
aChar++;
}
aChar++;
}
if (!field.empty()) line.push_back(field);
if (!field.empty()) line.push_back(field);
if (header_skiped) {
table.create_row_from_vector(cols_def, line);