author | František Kučera <franta-hg@frantovo.cz> |
Sat, 11 Aug 2018 22:56:41 +0200 | |
branch | v_0 |
changeset 1 | 21ef3f6cd5e9 |
parent 0 | relpipe-in-cli.cpp@cac146f5345a |
child 2 | 6615824d69b7 |
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 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
#include <RelationalWriter.h> |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
#include <RelpipeWriterException.h> |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
#include <Factory.h> |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
#include <TypeId.h> |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
#include "CLI.h" |
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 |
|
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
18 |
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
|
19 |
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
|
20 |
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
|
21 |
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
|
22 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
23 |
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
|
24 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
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
|
26 |
{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
|
27 |
{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
|
28 |
{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
|
29 |
{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
|
30 |
// {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
|
31 |
{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
|
32 |
{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
|
33 |
{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
|
34 |
}, true); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
35 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
36 |
string lineBytes; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
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
|
38 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
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
|
40 |
wsmatch lineMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
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
|
42 |
int g = 1; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
|
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
wsmatch deviceMatch; |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
} else { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
55 |
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
|
56 |
writer->writeAttribute(device); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
} |
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 |
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
|
60 |
writer->writeAttribute(mountPoint); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
writer->writeAttribute(type); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
writer->writeAttribute(options); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
writer->writeAttribute(dump); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
writer->writeAttribute(pass); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
|
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 |
|
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
int main(int argc, char** argv) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
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
|
72 |
//CLI cli(argc, argv); |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
try { |
1
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
if (isatty(fileno(stdin))) { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
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
|
79 |
processDataStream(cout, &s); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
} else { |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
processDataStream(cout, &cin); |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
} |
21ef3f6cd5e9
fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
resultCode = CLI::EXIT_CODE_SUCCESS; |
0
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
} catch (RelpipeWriterException e) { |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
resultCode = CLI::EXIT_CODE_DATA_ERROR; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
} |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
|
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
return resultCode; |
cac146f5345a
create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
} |