diff options
Diffstat (limited to 'apps/sample-timestamp.cpp')
-rw-r--r-- | apps/sample-timestamp.cpp | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/apps/sample-timestamp.cpp b/apps/sample-timestamp.cpp index 3a0b3e0..6ac2f86 100644 --- a/apps/sample-timestamp.cpp +++ b/apps/sample-timestamp.cpp @@ -14,21 +14,27 @@ namespace po = boost::program_options; class TimestampFixingSampleOutputStream : public SampleOutputStream { public: - TimestampFixingSampleOutputStream(string timestamp_name, string now_name, time_t start_time, shared_ptr<SampleOutputStream> output) : - timestamp_name_(timestamp_name), now_name_(now_name), start_time_(start_time), output_(output) { + TimestampFixingSampleOutputStream(KeyDictionary dict, string timestamp_name, string now_name, time_t start_time, shared_ptr<SampleOutputStream> output) : + timestamp_index(dict.indexOf(timestamp_name)), now_index(dict.indexOf(now_name)), start_time_(start_time), output_(output) { } - virtual void write(Sample sample) override { - long relative_time = sample.lexical_at<long>(now_name_); + virtual void write(SampleRecord sample) override { + o<long> relative_time_o = sample.lexical_at<long>(now_index); + + if (!relative_time_o) { + return; + } + + long relative_time = relative_time_o.get(); string new_value = std::to_string(start_time_ + relative_time); - sample.set(timestamp_name_, new_value); + sample.set(timestamp_index, new_value); output_->write(sample); }; private: - string now_name_, timestamp_name_; + KeyDictionary::index_t now_index, timestamp_index; time_t start_time_; shared_ptr<SampleOutputStream> output_; }; @@ -37,6 +43,7 @@ class sample_timestamp : public app { private: string input_file, timestamp_name, now_name; + KeyDictionary::index_t now_index; public: sample_timestamp() : input_file("") { @@ -69,19 +76,18 @@ public: return EXIT_FAILURE; } + KeyDictionary dict; + + now_index = dict.indexOf(now_name); + auto sample_buffer = make_shared<VectorSampleOutputStream>(); - unique_ptr<SampleStreamParser> parser = open_sample_input_stream(sample_buffer); - while (!input.eof()) { + unique_ptr<SampleStreamParser> parser = open_sample_input_stream(dict, sample_buffer); + while (!input.fail()) { char buffer[buffer_size]; input.read(buffer, buffer_size); + auto count = (size_t) input.gcount(); - if (input.bad()) { - cerr << "Error reading input" << endl; - return EXIT_FAILURE; - } - - size_t count = (size_t) input.gcount(); - + cerr << "eof? " << input.eof() << endl; mutable_buffers_1 b = boost::asio::buffer(buffer, count); parser->process(b); } @@ -93,21 +99,20 @@ public: time_t end_time = buf.st_mtim.tv_sec; - Sample sample = *--sample_buffer->samples.end(); + SampleRecord sample = *--sample_buffer->samples.end(); - string s; - try { - s = sample.at(now_name); - } catch (out_of_range &e) { + o<string> s = sample.at(now_index); + if (!s) { cerr << "Missing key '" + now_name + "'." << endl; + cerr << "keys: " << sample.to_string() << endl; return EXIT_FAILURE; } long now; try { - now = boost::lexical_cast<long>(s); + now = boost::lexical_cast<long>(s.get()); } catch (const boost::bad_lexical_cast &e) { - cerr << "Bad integer value '" + s + "'." << endl; + cerr << "Bad integer value '" + s.get() + "'." << endl; return EXIT_FAILURE; } @@ -124,16 +129,16 @@ public: return EXIT_FAILURE; } - auto output_stream = open_sample_output_stream(parser->type(), unique_ptr<ostream>(&cout)); - auto p = make_shared<TimestampFixingSampleOutputStream>("timestamp", now_name, start_time, move(output_stream)); - parser = open_sample_input_stream(p, parser->type()); + auto output_stream = open_sample_output_stream(dict, parser->type(), unique_ptr<ostream>(&cout)); + auto p = make_shared<TimestampFixingSampleOutputStream>(dict, "timestamp", now_name, start_time, move(output_stream)); + parser = open_sample_input_stream(dict, p, parser->type()); int recordCount = 0; while (!input.eof()) { char buffer[buffer_size]; - - size_t gcount = (size_t)input.readsome(buffer, buffer_size); + input.read(buffer, buffer_size); + size_t gcount = (size_t)input.gcount(); recordCount++; |