relpipe-out-tabular.cpp
branchv_0
changeset 3 9a4062b12fc9
child 5 911ec74cce33
equal deleted inserted replaced
2:31ef97e63eb0 3:9a4062b12fc9
       
     1 #include <cstdlib>
       
     2 #include <memory>
       
     3 
       
     4 #include <relpipe/cli/CLI.h>
       
     5 #include <relpipe/cli/RelpipeCLIException.h>
       
     6 #include <relpipe/reader/Factory.h>
       
     7 #include <relpipe/reader/RelationalReader.h>
       
     8 #include <relpipe/reader/RelpipeReaderException.h>
       
     9 
       
    10 
       
    11 using namespace relpipe::cli;
       
    12 using namespace relpipe::reader;
       
    13 
       
    14 class DemoHandler : public handlers::RelationalReaderStringHadler {
       
    15 
       
    16 	void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > attributes) override {
       
    17 		std::wcout << L"start relation: " << name << std::endl << std::flush;
       
    18 		for (int i = 0; i < attributes.size(); i++) {
       
    19 			std::wcout << L"\tcolumn: " << attributes[i].first << L" / " << (int) attributes[i].second << std::endl << std::flush;
       
    20 		}
       
    21 	}
       
    22 
       
    23 	void attribute(const string_t& value) override {
       
    24 		std::wcout << L"attribute: " << value << std::endl << std::flush;
       
    25 	}
       
    26 
       
    27 
       
    28 };
       
    29 
       
    30 int main(int argc, char** argv) {
       
    31 	CLI cli(argc, argv);
       
    32 
       
    33 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
       
    34 
       
    35 	try {
       
    36 		// if (cli.arguments().size() > 0) {
       
    37 
       
    38 		// const wstring commandName = cli.arguments()[0];
       
    39 		// vector<wstring> arguments(cli.arguments().size() - 1);
       
    40 		// for (int i = 1; i < cli.arguments().size(); i++) {
       
    41 		//	arguments[i - 1] = cli.arguments()[i];
       
    42 		// }
       
    43 
       
    44 		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
       
    45 		DemoHandler handler;
       
    46 		reader->addHandler(&handler);
       
    47 		reader->process();
       
    48 
       
    49 		resultCode = CLI::EXIT_CODE_SUCCESS;
       
    50 
       
    51 		// } else {
       
    52 		//	throw RelpipeCLIException(L"Missing command…", CLI::EXIT_CODE_BAD_SYNTAX);
       
    53 		// }
       
    54 	} catch (RelpipeCLIException e) {
       
    55 		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessge().c_str());
       
    56 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    57 		resultCode = e.getExitCode();
       
    58 	} catch (RelpipeReaderException e) {
       
    59 		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessge().c_str());
       
    60 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
       
    61 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
       
    62 	}
       
    63 
       
    64 	return resultCode;
       
    65 }