Get rid of csv-parser
This commit is contained in:
parent
f4b4ccbbea
commit
e6b71a7e4d
7 changed files with 66 additions and 27 deletions
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue