From 225ed55918377ba2b53bb535cb636bfae8fc1ab4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 17 Mar 2015 23:02:23 +0100 Subject: o Renaming CsvSampleParser to KeyValueSampleParser. --- apps/SoilMoistureIo.cpp | 23 +++++++++++------------ apps/SoilMoistureIo.h | 8 ++++---- apps/sample-convert.cpp | 2 +- apps/sm-serial-read.cpp | 6 +++--- test/SoilMoistureIoTest.cpp | 8 ++++---- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp index c9cfa1e..40f2f7a 100644 --- a/apps/SoilMoistureIo.cpp +++ b/apps/SoilMoistureIo.cpp @@ -174,7 +174,7 @@ void SqlSampleOutputStream::write(SampleRecord values) { // (*stream.get()) << "INSERT INTO " << table_name << "(" << fs << ") VALUES(" << vs << ");" << endl; } -void CsvSampleParser::process(mutable_buffers_1 buffer) { +void KeyValueSampleParser::process(mutable_buffers_1 buffer) { size_t size = buffer_size(buffer); @@ -199,14 +199,14 @@ void CsvSampleParser::process(mutable_buffers_1 buffer) { } -void CsvSampleParser::process_line(shared_ptr> packet) { +void KeyValueSampleParser::process_line(shared_ptr> packet) { auto timestamp = std::chrono::system_clock::now().time_since_epoch().count(); auto s = std::string((char *) packet->data(), packet->size()); static const boost::regex e("([_a-zA-Z0-9]+)=([0-9]+)"); - std::string::const_iterator start = s.begin(); - std::string::const_iterator end = s.end(); + auto start = s.cbegin(); + auto end = s.cend(); boost::match_results what; boost::match_flag_type flags = boost::match_default; @@ -217,9 +217,6 @@ void CsvSampleParser::process_line(shared_ptr> packet) { auto value = static_cast(what[2]); start = what[0].second; - - - auto key = dict.indexOf(name); sample.set(key, value); @@ -231,10 +228,10 @@ void CsvSampleParser::process_line(shared_ptr> packet) { } AutoSampleParser::AutoSampleParser(shared_ptr output, KeyDictionary &dict) : - SampleStreamParser(sample_format_type::AUTO), csvParser(new CsvSampleParser(output, dict)) { + SampleStreamParser(sample_format_type::AUTO), keyValueParser(new KeyValueSampleParser(output, dict)) { // Directly select the parser now until we have more than one parser - parser = std::move(csvParser); - type_ = sample_format_type::CSV; + parser = std::move(keyValueParser); + type_ = sample_format_type::KEY_VALUE; } void AutoSampleParser::process(mutable_buffers_1 buffer) { @@ -252,6 +249,8 @@ string to_string(const sample_format_type &arg) { return "csv"; else if (arg == sample_format_type::JSON) return "json"; + else if (arg == sample_format_type::KEY_VALUE) + return "key-value"; else if (arg == sample_format_type::SQL) return "sql"; else @@ -262,8 +261,8 @@ unique_ptr open_sample_input_stream( shared_ptr output, KeyDictionary &dict, sample_format_type type) { - if (type == sample_format_type::CSV) { - return make_unique(output, dict); + if (type == sample_format_type::KEY_VALUE) { + return make_unique(output, dict); } else if (type == sample_format_type::AUTO) { return make_unique(output, dict); } else { diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index 0d93d2a..5324114 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -24,6 +24,7 @@ using o = boost::optional; enum class sample_format_type { AUTO, CSV, + KEY_VALUE, JSON, SQL }; @@ -296,11 +297,10 @@ protected: } }; -// TODO: rename to "KeyValueParser", this is not CSV. -class CsvSampleParser : public SampleStreamParser { +class KeyValueSampleParser : public SampleStreamParser { public: - CsvSampleParser(shared_ptr output, KeyDictionary &dict) : + KeyValueSampleParser(shared_ptr output, KeyDictionary &dict) : SampleStreamParser(sample_format_type::CSV), output(output), dict(dict), line(make_shared>()) { } @@ -322,7 +322,7 @@ public: private: unique_ptr parser; - unique_ptr csvParser; + unique_ptr keyValueParser; public: virtual void process(mutable_buffers_1 buffer); }; diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index c5e9c4f..79d6f1c 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -78,7 +78,7 @@ public: return EXIT_FAILURE; } - auto input = make_shared(output, dict); + auto input = make_shared(output, dict); char data[100]; while (!inputStream->eof()) { diff --git a/apps/sm-serial-read.cpp b/apps/sm-serial-read.cpp index 0d0bfe6..10981e0 100644 --- a/apps/sm-serial-read.cpp +++ b/apps/sm-serial-read.cpp @@ -59,7 +59,7 @@ string hostname = get_hostname(); class port_handler { public: - port_handler(string device, serial_port &serial_port, shared_ptr input) : + port_handler(string device, serial_port &serial_port, shared_ptr input) : device(device), port(serial_port), input(input) { } @@ -84,7 +84,7 @@ private: uint8_t data[size]; mutable_buffers_1 buffer = boost::asio::buffer(data, size); - shared_ptr input; + shared_ptr input; }; class sm_serial_read : public app { @@ -136,7 +136,7 @@ public: return EXIT_FAILURE; } - shared_ptr input = make_shared(output, dict); + shared_ptr input = make_shared(output, dict); port_handler(port_name, port, input).run(); diff --git a/test/SoilMoistureIoTest.cpp b/test/SoilMoistureIoTest.cpp index 4a508b9..3e62b05 100644 --- a/test/SoilMoistureIoTest.cpp +++ b/test/SoilMoistureIoTest.cpp @@ -6,12 +6,12 @@ using namespace trygvis::soil_moisture; -BOOST_AUTO_TEST_CASE(csv_parser) { +BOOST_AUTO_TEST_CASE(key_value_parser) { KeyDictionary dict; auto buffer = make_shared(); - auto parser = new CsvSampleParser(buffer, dict); + auto parser = new KeyValueSampleParser(buffer, dict); char data[] = "a=1, b=2, c=3\n"; parser->process(boost::asio::buffer(data, sizeof(data))); @@ -26,7 +26,7 @@ BOOST_AUTO_TEST_CASE(csv_parser) { BOOST_CHECK_EQUAL((*it++)->index, 2); } -BOOST_AUTO_TEST_CASE(csv_parser_with_custom_dict) { +BOOST_AUTO_TEST_CASE(key_value_parser_with_custom_dict) { KeyDictionary dict; dict.indexOf("c"); @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(csv_parser_with_custom_dict) { auto buffer = make_shared(); - auto parser = new CsvSampleParser(buffer, dict); + auto parser = new KeyValueSampleParser(buffer, dict); char data[] = "a=1, b=2, c=3\n"; parser->process(boost::asio::buffer(data, sizeof(data))); -- cgit v1.2.3