diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-08 00:07:31 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-03-08 00:07:31 +0100 |
commit | 7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4 (patch) | |
tree | 21b33d8860fe6ab0ac690dbe8ea0eba00525eec4 /apps/sample-convert.cpp | |
parent | f2b544edddebf0701bad20a889197e650ffa1e56 (diff) | |
download | ble-toys-7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4.tar.gz ble-toys-7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4.tar.bz2 ble-toys-7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4.tar.xz ble-toys-7088a4a5639b27a4c82eb6f6560ec1f1fd538fd4.zip |
o Dynamic input and output types.
Diffstat (limited to 'apps/sample-convert.cpp')
-rw-r--r-- | apps/sample-convert.cpp | 105 |
1 files changed, 38 insertions, 67 deletions
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 <istream> - -enum class Format { - PLAIN, - JSON, - SQL -}; - -void validate(boost::any &v, const std::vector<std::string> &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<std::string>(arg)); -} - -} +#include <fstream> 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<Format>(&inputFormat)->default_value(Format::PLAIN)) -// ("output-format", po::value<Format>(&outputFormat)->default_value(Format::PLAIN)) -// } + void add_options(po::options_description_easy_init &options) override { + options + ("help", "produce this help message") + ("input", po::value<string>(&inputFile)->default_value("-")) + ("input-format", po::value<string>(&inputFormat)->default_value("csv")) + ("output", po::value<string>(&outputFile)->default_value("-")) + ("output-format", po::value<string>(&outputFormat)->default_value("plain")); + } int main(app_execution &execution) override { auto desc = execution.desc; @@ -67,31 +32,36 @@ public: shared_ptr<SampleOutputStream> output; - auto field_names = vector<string>({ - "hostname", - "device_type", - "device", - "timestamp", - "sensor", - "value" - }); - - field_names = vector<string>({ - "analog", - "dry", - "water", - "last_watering_started", - "last_watering_stopped", - "now" - }); - - output = make_shared<JsonSampleOutputStream>(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<CsvSampleOutputStream>(*outputStream); + } else if (outputFormat == "json") { + output = make_shared<JsonSampleOutputStream>(*outputStream); + } else if (outputFormat == "sql") { + output = make_shared<SqlSampleOutputStream>(*outputStream); + } else { + cerr << "Unsupported output format: " << outputFormat << endl; + return EXIT_FAILURE; + } auto input = make_shared<CsvParser>(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); |