#include "SoilMoistureIo.h" #include "json.hpp" #include namespace trygvis { namespace soil_moisture { using namespace std; using json = nlohmann::json; CsvSampleOutputStream::CsvSampleOutputStream(ostream &stream, vector fields) : stream(stream), fields(fields) { auto i = fields.begin(); while (true) { stream << *i; i++; if (i != fields.end()) { stream << ","; } else { break; } } stream << endl; } void CsvSampleOutputStream::write(it values) { auto i = fields.begin(); while (true) { auto field = *i; auto value = values.find(field); if (value != values.end()) { stream << value->second; } i++; if (i != fields.end()) { stream << ","; } else { break; } } stream << endl; } JsonSampleOutputStream::JsonSampleOutputStream(ostream &stream, vector fields) : stream(stream), fields(fields) { } void JsonSampleOutputStream::write(it values) { json doc; for (auto &f: fields) { auto value = values.find(f); if (value != values.end()) { doc[f] = value->second; } } stream << doc << endl; } SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, vector fields) : stream(stream), fields(fields) { } void SqlSampleOutputStream::write(it values) { auto i = fields.begin(); stringstream fs, vs; while (true) { auto field = *i; fs << field; auto value = values.find(field); if (value != values.end()) { vs << "'" << value->second << "'"; } else { vs << "NULL"; } i++; if (i != fields.end()) { fs << ","; vs << ","; } else { break; } } stream << "INSERT INTO (" << fs << ") VALUES(" << vs << ")" << endl; } } }