From c56840f03cf139d60c6d90b55cf16e70f6ae2bc2 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 00:15:04 +0200 Subject: o Going all header file based and single-executable to launch all apps. o Ading CMake magic to generate the launcher --- apps/sample-convert.h | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 apps/sample-convert.h (limited to 'apps/sample-convert.h') diff --git a/apps/sample-convert.h b/apps/sample-convert.h new file mode 100644 index 0000000..0b805be --- /dev/null +++ b/apps/sample-convert.h @@ -0,0 +1,121 @@ +#include "trygvis/sensor.h" +#include "trygvis/sensor/io.h" +#include "json.hpp" +#include "apps.h" +#include +#include + +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 + ("help", "produce this help message") + ("input", po::value(&input_file)->default_value("-")) +// ("input-format", po::value(&input_format)->default_value("csv")) + ("output", po::value(&output_file)->default_value("-")) + ("output-format", po::value(&output_format)->default_value(sample_format_type::KEY_VALUE)) + ("fields", po::value(&fields)) + ("add-timestamp", po::value(&add_timestamp)->default_value(true)) + ("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; + } +}; + +} +} -- cgit v1.2.3