diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-06 21:07:34 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-06 21:07:34 +0100 |
commit | a83e6f5960a549d54991495336bd12d549127d91 (patch) | |
tree | d21f63b718d65074215fff6edbbcdbd5f26caf4a /apps/SoilMoistureIo.cpp | |
parent | e03bf5d416776cb5ea27c7354657920939c04e71 (diff) | |
download | ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.gz ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.bz2 ble-toys-a83e6f5960a549d54991495336bd12d549127d91.tar.xz ble-toys-a83e6f5960a549d54991495336bd12d549127d91.zip |
o Starting on a tool to convert between sample formats.
Diffstat (limited to 'apps/SoilMoistureIo.cpp')
-rw-r--r-- | apps/SoilMoistureIo.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp new file mode 100644 index 0000000..1280fc6 --- /dev/null +++ b/apps/SoilMoistureIo.cpp @@ -0,0 +1,107 @@ +#include "SoilMoistureIo.h" + +#include "json.hpp" +#include <set> + +namespace trygvis { +namespace soil_moisture { + +using namespace std; +using json = nlohmann::json; + +CsvSampleOutputStream::CsvSampleOutputStream(ostream &stream, vector<string> 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<string> 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<string> 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; +} + +} +} |