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

/**
 * Relational pipes
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <cstdlib>
#include <vector>
#include <memory>
#include <locale>
#include <regex>
#include <algorithm>
#include <unistd.h>

#include <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include <relpipe/writer/AttributeMetadata.h>
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>

#include <relpipe/cli/CLI.h>

#include "FstabCommand.h"

using namespace std;
using namespace relpipe::cli;
using namespace relpipe::writer;

namespace relpipe {
namespace in {
namespace fstab {

using namespace relpipe::cli;
using namespace relpipe::writer;

/**
 * see https://hg.frantovo.cz/sql-api/file/tip/prototyp/prototyp.sql#l49
 */
void FstabCommand::process(std::istream& input, std::shared_ptr<writer::RelationalWriter> writer, Configuration& configuration) {
	wregex devicePattern = wregex(L"(LABEL|UUID)=(.*)");
	wregex linePattern = wregex(L"^([^\\s#]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s*$");
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.

	writer->startRelation(configuration.relation,{
		{L"scheme", TypeId::STRING},
		{L"device", TypeId::STRING},
		{L"mount_point", TypeId::STRING},
		{L"type", TypeId::STRING},
		// {L"types", TypeId::STRING}, // TODO: array
		{L"options", TypeId::STRING}, // TODO: array
		{L"dump", TypeId::INTEGER},
		{L"pass", TypeId::INTEGER}
	}, true);

	string lineBytes;
	while (getline(input, lineBytes)) {

		wstring line = convertor.from_bytes(lineBytes);
		wsmatch lineMatch;
		if (regex_search(line, lineMatch, linePattern) && lineMatch.size() > 0) {
			int g = 1;
			wstring device = lineMatch[g++];
			wstring mountPoint = lineMatch[g++];
			wstring type = lineMatch[g++];
			wstring options = lineMatch[g++];
			wstring dump = lineMatch[g++];
			wstring pass = lineMatch[g++];

			wsmatch deviceMatch;
			if (regex_search(device, deviceMatch, devicePattern)) {
				writer->writeAttribute(deviceMatch[1]);
				writer->writeAttribute(deviceMatch[2]);
			} else {
				writer->writeAttribute(L""); // TODO: null (requires bitmap)
				writer->writeAttribute(device);
			}

			if (mountPoint == L"none") mountPoint = L""; // TODO: null (requires bitmap)
			writer->writeAttribute(mountPoint);
			writer->writeAttribute(type);
			writer->writeAttribute(options);
			writer->writeAttribute(dump);
			writer->writeAttribute(pass);
		}
	}
}

}
}
}