From 7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 8 Mar 2015 00:07:31 +0100 Subject: o Dynamic input and output types. --- apps/sample-convert.cpp | 105 ++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 67 deletions(-) (limited to 'apps/sample-convert.cpp') diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index 9a0627d..370059c 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -1,45 +1,7 @@ #include "SoilMoistureIo.h" #include "json.hpp" #include "apps.h" -#include - -enum class Format { - PLAIN, - JSON, - SQL -}; - -void validate(boost::any &v, const std::vector &values, Format *, int) { - using namespace boost::program_options; - - const std::string &s = validators::get_single_string(values); - - if (s == "plain") { - v = boost::any(Format::PLAIN); - } else if (s == "json") { - v = boost::any(Format::JSON); - } else if (s == "sql") { - v = boost::any(Format::SQL); - } else { - throw validation_error(validation_error::invalid_option_value); - } -} - -namespace boost { - -template<> -std::string lexical_cast(const Format &arg) { - if (arg == Format::PLAIN) - return "plain"; - else if (arg == Format::JSON) - return "json"; - else if (arg == Format::SQL) - return "sql"; - else - throw std::runtime_error("Unknown format value: " + lexical_cast(arg)); -} - -} +#include namespace trygvis { namespace apps { @@ -49,17 +11,20 @@ using namespace trygvis::apps; using namespace trygvis::soil_moisture; namespace po = boost::program_options; -Format inputFormat, outputFormat; +string inputFile, inputFormat; +string outputFile, outputFormat; class sample_convert : public app { public: -// void add_options(po::options_description_easy_init &options) override { -// options -// ("help", "produce help message") -// ("input-format", po::value(&inputFormat)->default_value(Format::PLAIN)) -// ("output-format", po::value(&outputFormat)->default_value(Format::PLAIN)) -// } + void add_options(po::options_description_easy_init &options) override { + options + ("help", "produce this help message") + ("input", po::value(&inputFile)->default_value("-")) + ("input-format", po::value(&inputFormat)->default_value("csv")) + ("output", po::value(&outputFile)->default_value("-")) + ("output-format", po::value(&outputFormat)->default_value("plain")); + } int main(app_execution &execution) override { auto desc = execution.desc; @@ -67,31 +32,36 @@ public: shared_ptr output; - auto field_names = vector({ - "hostname", - "device_type", - "device", - "timestamp", - "sensor", - "value" - }); - - field_names = vector({ - "analog", - "dry", - "water", - "last_watering_started", - "last_watering_stopped", - "now" - }); - - output = make_shared(cout, field_names); + istream *inputStream; + if (inputFile == "-") { + inputStream = &cin; + } else { + inputStream = new ifstream(inputFile); + } + + ostream *outputStream; + if (outputFile == "-") { + outputStream = &cout; + } else { + outputStream = new ofstream(outputFile); + } + + if (outputFormat == "plain") { + output = make_shared(*outputStream); + } else if (outputFormat == "json") { + output = make_shared(*outputStream); + } else if (outputFormat == "sql") { + output = make_shared(*outputStream); + } else { + cerr << "Unsupported output format: " << outputFormat << endl; + return EXIT_FAILURE; + } auto input = make_shared(output); char data[100]; - while (!cin.eof()) { - cin.get(data[0]); + while (!inputStream->eof()) { + inputStream->get(data[0]); input->process(boost::asio::buffer(data, 1)); } @@ -104,6 +74,7 @@ public: } using namespace trygvis::apps; + int main(int argc, char *argv[]) { sample_convert app; return launch_app(argc, argv, app); -- cgit v1.2.3