diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-07 09:25:50 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-07 09:25:50 +0100 |
commit | f2b544edddebf0701bad20a889197e650ffa1e56 (patch) | |
tree | 54e5d863b27a397a560abbedae32c2a598d3c44e /apps/SoilMoistureIo.h | |
parent | 1cd0616a0ca9ecffcd1b523f1fc069a22fa4609d (diff) | |
download | ble-toys-f2b544edddebf0701bad20a889197e650ffa1e56.tar.gz ble-toys-f2b544edddebf0701bad20a889197e650ffa1e56.tar.bz2 ble-toys-f2b544edddebf0701bad20a889197e650ffa1e56.tar.xz ble-toys-f2b544edddebf0701bad20a889197e650ffa1e56.zip |
o First start of a proper CSV parser.
Diffstat (limited to 'apps/SoilMoistureIo.h')
-rw-r--r-- | apps/SoilMoistureIo.h | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h index 739758a..aff7c85 100644 --- a/apps/SoilMoistureIo.h +++ b/apps/SoilMoistureIo.h @@ -4,24 +4,54 @@ #include <ostream> #include <vector> #include <map> +#include <memory> +#include <boost/asio/buffer.hpp> +#include <functional> namespace trygvis { namespace soil_moisture { using namespace std; +using namespace boost::asio; -class SampleOutputStream { +class Sample { public: - typedef map<string, string> it; + Sample() : entries() { + } + + Sample(map<string, string> entries) : entries(entries) { + } + + map<string, string>::iterator find(string &s) { + return entries.find(s); + } + + map<string, string>::iterator begin() { + return entries.begin(); + } + + map<string, string>::iterator end() { + return entries.end(); + } - virtual void write(it values) = 0; + string& operator[](string key) { + return entries[key]; + } + +private: + map<string, string> entries; +}; + +class SampleOutputStream { +public: + virtual void write(Sample sample) = 0; }; class CsvSampleOutputStream : public SampleOutputStream { public: CsvSampleOutputStream(ostream& stream, vector<string> fields); - void write(it values); + void write(Sample values); private: ostream& stream; @@ -32,7 +62,7 @@ class JsonSampleOutputStream : public SampleOutputStream { public: JsonSampleOutputStream(ostream& stream, vector<string> fields); - void write(it values); + void write(Sample values); private: ostream& stream; @@ -43,13 +73,29 @@ class SqlSampleOutputStream : public SampleOutputStream { public: SqlSampleOutputStream(ostream& stream, vector<string> fields); - void write(it values); + void write(Sample values); private: ostream& stream; vector<string> fields; }; +class CsvParser { + +public: + CsvParser(shared_ptr<SampleOutputStream> output) : output(output), line(make_shared<vector<uint8_t>>()) { + } + + void process(mutable_buffers_1 buffer); + +private: + void process_line(shared_ptr<vector<uint8_t>> packet); + + static const uint8_t packet_delimiter = '\n'; + shared_ptr<SampleOutputStream> output; + shared_ptr<vector<uint8_t>> line; +}; + } } |