From e6b71a7e4d2bd0220d461c1bda878bef561e640c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Tue, 25 Apr 2023 16:11:34 +0200 Subject: [PATCH] Get rid of csv-parser --- .gitmodules | 5 +---- .vscode/settings.json | 32 +++++++++++++++++++++++++++++++- src/core/CMakeLists.txt | 8 +++----- src/core/csvreader.cpp | 39 +++++++++++++++++++++++---------------- src/core/utils.cpp | 7 +++++++ src/core/utils.h | 1 + subprojects/csv-parser | 1 - 7 files changed, 66 insertions(+), 27 deletions(-) delete mode 160000 subprojects/csv-parser diff --git a/.gitmodules b/.gitmodules index 672e543..934c21f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 43f0c12..41f34ac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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, diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 58b9abf..40836cf 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -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}/..) diff --git a/src/core/csvreader.cpp b/src/core/csvreader.cpp index 5e092c9..6c2cdf6 100644 --- a/src/core/csvreader.cpp +++ b/src/core/csvreader.cpp @@ -3,7 +3,8 @@ #include -#include +// #include +#include #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 strs; + boost::split(strs, line, boost::is_any_of(";")); + + auto seller = std::make_unique(); + + try { + int sellerNo = std::stoi(strs[0]); + seller->setSellerNo(sellerNo); + } catch (std::invalid_argument const &ex) { continue; } - auto seller = std::make_unique(); - seller->setSellerNo(row[0].get()); - if (row[1].is_int()) { - seller->setNumArticlesOffered(row[1].get()); - } 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().empty() && row[3].get().empty()) { + if (strs[2].empty() && strs[2].empty()) { seller->setFirstName("N."); seller->setLastName("N."); } else { - std::string firstName = row[2].get(); + std::string firstName = strs[2]; seller->setFirstName(trim(firstName)); - std::string lastName = row[3].get(); + std::string lastName = strs[3]; seller->setLastName(trim(lastName)); } diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 75242f3..1e4b9c6 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -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(); +} \ No newline at end of file diff --git a/src/core/utils.h b/src/core/utils.h index 3a9ac21..0adfd75 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -10,5 +10,6 @@ std::string <rim(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 diff --git a/subprojects/csv-parser b/subprojects/csv-parser deleted file mode 160000 index ea547fd..0000000 --- a/subprojects/csv-parser +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ea547fdb16c7baf99bd9ced5febba52cc5da3ca3