From eecddf878f98bb921622a5a5bb2600aedb5a53fa Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 8 Mar 2015 19:38:15 +0100 Subject: o Improved SQL output. --- apps/sample-convert.cpp | 57 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'apps/sample-convert.cpp') diff --git a/apps/sample-convert.cpp b/apps/sample-convert.cpp index 23a04ff..fb74fcf 100644 --- a/apps/sample-convert.cpp +++ b/apps/sample-convert.cpp @@ -11,21 +11,30 @@ using namespace trygvis::apps; using namespace trygvis::soil_moisture; namespace po = boost::program_options; -string inputFile, inputFormat; -string outputFile, outputFormat; - class sample_convert : public app { public: + sample_convert() : table_name(""),input_file(""), input_format(""), + output_file(""), output_format("") { + } + 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")); + ("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("plain")); } + 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; @@ -33,35 +42,40 @@ public: shared_ptr output; istream *inputStream; - if (inputFile == "-") { + if (input_file == "-") { inputStream = &cin; } else { - inputStream = new ifstream(inputFile); + inputStream = new ifstream(input_file); if (inputStream->fail()) { - cerr << "Unable to open input file " << inputFile << endl; + cerr << "Unable to open input file " << input_file << endl; return EXIT_FAILURE; } } ostream *outputStream; - if (outputFile == "-") { + if (output_file == "-") { outputStream = &cout; } else { - outputStream = new ofstream(outputFile); + outputStream = new ofstream(output_file); if (outputStream->fail()) { - cerr << "Unable to open output file " << outputFile << endl; + cerr << "Unable to open output file " << output_file << endl; return EXIT_FAILURE; } } - if (outputFormat == "plain") { + if (output_format == "plain") { output = make_shared(*outputStream); - } else if (outputFormat == "json") { + } else if (output_format == "json") { output = make_shared(*outputStream); - } else if (outputFormat == "sql") { - output = make_shared(*outputStream); + } else if (output_format == "sql") { + if (table_name.size() == 0) { + cerr << "Missing option: table-name" << endl; + return EXIT_FAILURE; + } + + output = make_shared(*outputStream, table_name); } else { - cerr << "Unsupported output format: " << outputFormat << endl; + cerr << "Unsupported output format: " << output_format << endl; return EXIT_FAILURE; } @@ -73,9 +87,16 @@ public: input->process(boost::asio::buffer(data, 1)); } + delete outputStream; + return EXIT_SUCCESS; } +private: + string input_file, input_format; + string output_file, output_format; + + string table_name; }; } -- cgit v1.2.3