relpipe-out-tabular.cpp
branchv_0
changeset 3 9a4062b12fc9
child 5 911ec74cce33
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/relpipe-out-tabular.cpp	Mon Sep 03 23:44:34 2018 +0200
@@ -0,0 +1,65 @@
+#include <cstdlib>
+#include <memory>
+
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+#include <relpipe/reader/Factory.h>
+#include <relpipe/reader/RelationalReader.h>
+#include <relpipe/reader/RelpipeReaderException.h>
+
+
+using namespace relpipe::cli;
+using namespace relpipe::reader;
+
+class DemoHandler : public handlers::RelationalReaderStringHadler {
+
+	void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > attributes) override {
+		std::wcout << L"start relation: " << name << std::endl << std::flush;
+		for (int i = 0; i < attributes.size(); i++) {
+			std::wcout << L"\tcolumn: " << attributes[i].first << L" / " << (int) attributes[i].second << std::endl << std::flush;
+		}
+	}
+
+	void attribute(const string_t& value) override {
+		std::wcout << L"attribute: " << value << std::endl << std::flush;
+	}
+
+
+};
+
+int main(int argc, char** argv) {
+	CLI cli(argc, argv);
+
+	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
+
+	try {
+		// if (cli.arguments().size() > 0) {
+
+		// const wstring commandName = 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];
+		// }
+
+		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
+		DemoHandler handler;
+		reader->addHandler(&handler);
+		reader->process();
+
+		resultCode = CLI::EXIT_CODE_SUCCESS;
+
+		// } else {
+		//	throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
+		// }
+	} catch (RelpipeCLIException e) {
+		fwprintf(stderr, L"Caught CLI 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 = e.getExitCode();
+	} catch (RelpipeReaderException e) {
+		fwprintf(stderr, L"Caught Reader 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;
+}