author | František Kučera <franta-hg@frantovo.cz> |
Mon, 13 Aug 2018 20:31:13 +0200 | |
branch | v_0 |
changeset 5 | 549a40d276c3 |
parent 4 | 0afc596749a1 |
child 7 | f3cc6f4b627f |
permissions | -rw-r--r-- |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
#include <cstdlib> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
2 |
#include <fstream> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
#include <memory> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
4 |
#include <regex> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
5 |
#include <algorithm> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
6 |
#include <unistd.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
|
5
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
8 |
#include <relpipe/writer/RelationalWriter.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
9 |
#include <relpipe/writer/RelpipeWriterException.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
10 |
#include <relpipe/writer/Factory.h> |
549a40d276c3
relpipe-lib-writer: move public header files to: include/relpipe/writer/
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
11 |
#include <relpipe/writer/TypeId.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
|
4
0afc596749a1
move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
13 |
#include <relpipe/cli/CLI.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
using namespace relpipe::cli; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
using namespace relpipe::writer; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
|
2
6615824d69b7
original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
18 |
/** |
6615824d69b7
original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
19 |
* see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49 |
6615824d69b7
original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
20 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
void processDataStream(ostream &output, istream* input) { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)"); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$"); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
24 |
wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
28 |
writer->startRelation(L"fstab",{ |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
{L"scheme", TypeId::STRING}, |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
{L"device", TypeId::STRING}, |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
31 |
{L"mount_point", TypeId::STRING}, |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
32 |
{L"type", TypeId::STRING}, |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
33 |
// {L"types", TypeId::STRING}, // TODO: array |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
34 |
{L"options", TypeId::STRING}, // TODO: array |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
35 |
{L"dump", TypeId::INTEGER}, |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
36 |
{L"pass", TypeId::INTEGER} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
}, true); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
string lineBytes; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
while (getline(*input, lineBytes)) { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
42 |
wstring line = convertor.from_bytes(lineBytes); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
wsmatch lineMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
int g = 1; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
wstring device = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
wstring mountPoint = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
wstring type = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
wstring options = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
wstring dump = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
wstring pass = lineMatch[g++]; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
wsmatch deviceMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
if (regex_search(device, deviceMatch, devicePattern)) { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
writer->writeAttribute(deviceMatch[1]); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
writer->writeAttribute(deviceMatch[2]); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
} else { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
writer->writeAttribute(L""); // TODO: null (requires bitmap) |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
writer->writeAttribute(device); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap) |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
writer->writeAttribute(mountPoint); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
writer->writeAttribute(type); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
writer->writeAttribute(options); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
writer->writeAttribute(dump); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
writer->writeAttribute(pass); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
int main(int argc, char** argv) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
setlocale(LC_ALL, ""); |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
//CLI cli(argc, argv); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
try { |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
if (isatty(fileno(stdin))) { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
81 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
82 |
* Our program is executed on TTY without input stream redirection → read from default file location. |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
* e.g. $ relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
* (we don't expect that user is writing the content of fstab by hand on the terminal) |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
85 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
ifstream s("/etc/fstab"); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
processDataStream(cout, &s); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
} else { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
89 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
90 |
* Input stream comes from a file or is piped from other command → read it instead of default file. |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
91 |
* e.g. $ cat /etc/fstab | grep '/mnt/' | relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
92 |
* or $ relpipe-in-fstab < /etc/fstab | … # without UUoC |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
93 |
* or $ ssh example.com cat /etc/fstab | relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
94 |
* or $ cat | relpipe-in-fstab | … # user writes the fstab content by hand |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
95 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
processDataStream(cout, &cin); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
} catch (RelpipeWriterException e) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str()); |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount()); |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
} |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
return resultCode; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
} |