parsing csv reimplemented
This commit is contained in:
parent
8e62283299
commit
7223954af8
1 changed files with 69 additions and 60 deletions
129
src/utils.cpp
129
src/utils.cpp
|
@ -4,9 +4,7 @@
|
|||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
using std::istringstream;
|
||||
using std::string;
|
||||
using namespace std::chrono;
|
||||
using namespace std;
|
||||
|
||||
bool isDST(int8_t day, int8_t month, int8_t dow) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
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) {
|
||||
istringstream stream(csv);
|
||||
string line{""};
|
||||
std::vector<WasteDate> dates;
|
||||
vector<WasteDate> wasteDates;
|
||||
|
||||
// Get rid of the first line (header)
|
||||
std::getline(stream, line);
|
||||
getline(stream, line);
|
||||
|
||||
while (std::getline(stream, line)) {
|
||||
string delimiter = ";";
|
||||
size_t pos = 0;
|
||||
string token;
|
||||
uint tokenPos = 0;
|
||||
while ((pos = line.find_first_of(delimiter)) != std::string::npos) {
|
||||
token = line.substr(0, pos);
|
||||
while (getline(stream, line)) {
|
||||
auto tokenVec = split(line, ";");
|
||||
|
||||
if (token.length() > 0) {
|
||||
istringstream liness(token);
|
||||
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}};
|
||||
for (int i = 0; i < tokenVec.size(); i++) {
|
||||
string token = tokenVec.at(i);
|
||||
|
||||
std::vector<WasteDate>::iterator it;
|
||||
if (token.length() == 0)
|
||||
continue;
|
||||
|
||||
it = std::find_if(dates.begin(), dates.end(),
|
||||
[&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());
|
||||
}
|
||||
auto date = stringToDate(token);
|
||||
|
||||
switch (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;
|
||||
}
|
||||
// Take existing date or create a new one.
|
||||
std::vector<WasteDate>::iterator it;
|
||||
it = std::find_if(wasteDates.begin(), wasteDates.end(),
|
||||
[&date](const WasteDate &x) { return date == x.date; });
|
||||
if (it == wasteDates.end()) {
|
||||
WasteDate wd;
|
||||
wd.date = date;
|
||||
wasteDates.push_back(wd);
|
||||
it = std::prev(wasteDates.end());
|
||||
}
|
||||
|
||||
line.erase(0, pos + delimiter.length());
|
||||
++tokenPos;
|
||||
switch (i) {
|
||||
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) {
|
||||
|
@ -130,7 +140,6 @@ int wifi_setup_impl(uint32_t country, const string &ssid, const string &pw, bool
|
|||
continue;
|
||||
}
|
||||
flashrate = flashrate / (status + 1);
|
||||
// printf("Connect status: %d %d\n", status, flashrate);
|
||||
}
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
sleep_ms(flashrate);
|
||||
|
@ -170,4 +179,4 @@ void wifi_setup() {
|
|||
}
|
||||
|
||||
} while (res != CYW43_LINK_UP);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue