CLI infrastructure ported from the prototype v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 08:13:35 +0200
branchv_0
changeset 10 77593735b057
parent 9 3be327f67cdd
child 11 3798b6bc9aea
CLI infrastructure ported from the prototype
CLI.h
nbproject/configurations.xml
nbproject/project.xml
relpipe-in-cli.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CLI.h	Sat Jul 28 08:13:35 2018 +0200
@@ -0,0 +1,61 @@
+#pragma once
+
+#include <locale.h>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <locale>
+#include <codecvt>
+
+using namespace std;
+
+namespace relpipe {
+namespace cli {
+
+class CLI {
+public:
+
+	CLI(int argc, char* argv[]) {
+		setlocale(LC_ALL, "");
+
+		this->argc = &argc;
+		this->argv = &argv;
+
+		program = convertor.from_bytes(argv[0]);
+
+		for (int i = 1; i < argc; i++) {
+			args.insert(args.end(), convertor.from_bytes(argv[i]));
+		}
+
+	}
+
+	CLI(const CLI& orig) {
+	}
+
+	virtual ~CLI() {
+	}
+
+	const wstring programName() {
+		return (const wstring) program;
+	}
+
+	const vector<wstring> arguments() {
+		return (const vector<wstring>)args;
+	}
+
+	static const int EXIT_CODE_SUCCESS = 0;
+	static const int EXIT_CODE_UNEXPECTED_ERROR = 1;
+	static const int EXIT_CODE_BAD_SYNTAX = 3;
+	static const int EXIT_CODE_UNKNOWN_COMMAND = 4;
+	static const int EXIT_CODE_DATA_ERROR = 5;
+
+private:
+	int* argc;
+	char*** argv;
+	wstring program;
+	vector<wstring> args;
+	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+};
+
+}
+}
--- a/nbproject/configurations.xml	Fri Jul 27 00:07:16 2018 +0200
+++ b/nbproject/configurations.xml	Sat Jul 28 08:13:35 2018 +0200
@@ -4,6 +4,7 @@
     <logicalFolder name="HeaderFiles"
                    displayName="Header Files"
                    projectFiles="true">
+      <itemPath>CLI.h</itemPath>
     </logicalFolder>
     <logicalFolder name="ResourceFiles"
                    displayName="Resource Files"
@@ -50,6 +51,8 @@
           </linkerLibItems>
         </linkerTool>
       </compileType>
+      <item path="CLI.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
@@ -79,6 +82,8 @@
           </linkerLibItems>
         </linkerTool>
       </compileType>
+      <item path="CLI.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="relpipe-in-cli.cpp" ex="false" tool="1" flavor2="0">
       </item>
     </conf>
--- a/nbproject/project.xml	Fri Jul 27 00:07:16 2018 +0200
+++ b/nbproject/project.xml	Sat Jul 28 08:13:35 2018 +0200
@@ -6,7 +6,7 @@
             <name>relpipe-in-cli.cpp</name>
             <c-extensions/>
             <cpp-extensions>cpp</cpp-extensions>
-            <header-extensions/>
+            <header-extensions>h</header-extensions>
             <sourceEncoding>UTF-8</sourceEncoding>
             <make-dep-projects/>
             <sourceRootList/>
--- a/relpipe-in-cli.cpp	Fri Jul 27 00:07:16 2018 +0200
+++ b/relpipe-in-cli.cpp	Sat Jul 28 08:13:35 2018 +0200
@@ -4,10 +4,56 @@
 #include <tuple>
 
 #include <RelationalWriter.h>
+#include <RelpipeWriterException.h>
 #include <Factory.h>
 #include <TypeId.h>
 
+#include "CLI.h"
+
+int demo();
+
 int main(int argc, char** argv) {
+	using namespace relpipe::cli;
+	using namespace relpipe::writer;
+
+	setlocale(LC_ALL, "");
+	CLI cli(argc, argv);
+
+	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
+
+	try {
+		if (cli.arguments().size() > 0) {
+
+			const wstring action = cli.arguments()[0];
+			vector<wstring> arguments(cli.arguments().size() - 1);
+			for (int i = 1; i < cli.arguments().size(); i++) {
+				arguments[i - 1] = cli.arguments()[i];
+			}
+
+			if (action == L"demo") {
+				resultCode = demo();
+			} else {
+				fwprintf(stderr, L"Unknown command: %ls\n", action.c_str());
+				resultCode = CLI::EXIT_CODE_UNKNOWN_COMMAND;
+			}
+
+
+		} else {
+			fwprintf(stderr, L"Missing command…\n");
+			resultCode = CLI::EXIT_CODE_BAD_SYNTAX;
+		}
+	} catch (RelpipeWriterException e) {
+		fwprintf(stderr, L"Caught exception: %ls\n", e.getMessge().c_str());
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = CLI::EXIT_CODE_DATA_ERROR;
+	}
+
+
+	return resultCode;
+
+}
+
+int demo() {
 	using namespace relpipe::writer;
 	std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));