From 17de795175d0573d8ac6c2517d0eb35e8a4f8f69 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 18 Mar 2015 23:02:25 +0100 Subject: o Replacing custom Format with common sample_format_type. --- apps/SoilMoistureIo.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 4 deletions(-) (limited to 'apps/SoilMoistureIo.cpp') 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 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 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 open_sample_input_stream( shared_ptr output, KeyDictionary &dict, @@ -266,7 +337,7 @@ unique_ptr open_sample_input_stream( } else if (type == sample_format_type::AUTO) { return make_unique(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 open_sample_output_stream( sample_format_type type) { if (type == sample_format_type::CSV) { - return make_unique(move(output), dict); + return make_unique(output, dict); + } else if (type == sample_format_type::KEY_VALUE) { + return make_unique(output, dict); } else if (type == sample_format_type::JSON) { - return make_unique(move(output), dict); + return make_unique(output, dict); // } else if (type == sample_format_type::SQL) { // return make_unique(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)); } } -- cgit v1.2.3