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 url = https://github.com/nlohmann/json.git
[submodule "subprojects/singleapplication"] [submodule "subprojects/singleapplication"]
path = subprojects/singleapplication/singleapplication.git path = subprojects/singleapplication/singleapplication.git
url = https://github.com/itay-grudev/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

32
.vscode/settings.json vendored
View File

@ -65,7 +65,37 @@
"condition_variable": "cpp", "condition_variable": "cpp",
"mutex": "cpp", "mutex": "cpp",
"hash_map": "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", "C_Cpp.clang_format_path": "/usr/bin/clang-format",
"cmake.configureOnOpen": true, "cmake.configureOnOpen": true,

View File

@ -4,8 +4,6 @@ find_package(Boost 1.62 REQUIRED)
find_package(SQLite3 REQUIRED) find_package(SQLite3 REQUIRED)
# Because csv-parser needs threads: # Because csv-parser needs threads:
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(fmt) find_package(fmt)
@ -25,12 +23,12 @@ set(CORE_SOURCES
add_library(core STATIC ${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) 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) target_link_libraries(core PRIVATE bcrypt)
else() 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() endif()
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)

View File

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

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