parsing csv reimplemented
This commit is contained in:
parent
8e62283299
commit
7223954af8
1 changed files with 69 additions and 60 deletions
|
@ -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,46 +29,63 @@ bool isDST(int8_t day, int8_t month, int8_t dow) {
|
|||
return previousSunday < 25;
|
||||
}
|
||||
|
||||
std::vector<WasteDate> parseCsv(const std::string &csv) {
|
||||
istringstream stream(csv);
|
||||
string line{""};
|
||||
std::vector<WasteDate> dates;
|
||||
|
||||
// Get rid of the first line (header)
|
||||
std::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);
|
||||
|
||||
if (token.length() > 0) {
|
||||
istringstream liness(token);
|
||||
chrono::year_month_day stringToDate(const std::string &dateStr) {
|
||||
istringstream partss(dateStr);
|
||||
string day_str, month_str, year_str;
|
||||
getline(liness, day_str, '.');
|
||||
getline(liness, month_str, '.');
|
||||
getline(liness, 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());
|
||||
year_month_day date{std::chrono::year{year}, std::chrono::month{(uint)month},
|
||||
std::chrono::day{(uint)day}};
|
||||
|
||||
std::vector<WasteDate>::iterator it;
|
||||
|
||||
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());
|
||||
chrono::year_month_day date{chrono::year{year}, chrono::month{(uint)month},
|
||||
chrono::day{(uint)day}};
|
||||
return date;
|
||||
}
|
||||
|
||||
switch (tokenPos) {
|
||||
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{""};
|
||||
vector<WasteDate> wasteDates;
|
||||
|
||||
// Get rid of the first line (header)
|
||||
getline(stream, line);
|
||||
|
||||
while (getline(stream, line)) {
|
||||
auto tokenVec = split(line, ";");
|
||||
|
||||
for (int i = 0; i < tokenVec.size(); i++) {
|
||||
string token = tokenVec.at(i);
|
||||
|
||||
if (token.length() == 0)
|
||||
continue;
|
||||
|
||||
auto date = stringToDate(token);
|
||||
|
||||
// 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());
|
||||
}
|
||||
|
||||
switch (i) {
|
||||
case 0:
|
||||
it->wasteTypes.push_back(Waste::GelberSack);
|
||||
break;
|
||||
|
@ -93,14 +108,9 @@ std::vector<WasteDate> parseCsv(const std::string &csv) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
line.erase(0, pos + delimiter.length());
|
||||
++tokenPos;
|
||||
}
|
||||
// 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);
|
||||
|
|
Loading…
Reference in a new issue