src/FstabCommand.cpp
branchv_0
changeset 25 f71eb7aefd25
parent 19 3cc1b9be97bc
equal deleted inserted replaced
24:bbebee4044d9 25:f71eb7aefd25
       
     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, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #include <cstdlib>
       
    18 #include <vector>
       
    19 #include <memory>
       
    20 #include <locale>
       
    21 #include <regex>
       
    22 #include <algorithm>
       
    23 #include <unistd.h>
       
    24 
       
    25 #include <relpipe/writer/RelationalWriter.h>
       
    26 #include <relpipe/writer/RelpipeWriterException.h>
       
    27 #include <relpipe/writer/AttributeMetadata.h>
       
    28 #include <relpipe/writer/Factory.h>
       
    29 #include <relpipe/writer/TypeId.h>
       
    30 
       
    31 #include <relpipe/cli/CLI.h>
       
    32 
       
    33 #include "FstabCommand.h"
       
    34 
       
    35 using namespace std;
       
    36 using namespace relpipe::cli;
       
    37 using namespace relpipe::writer;
       
    38 
       
    39 namespace relpipe {
       
    40 namespace in {
       
    41 namespace fstab {
       
    42 
       
    43 using namespace relpipe::cli;
       
    44 using namespace relpipe::writer;
       
    45 
       
    46 /**
       
    47  * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49
       
    48  */
       
    49 void FstabCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
       
    50 	wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)");
       
    51 	wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$");
       
    52 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    53 
       
    54 	writer->startRelation(configuration.relation,{
       
    55 		{L"scheme", TypeId::STRING},
       
    56 		{L"device", TypeId::STRING},
       
    57 		{L"mount_point", TypeId::STRING},
       
    58 		{L"type", TypeId::STRING},
       
    59 		// {L"types", TypeId::STRING}, // TODO: array
       
    60 		{L"options", TypeId::STRING}, // TODO: array
       
    61 		{L"dump", TypeId::INTEGER},
       
    62 		{L"pass", TypeId::INTEGER}
       
    63 	}, true);
       
    64 
       
    65 	string lineBytes;
       
    66 	while (getline(input, lineBytes)) {
       
    67 
       
    68 		wstring line = convertor.from_bytes(lineBytes);
       
    69 		wsmatch lineMatch;
       
    70 		if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) {
       
    71 			int g = 1;
       
    72 			wstring device = lineMatch[g++];
       
    73 			wstring mountPoint = lineMatch[g++];
       
    74 			wstring type = lineMatch[g++];
       
    75 			wstring options = lineMatch[g++];
       
    76 			wstring dump = lineMatch[g++];
       
    77 			wstring pass = lineMatch[g++];
       
    78 
       
    79 			wsmatch deviceMatch;
       
    80 			if (regex_search(device, deviceMatch, devicePattern)) {
       
    81 				writer->writeAttribute(deviceMatch[1]);
       
    82 				writer->writeAttribute(deviceMatch[2]);
       
    83 			} else {
       
    84 				writer->writeAttribute(L""); // TODO: null (requires bitmap)
       
    85 				writer->writeAttribute(device);
       
    86 			}
       
    87 
       
    88 			if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap)
       
    89 			writer->writeAttribute(mountPoint);
       
    90 			writer->writeAttribute(type);
       
    91 			writer->writeAttribute(options);
       
    92 			writer->writeAttribute(dump);
       
    93 			writer->writeAttribute(pass);
       
    94 		}
       
    95 	}
       
    96 }
       
    97 
       
    98 }
       
    99 }
       
   100 }