src/FstabCommand.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 09 May 2021 17:20:30 +0200
branchv_0
changeset 25 f71eb7aefd25
parent 19 src/relpipe-in-fstab.cpp@3cc1b9be97bc
permissions -rw-r--r--
add CLIParser and Configuration: --relation option
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     1
/**
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     2
 * Relational pipes
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     4
 *
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
19
3cc1b9be97bc fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     8
 *
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    12
 * GNU General Public License for more details.
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    13
 *
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f3cc6f4b627f license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    16
 */
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#include <cstdlib>
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    18
#include <vector>
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    20
#include <locale>
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>
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    27
#include <relpipe/writer/AttributeMetadata.h>
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
    28
#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
    29
#include <relpipe/writer/TypeId.h>
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
4
0afc596749a1 move CLI.h to a common header-only library
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    31
#include <relpipe/cli/CLI.h>
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    33
#include "FstabCommand.h"
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    34
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    35
using namespace std;
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    36
using namespace relpipe::cli;
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    37
using namespace relpipe::writer;
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    38
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    39
namespace relpipe {
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    40
namespace in {
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    41
namespace fstab {
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    42
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
using namespace relpipe::cli;
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
using namespace relpipe::writer;
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
2
6615824d69b7 original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
/**
6615824d69b7 original inspiration: SQL-API
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
 * 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
    48
 */
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    49
void FstabCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
1
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	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
    51
	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
    52
	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
    53
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    54
	writer->startRelation(configuration.relation,{
1
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
		{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
    56
		{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
    57
		{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
    58
		{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
    59
		// {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
    60
		{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
    61
		{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
    62
		{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
    63
	}, true);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
	string lineBytes;
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    66
	while (getline(input, lineBytes)) {
1
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
		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
    69
		wsmatch lineMatch;
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		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
    71
			int g = 1;
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
			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
    73
			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
    74
			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
    75
			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
    76
			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
    77
			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
    78
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
			wsmatch deviceMatch;
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
			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
    81
				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
    82
				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
    83
			} else {
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
				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
    85
				writer->writeAttribute(device);
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
			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
    89
			writer->writeAttribute(mountPoint);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
			writer->writeAttribute(type);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
			writer->writeAttribute(options);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
			writer->writeAttribute(dump);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
			writer->writeAttribute(pass);
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
		}
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
	}
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    96
}
21ef3f6cd5e9 fstab logic ported from the prototype: FstabRelationalGenerator.h
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    98
}
0
cac146f5345a create netbeans project
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
}
25
f71eb7aefd25 add CLIParser and Configuration: --relation option
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   100
}