cadMousePro-daemon/src/CLI.h
branchv_0
changeset 8 465572518625
parent 7 148f8dd077e8
child 26 fff8e9a86e85
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cadMousePro-daemon/src/CLI.h	Mon Aug 19 17:27:19 2019 +0200
@@ -0,0 +1,80 @@
+/**
+ * cadMousePro
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <locale.h>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <locale>
+#include <codecvt>
+#include <iostream>
+
+/**
+ * a common header-only library for CLI programs
+ */
+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 std::wstring programName() {
+		return (const std::wstring) program;
+	}
+
+	const std::vector<std::wstring> arguments() {
+		return (const std::vector<std::wstring>)args;
+	}
+
+	static const int EXIT_CODE_SUCCESS = 0;
+	static const int EXIT_CODE_UNEXPECTED_ERROR = 1;
+	static const int EXIT_CODE_BAD_CLI_ARGUMENTS = 6;
+
+	/**
+	 * std::cin and std::cout are "tied" by default i.e. reading from std::cin causes flush of the std::cout.
+	 * Call this to "untie" them and have less write() calls (better buffering).
+	 */
+	static void untieStdIO() {
+		std::cin.tie(nullptr);
+	}
+
+private:
+	int* argc;
+	char*** argv;
+	std::wstring program;
+	std::vector<std::wstring> args;
+	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+};