diff options
Diffstat (limited to 'apps/sample-add-timestamp.cpp')
-rw-r--r-- | apps/sample-add-timestamp.cpp | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/apps/sample-add-timestamp.cpp b/apps/sample-add-timestamp.cpp index 8bc4bac..841782e 100644 --- a/apps/sample-add-timestamp.cpp +++ b/apps/sample-add-timestamp.cpp @@ -1,8 +1,6 @@ #include "trygvis/sensor.h" #include "trygvis/sensor/io.h" #include "apps.h" -#include <vector> -#include "apps.h" namespace trygvis { namespace apps { @@ -13,10 +11,10 @@ using namespace trygvis::sensor; using namespace trygvis::sensor::io; namespace po = boost::program_options; -class TimestampAddingSampleOutputStream : public SampleOutputStream { +class TimestampAddingSampleOutputStream : public SampleConsumer { public: - TimestampAddingSampleOutputStream(shared_ptr<SampleOutputStream> output, KeyDictionary &dict, string timestamp_name) - : timestamp_key(dict.indexOf(timestamp_name)) { + TimestampAddingSampleOutputStream(shared_ptr<SampleConsumer> output, KeyDictionary &dict, string timestamp_name) + : output_(output), timestamp_key(dict.indexOf(timestamp_name)) { if (input_time_resolution_ == time_resolution::MILLISECONDS) { factor = 1000; } else { @@ -24,21 +22,21 @@ public: } } - virtual void write(SampleRecord const &sample) override { + virtual void onSample(const SampleRecord &sample) override { time_t now = time(NULL) * factor; SampleRecord updated_sample(sample); updated_sample.set(timestamp_key, std::to_string(now)); - output_->write(updated_sample); + output_->onSample(updated_sample); }; private: const SampleKey *timestamp_key; - time_resolution input_time_resolution_; + const time_resolution input_time_resolution_ = time_resolution::MILLISECONDS; int factor; - shared_ptr<SampleOutputStream> output_; + shared_ptr<SampleConsumer> output_; }; class sample_add_timestamp : public app { @@ -62,25 +60,22 @@ public: const int buffer_size = 1024; int main(app_execution &execution) override { - shared_ptr<istream> input; - - input = shared_ptr<istream>(&cin, noop_deleter); + auto stdin = shared_ptr<istream>(&cin, noop_deleter); + auto stdout = shared_ptr<ostream>(&cout, noop_deleter); KeyDictionary dict; sample_output_stream_options options = {}; - auto unique_output_stream = - open_sample_output_stream(shared_ptr<ostream>(&cout, noop_deleter), dict, output_format, options); - shared_ptr<SampleOutputStream> output_stream{std::move(unique_output_stream)}; - auto p = make_shared<TimestampAddingSampleOutputStream>(output_stream, dict, timestamp_name); + auto writer = open_sample_writer(stdout, dict, output_format, options); + auto p = make_shared<TimestampAddingSampleOutputStream>(std::move(writer), dict, timestamp_name); auto parser = open_sample_stream_parser(p, dict); int recordCount = 0; - while (!input->eof()) { + while (!stdin->eof()) { char buffer[buffer_size]; - input->read(buffer, buffer_size); - size_t gcount = (size_t) input->gcount(); + stdin->read(buffer, buffer_size); + auto gcount = static_cast<size_t>(stdin->gcount()); recordCount++; |