author | František Kučera <franta-hg@frantovo.cz> |
Sun, 26 May 2019 22:22:58 +0200 | |
branch | v_0 |
changeset 17 | c4cc103ae0ed |
parent 9 | c0b16e4bd773 |
child 19 | 3cc1b9be97bc |
permissions | -rw-r--r-- |
7 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#include <cstdlib> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
#include <fstream> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <memory> |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <regex> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
22 |
#include <algorithm> |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
#include <unistd.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
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
|
25 |
#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
|
26 |
#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
|
27 |
#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
|
28 |
#include <relpipe/writer/TypeId.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
4
0afc596749a1
move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
30 |
#include <relpipe/cli/CLI.h> |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
using namespace relpipe::cli; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
using namespace relpipe::writer; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
|
2
6615824d69b7
original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
35 |
/** |
6615824d69b7
original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
36 |
* 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
|
37 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
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
|
44 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
45 |
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
|
46 |
{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
|
47 |
{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
|
48 |
{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
|
49 |
{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
|
50 |
// {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
|
51 |
{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
|
52 |
{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
|
53 |
{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
|
54 |
}, true); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
string lineBytes; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
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
|
58 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
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
|
60 |
wsmatch lineMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
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
|
62 |
int g = 1; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
wsmatch deviceMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
} else { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
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
|
76 |
writer->writeAttribute(device); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
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
|
80 |
writer->writeAttribute(mountPoint); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
writer->writeAttribute(type); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
writer->writeAttribute(options); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
writer->writeAttribute(dump); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
writer->writeAttribute(pass); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
int main(int argc, char** argv) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
setlocale(LC_ALL, ""); |
8
21b14ec0bc60
CLI: untieStdIO() to avoid unwanted implicit flush() calls on std::cout → less write() calls
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
92 |
CLI::untieStdIO(); |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
//CLI cli(argc, argv); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
try { |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
if (isatty(fileno(stdin))) { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
100 |
* 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
|
101 |
* e.g. $ relpipe-in-fstab | … |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
102 |
* (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
|
103 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
104 |
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
|
105 |
processDataStream(cout, &s); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
106 |
} else { |
3
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
107 |
/** |
e8be873f1eab
fstab: documentation for isatty(fileno(stdin))
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
108 |
* 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
|
109 |
* 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
|
110 |
* 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
|
111 |
* 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
|
112 |
* 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
|
113 |
*/ |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
114 |
processDataStream(cout, &cin); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
115 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
116 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
} catch (RelpipeWriterException e) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
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
|
120 |
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
|
121 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
} |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
return resultCode; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
} |