parsing csv reimplemented

This commit is contained in:
Martin Brodbeck 2023-01-04 09:07:12 +01:00
parent 8e62283299
commit 7223954af8

View file

@ -4,9 +4,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
using std::istringstream; using namespace std;
using std::string;
using namespace std::chrono;
bool isDST(int8_t day, int8_t month, int8_t dow) { bool isDST(int8_t day, int8_t month, int8_t dow) {
// January, february, november and december are out. // January, february, november and december are out.
@ -31,76 +29,88 @@ bool isDST(int8_t day, int8_t month, int8_t dow) {
return previousSunday < 25; return previousSunday < 25;
} }
chrono::year_month_day stringToDate(const std::string &dateStr) {
istringstream partss(dateStr);
string day_str, month_str, year_str;
getline(partss, day_str, '.');
getline(partss, month_str, '.');
getline(partss, year_str, '.');
int day = atoi(day_str.c_str());
int month = atoi(month_str.c_str());
int year = atoi(year_str.c_str());
chrono::year_month_day date{chrono::year{year}, chrono::month{(uint)month},
chrono::day{(uint)day}};
return date;
}
std::vector<std::string> split(const std::string &s, const std::string &delimiter) {
std::vector<std::string> result;
std::string::size_type start{0};
std::string::size_type pos{0};
do {
pos = s.find_first_of(delimiter, start);
result.push_back(s.substr(start, pos - start));
start = pos + 1;
} while (pos != std::string::npos);
return result;
}
std::vector<WasteDate> parseCsv(const std::string &csv) { std::vector<WasteDate> parseCsv(const std::string &csv) {
istringstream stream(csv); istringstream stream(csv);
string line{""}; string line{""};
std::vector<WasteDate> dates; vector<WasteDate> wasteDates;
// Get rid of the first line (header) // Get rid of the first line (header)
std::getline(stream, line); getline(stream, line);
while (std::getline(stream, line)) { while (getline(stream, line)) {
string delimiter = ";"; auto tokenVec = split(line, ";");
size_t pos = 0;
string token;
uint tokenPos = 0;
while ((pos = line.find_first_of(delimiter)) != std::string::npos) {
token = line.substr(0, pos);
if (token.length() > 0) { for (int i = 0; i < tokenVec.size(); i++) {
istringstream liness(token); string token = tokenVec.at(i);
string day_str, month_str, year_str;
getline(liness, day_str, '.');
getline(liness, month_str, '.');
getline(liness, year_str, '.');
int day = atoi(day_str.c_str());
int month = atoi(month_str.c_str());
int year = atoi(year_str.c_str());
year_month_day date{std::chrono::year{year}, std::chrono::month{(uint)month},
std::chrono::day{(uint)day}};
std::vector<WasteDate>::iterator it; if (token.length() == 0)
continue;
it = std::find_if(dates.begin(), dates.end(), auto date = stringToDate(token);
[&date](const WasteDate &x) { return date == x.date; });
if (it == dates.end()) {
WasteDate wd;
wd.date = date;
dates.push_back(wd);
it = std::prev(dates.end());
}
switch (tokenPos) { // Take existing date or create a new one.
case 0: std::vector<WasteDate>::iterator it;
it->wasteTypes.push_back(Waste::GelberSack); it = std::find_if(wasteDates.begin(), wasteDates.end(),
break; [&date](const WasteDate &x) { return date == x.date; });
case 1: if (it == wasteDates.end()) {
it->wasteTypes.push_back(Waste::Papiertonne); WasteDate wd;
break; wd.date = date;
case 2: wasteDates.push_back(wd);
it->wasteTypes.push_back(Waste::Biotonne); it = std::prev(wasteDates.end());
break;
case 3:
it->wasteTypes.push_back(Waste::Restmuell);
break;
case 4:
it->wasteTypes.push_back(Waste::Problemstoffmobil);
break;
default:
#ifdef DEBUG
printf("Unknown waste token detected.\n");
#endif
break;
}
} }
line.erase(0, pos + delimiter.length()); switch (i) {
++tokenPos; case 0:
it->wasteTypes.push_back(Waste::GelberSack);
break;
case 1:
it->wasteTypes.push_back(Waste::Papiertonne);
break;
case 2:
it->wasteTypes.push_back(Waste::Biotonne);
break;
case 3:
it->wasteTypes.push_back(Waste::Restmuell);
break;
case 4:
it->wasteTypes.push_back(Waste::Problemstoffmobil);
break;
default:
#ifdef DEBUG
printf("Unknown waste token detected.\n");
#endif
break;
}
} }
// std::cout << "Size left: " << line.size() << " - " << line << std::endl;
} }
return dates; return wasteDates;
} }
int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool firstTry) { int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool firstTry) {
@ -130,7 +140,6 @@ int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool
continue; continue;
} }
flashrate = flashrate / (status + 1); flashrate = flashrate / (status + 1);
// printf("Connect status: %d %d\n", status, flashrate);
} }
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(flashrate); sleep_ms(flashrate);