aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoistureIo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SoilMoistureIo.cpp')
-rw-r--r--apps/SoilMoistureIo.cpp81
1 files changed, 77 insertions, 4 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp
index 40f2f7a..c7f3504 100644
--- a/apps/SoilMoistureIo.cpp
+++ b/apps/SoilMoistureIo.cpp
@@ -121,6 +121,54 @@ void JsonSampleOutputStream::write(SampleRecord sample) {
*stream.get() << doc << endl;
}
+KeyValueSampleOutputStream::KeyValueSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict) :
+ dict(dict), stream(move(stream)) {
+}
+
+void KeyValueSampleOutputStream::write(SampleRecord sample) {
+ // Skip empty records
+ if (sample.empty()) {
+ return;
+ }
+
+ auto &s = *stream.get();
+
+ bool first = true;
+ if (!dict.empty()) {
+ for (auto &key: dict) {
+ auto sampleKey = sample.dict.indexOf(key->name);
+
+ auto value = sample.at(sampleKey);
+
+ if (value) {
+ if (first) {
+ first = false;
+ } else {
+ s << ", ";
+ }
+ s << key->name << "=" << value;
+ }
+ }
+ } else {
+ for (auto &sampleKey: sample.dict) {
+ auto o = sample.at(sampleKey);
+
+ if (o) {
+ if (first) {
+ first = false;
+ } else {
+ s << ", ";
+ }
+ // Make sure that the key is registered in the dictionary
+ dict.indexOf(sampleKey->name);
+ s << sampleKey->name << "=" << o.get();
+ }
+ }
+ }
+
+ *stream.get() << endl;
+}
+
SqlSampleOutputStream::SqlSampleOutputStream(shared_ptr<ostream> stream, KeyDictionary &dict, string table_name) :
dict(dict), stream(move(stream)), table_name(table_name) {
}
@@ -257,6 +305,29 @@ string to_string(const sample_format_type &arg) {
throw std::runtime_error("Unknown format value: " + to_string(arg));
}
+std::ostream& operator<<(std::ostream& os, sample_format_type const& type){
+ return os << to_string(type);
+}
+
+std::istream& operator>>(std::istream& is, sample_format_type& type){
+ string s;
+ is >> s;
+
+ if (s == "auto") {
+ type = sample_format_type::AUTO;
+ } else if (s == "csv") {
+ type = sample_format_type::CSV;
+ } else if (s == "key-value") {
+ type = sample_format_type::KEY_VALUE;
+ } else if (s == "json") {
+ type = sample_format_type::JSON;
+ } else if (s == "sql") {
+ type = sample_format_type::SQL;
+ }
+
+ return is;
+}
+
unique_ptr<SampleStreamParser> open_sample_input_stream(
shared_ptr<SampleOutputStream> output,
KeyDictionary &dict,
@@ -266,7 +337,7 @@ unique_ptr<SampleStreamParser> open_sample_input_stream(
} else if (type == sample_format_type::AUTO) {
return make_unique<AutoSampleParser>(output, dict);
} else {
- throw sample_exception("Unsupported format type: " + to_string(type));
+ throw sample_exception("No parser for format type: " + to_string(type));
}
}
@@ -276,13 +347,15 @@ unique_ptr<SampleOutputStream> open_sample_output_stream(
sample_format_type type) {
if (type == sample_format_type::CSV) {
- return make_unique<CsvSampleOutputStream>(move(output), dict);
+ return make_unique<CsvSampleOutputStream>(output, dict);
+ } else if (type == sample_format_type::KEY_VALUE) {
+ return make_unique<KeyValueSampleOutputStream>(output, dict);
} else if (type == sample_format_type::JSON) {
- return make_unique<JsonSampleOutputStream>(move(output), dict);
+ return make_unique<JsonSampleOutputStream>(output, dict);
// } else if (type == sample_format_type::SQL) {
// return make_unique<SqlSampleOutputStream>(dict, move(output), table_name);
} else {
- throw sample_exception("Unsupported format type: " + to_string(type));
+ throw sample_exception("No writer for format type: " + to_string(type));
}
}