relpipe-in-cli.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Jul 2018 14:06:53 +0200
branchv_0
changeset 11 3798b6bc9aea
parent 10 77593735b057
child 12 bc6fe00dd831
permissions -rw-r--r--
ArgumentsCommand: generate relational data from CLI arguments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#include <cstdlib>
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     2
#include <memory>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
7
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
     4
#include <tuple>
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
     5
1
7e43750deca8 shared library with pure abstract class (interface)
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     6
#include <RelationalWriter.h>
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     7
#include <RelpipeWriterException.h>
6
4585c212a767 move create() method to a factory
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     8
#include <Factory.h>
5
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
     9
#include <TypeId.h>
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    11
#include "CLI.h"
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    12
#include "Command.h"
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    13
#include "ArgumentsCommand.h"
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    14
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    15
int demo();
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    16
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
int main(int argc, char** argv) {
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    18
	using namespace relpipe::cli;
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    19
	using namespace relpipe::in::cli;
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    20
	using namespace relpipe::writer;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    21
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    22
	setlocale(LC_ALL, "");
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    23
	CLI cli(argc, argv);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    24
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    25
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    26
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    27
	try {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    28
		if (cli.arguments().size() > 0) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    30
			const wstring action = cli.arguments()[0];
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    31
			vector<wstring> arguments(cli.arguments().size() - 1);
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    32
			for (int i = 1; i < cli.arguments().size(); i++) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
				arguments[i - 1] = cli.arguments()[i];
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
			}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    35
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    36
			if (action == L"demo") {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    37
				resultCode = demo();
11
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    38
			} else if (action == L"generate") {
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    39
				ArgumentsCommand command;
3798b6bc9aea ArgumentsCommand: generate relational data from CLI arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    40
				command.process(cin, cout, action, arguments);
10
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    41
			} else {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    42
				fwprintf(stderr, L"Unknown command: %ls\n", action.c_str());
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    43
				resultCode = CLI::EXIT_CODE_UNKNOWN_COMMAND;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    44
			}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    45
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    46
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    47
		} else {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    48
			fwprintf(stderr, L"Missing command…\n");
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    49
			resultCode = CLI::EXIT_CODE_BAD_SYNTAX;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
		}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
	} catch (RelpipeWriterException e) {
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    52
		fwprintf(stderr, L"Caught exception: %ls\n", e.getMessge().c_str());
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    53
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    54
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    55
	}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
	return resultCode;
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    60
}
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    61
77593735b057 CLI infrastructure ported from the prototype
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
int demo() {
5
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    63
	using namespace relpipe::writer;
6
4585c212a767 move create() method to a factory
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    64
	std::shared_ptr<RelationalWriter> writer(Factory::create(std::cout));
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
7
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    66
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    67
	// Various data types passed as strings
8
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    68
	writer->startRelation(L"table_from_strings",{
5
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    69
		{L"s", TypeId::STRING},
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    70
		{L"i", TypeId::INTEGER},
83dd71fe5cfd use TypeId enum instead of numeric constants
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    71
		{L"b", TypeId::BOOLEAN}
4
e615c7d87279 multiple relations, various data types
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
	}, true);
e615c7d87279 multiple relations, various data types
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
8
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    74
	writer->writeAttribute(L"a");
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
	writer->writeAttribute(L"1");
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    76
	writer->writeAttribute(L"true");
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    78
	writer->writeAttribute(L"b");
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    79
	writer->writeAttribute(L"2");
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    80
	writer->writeAttribute(L"false");
3
7d4525147bf7 multiple records, no nullptr
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    81
7
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    82
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    83
	// Various data types passed as raw pointers + typeids
8
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    84
	writer->startRelation(L"from_raw_pointers",{
7
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    85
		{L"s", TypeId::STRING},
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    86
		{L"i", TypeId::INTEGER},
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    87
		{L"b", TypeId::BOOLEAN}
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    88
	}, true);
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    89
8
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    90
	string_t sValue;
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    91
	integer_t iValue;
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    92
	boolean_t bValue;
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    93
9
3be327f67cdd more records
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    94
	for (int i = 0; i < 8; i++) {
3be327f67cdd more records
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    95
		sValue.append(L"*");
3be327f67cdd more records
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    96
		iValue = i + 1;
3be327f67cdd more records
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    97
		bValue = iValue % 2 == 0;
8
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    98
		writer->writeAttribute(&sValue, typeid (sValue));
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    99
		writer->writeAttribute(&iValue, typeid (iValue));
c1472544d95a replace writeRecord() with sequence of writeAttribute()
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   100
		writer->writeAttribute(&bValue, typeid (bValue));
7
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   101
	}
9ac885dd8037 writeAttribute() with raw pointer and type_info
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   102
3
7d4525147bf7 multiple records, no nullptr
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
	return 0;
0
3dd6adb7e191 empty project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
}