From ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 19 Jul 2015 21:39:28 +0200 Subject: o Going back to a bunch of cpp files instead of launcher+bunch of header files. This ends up with an easier build file and faster builds with CMake's "OBJECT" library type. --- apps/sample-convert.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 apps/sample-convert.cpp (limited to 'apps/sample-convert.cpp') diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp new file mode 100644 index 0000000..f592291 --- /dev/null +++ b/apps/sample-convert.cpp @@ -0,0 +1,124 @@ +#include "trygvis/sensor.h" +#include "trygvis/sensor/io.h" +#include "json.hpp" +#include "apps.h" +#include +#include +#include "apps.h" + +namespace trygvis { +namespace apps { + +using namespace std; +using namespace trygvis::apps; +using namespace trygvis::sensor; +using namespace trygvis::sensor::io; +using boost::tokenizer; +namespace po = boost::program_options; + +class sample_convert : public app { +private: + string fields; + string timestamp_field; + bool add_timestamp; + string input_file, output_file; + sample_format_type output_format; + + string table_name; + +public: + sample_convert() : app("sample-convert") {} + + ~sample_convert() = default; + + void add_options(po::options_description_easy_init &options) override { + options("input", po::value(&input_file)->default_value("-")); + // ("input-format", po::value(&input_format)->default_value("csv")) + options("output", po::value(&output_file)->default_value("-")); + options("output-format", + po::value(&output_format)->default_value(sample_format_type::KEY_VALUE)); + options("fields", po::value(&fields)); + options("add-timestamp", po::value(&add_timestamp)->default_value(true)); + options("timestamp-field", po::value(×tamp_field)->default_value("timestamp")); + } + + void add_extra_options(po::options_description &all_options) override { + po::options_description sql("SQL"); + sql.add_options()("table-name", po::value(&table_name)); + + all_options.add(sql); + }; + + int main(app_execution &execution) override { + auto desc = execution.desc; + auto vm = execution.vm; + + KeyDictionary dict; + + istream *inputStream; + if (input_file == "-") { + inputStream = &cin; + } else { + inputStream = new ifstream(input_file); + if (inputStream->fail()) { + cerr << "Unable to open input file " << input_file << endl; + return EXIT_FAILURE; + } + } + + shared_ptr outputStream; + if (output_file == "-") { + outputStream = shared_ptr(&cout, noop_deleter); + } else { + outputStream = make_shared(output_file); + if (outputStream->fail()) { + cerr << "Unable to open output file " << output_file << endl; + return EXIT_FAILURE; + } + } + + sample_output_stream_options options; + trygvis::sensor::io::timestamp_field_option tf(timestamp_field); + + options.push_back(&tf); + + table_name_option tno(table_name); + if (table_name != "") { + options.push_back(&tno); + } + + tokenizer<> tok(fields); + output_fields_option fs; + std::copy(tok.begin(), tok.end(), std::back_inserter(fs.fields)); + if (!fs.fields.empty()) { + options.push_back(&fs); + } + + unique_ptr o = open_sample_output_stream(outputStream, dict, output_format, options); + + if (add_timestamp) { + o = make_unique(move(o), dict, timestamp_field); + } + + shared_ptr output(move(o)); + + auto input = make_shared(output, dict); + + char data[100]; + while (!inputStream->eof()) { + inputStream->get(data[0]); + auto buf = boost::asio::buffer(data, 1); + input->process(buf); + } + + return EXIT_SUCCESS; + } +}; +} +} + +int main(int argc, const char *argv[]) { + using namespace trygvis::apps; + + return real_main(new sample_convert(), argc, argv); +} -- cgit v1.2.3