Get rid of csv-parser

This commit is contained in:
Martin Brodbeck 2023-04-25 16:11:34 +02:00
parent f4b4ccbbea
commit e6b71a7e4d
7 changed files with 66 additions and 27 deletions

5
.gitmodules vendored
View File

@ -3,7 +3,4 @@
url = https://github.com/nlohmann/json.git
[submodule "subprojects/singleapplication"]
path = subprojects/singleapplication/singleapplication.git
url = https://github.com/itay-grudev/SingleApplication.git
[submodule "subprojects/csv-parser"]
path = subprojects/csv-parser
url = https://github.com/vincentlaucsb/csv-parser.git
url = https://github.com/itay-grudev/SingleApplication.git

32
.vscode/settings.json vendored
View File

@ -65,7 +65,37 @@
"condition_variable": "cpp",
"mutex": "cpp",
"hash_map": "cpp",
"future": "cpp"
"future": "cpp",
"bit": "cpp",
"compare": "cpp",
"concepts": "cpp",
"forward_list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_set": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"random": "cpp",
"semaphore": "cpp",
"stop_token": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"__verbose_abort": "cpp",
"format": "cpp",
"ios": "cpp",
"locale": "cpp"
},
"C_Cpp.clang_format_path": "/usr/bin/clang-format",
"cmake.configureOnOpen": true,

View File

@ -4,8 +4,6 @@ find_package(Boost 1.62 REQUIRED)
find_package(SQLite3 REQUIRED)
# Because csv-parser needs threads:
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(fmt)
@ -25,12 +23,12 @@ set(CORE_SOURCES
add_library(core STATIC ${CORE_SOURCES})
target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/subprojects/csv-parser/single_include)
#target_include_directories(core PRIVATE ${PROJECT_SOURCE_DIR}/subprojects/csv-parser/single_include)
if (WIN32)
target_link_libraries(core PRIVATE sqlite3 nlohmann_json::nlohmann_json Threads::Threads fmt::fmt)
target_link_libraries(core PRIVATE sqlite3 nlohmann_json::nlohmann_json fmt::fmt)
target_link_libraries(core PRIVATE bcrypt)
else()
target_link_libraries(core PRIVATE sqlite3 nlohmann_json::nlohmann_json Threads::Threads fmt::fmt)
target_link_libraries(core PRIVATE sqlite3 nlohmann_json::nlohmann_json fmt::fmt)
endif()
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)

View File

@ -3,7 +3,8 @@
#include <fstream>
#include <csv.hpp>
// #include <csv.hpp>
#include <boost/algorithm/string.hpp>
#ifdef DELETE
#undef DELETE
@ -13,17 +14,16 @@ namespace fs = std::filesystem;
std::size_t CsvReader::readSellersFromFile(const fs::path &filePath, Marketplace *market)
{
csv::CSVFormat format;
format.delimiter(';');
#if defined(_WIN64) || defined(_WIN32)
// Windows: Somhow this is necessary in order to open file names with umlauts
auto wide = filePath.wstring();
std::string fileName(wide.begin(), wide.end());
csv::CSVReader csvReader(fileName, format);
std::ifstream infile(fileName);
#else
csv::CSVReader csvReader(filePath.string(), format);
// csv::CSVReader csvReader(filePath.string(), format);
std::ifstream infile(filePath.string());
#endif
for (auto &seller : market->getSellers()) {
@ -32,28 +32,35 @@ std::size_t CsvReader::readSellersFromFile(const fs::path &filePath, Marketplace
market->storeToDb(true);
for (csv::CSVRow &row : csvReader) {
if (!row[0].is_int()) {
std::string line;
while (getline(infile, line)) {
std::vector<std::string> strs;
boost::split(strs, line, boost::is_any_of(";"));
auto seller = std::make_unique<Seller>();
try {
int sellerNo = std::stoi(strs[0]);
seller->setSellerNo(sellerNo);
} catch (std::invalid_argument const &ex) {
continue;
}
auto seller = std::make_unique<Seller>();
seller->setSellerNo(row[0].get<int>());
if (row[1].is_int()) {
seller->setNumArticlesOffered(row[1].get<int>());
} else {
if (isNumber(strs[1]))
seller->setNumArticlesOffered(std::stoi(strs[1]));
else
seller->setNumArticlesOffered(0);
}
// If both, first name and last name, are empty, use N. N.
// Else, use the real values.
if (row[2].get<std::string>().empty() && row[3].get<std::string>().empty()) {
if (strs[2].empty() && strs[2].empty()) {
seller->setFirstName("N.");
seller->setLastName("N.");
} else {
std::string firstName = row[2].get<std::string>();
std::string firstName = strs[2];
seller->setFirstName(trim(firstName));
std::string lastName = row[3].get<std::string>();
std::string lastName = strs[3];
seller->setLastName(trim(lastName));
}

View File

@ -61,3 +61,10 @@ bool case_insensitive_match(std::string s1, std::string s2)
return true; // The strings are same
return false; // not matched
}
bool isNumber(const std::string &str)
{
return !str.empty() && std::find_if(str.begin(), str.end(), [](unsigned char c) {
return !std::isdigit(c);
}) == str.end();
}

View File

@ -10,5 +10,6 @@ std::string &ltrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r ");
bool case_insensitive_match(std::string s1, std::string s2);
bool isNumber(const std::string &str);
#endif

@ -1 +0,0 @@
Subproject commit ea547fdb16c7baf99bd9ced5febba52cc5da3ca3