new CLI interface: --relation --attribute --value --replacement --case-sensitive --invert-match – first version v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 May 2021 18:18:10 +0200
branchv_0
changeset 25 0cfbaf5c57a6
parent 24 82e40295dfb4
child 26 576d4965434f
new CLI interface: --relation --attribute --value --replacement --case-sensitive --invert-match – first version
bash-completion.sh
nbproject/configurations.xml
src/CLIParser.h
src/CMakeLists.txt
src/Configuration.h
src/SedHandler.h
src/relpipe-tr-sed.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh	Sat May 15 18:18:10 2021 +0200
@@ -0,0 +1,59 @@
+# Relational pipes
+# Copyright © 2021 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/>.
+
+_relpipe_tr_sed_completion() {
+	local w0 w1 w2 w3
+
+	COMPREPLY=()
+	w0=${COMP_WORDS[COMP_CWORD]}
+	w1=${COMP_WORDS[COMP_CWORD-1]}
+	w2=${COMP_WORDS[COMP_CWORD-2]}
+	w3=${COMP_WORDS[COMP_CWORD-3]}
+
+	BOOLEAN_VALUES=(
+		"true"
+		"false"
+	)
+
+	ENTITY_VALUES=(
+		"relation"
+		"attribute"
+		"value"
+	)
+
+	if   [[ "$w1" == "--relation"                      && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
+	elif [[ "$w1" == "--attribute"                     && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
+	elif [[ "$w1" == "--value"                         && "x$w0" == "x" ]];    then COMPREPLY=("''")
+	elif [[ "$w1" == "--replacement"                   && "x$w0" == "x" ]];    then COMPREPLY=("''")
+	elif [[ "$w1" == "--modify"                                         ]];    then COMPREPLY=($(compgen -W "${ENTITY_VALUES[*]}"  -- "$w0"))
+	elif [[ "$w1" == "--case-sensitive"                                 ]];    then COMPREPLY=($(compgen -W "${ENTITY_VALUES[*]}"  -- "$w0"))
+	elif [[ "$w2" == "--case-sensitive"                                 ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	elif [[ "$w1" == "--invert-match"                                   ]];    then COMPREPLY=($(compgen -W "${ENTITY_VALUES[*]}"  -- "$w0"))
+	elif [[ "$w2" == "--invert-match"                                   ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
+	else
+		OPTIONS=(
+			"--relation"
+			"--attribute"
+			"--value"
+			"--replacement"
+			"--modify"
+			"--case-sensitive"
+			"--invert-match"
+		)
+		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+	fi
+}
+
+complete -F _relpipe_tr_sed_completion relpipe-tr-sed
--- a/nbproject/configurations.xml	Sat Oct 24 00:08:19 2020 +0200
+++ b/nbproject/configurations.xml	Sat May 15 18:18:10 2021 +0200
@@ -81,6 +81,7 @@
             <incDir>
               <pElem>../relpipe-lib-reader.cpp/include</pElem>
               <pElem>../relpipe-lib-writer.cpp/include</pElem>
+              <pElem>../relpipe-lib-common.cpp/include</pElem>
               <pElem>../relpipe-lib-cli.cpp/include</pElem>
               <pElem>build/Debug/src</pElem>
             </incDir>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/CLIParser.h	Sat May 15 18:18:10 2021 +0200
@@ -0,0 +1,130 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 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/>.
+ */
+#pragma once
+
+#include <vector>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/cli/CLI.h>
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace tr {
+namespace sed {
+
+class CLIParser {
+private:
+
+	relpipe::common::type::StringX readNext(std::vector<relpipe::common::type::StringX> arguments, int& i) {
+		if (i < arguments.size()) return arguments[i++];
+		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	/**
+	 * TODO: use a common method
+	 */
+	bool parseBoolean(const relpipe::common::type::StringX& value) {
+		if (value == L"true") return true;
+		else if (value == L"false") return false;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	ENTITY parseEntity(const relpipe::common::type::StringX& value) {
+		if (value == L"relation") return ENTITY::RELATION;
+		else if (value == L"attribute") return ENTITY::ATTRIBUTE;
+		else if (value == L"value") return ENTITY::VALUE;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse entity value: " + value + L" (expecting „relation“, „attribute“ or „value“)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
+	void addRule(RelationConfiguration& currentRelation, RewriteRule& currentRule) {
+		if (currentRule.attribute.size()) {
+			currentRule.attributePattern = currentRule.caseSensitive[ENTITY::ATTRIBUTE] ? std::wregex(currentRule.attribute) : std::wregex(currentRule.attribute, std::regex_constants::icase);
+			currentRule.valuePattern = currentRule.caseSensitive[ENTITY::VALUE] ? std::wregex(currentRule.value) : std::wregex(currentRule.value, std::regex_constants::icase);
+			currentRelation.rules.push_back(currentRule);
+			currentRule = RewriteRule(currentRelation.caseSensitive, currentRelation.invertMatch);
+		}
+	}
+
+	void addRelation(Configuration& c, RelationConfiguration& currentRelation, RewriteRule& currentRule) {
+		if (currentRelation.relation.size()) {
+			currentRelation.relationPattern = currentRelation.caseSensitive[ENTITY::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase);
+			addRule(currentRelation, currentRule); // last rule
+			c.relationConfigurations.push_back(currentRelation);
+			currentRelation = RelationConfiguration();
+		}
+	}
+
+public:
+
+	static const relpipe::common::type::StringX OPTION_RELATION;
+	static const relpipe::common::type::StringX OPTION_ATTRIBUTE;
+	static const relpipe::common::type::StringX OPTION_VALUE;
+	static const relpipe::common::type::StringX OPTION_REPLACEMENT;
+	static const relpipe::common::type::StringX OPTION_CASE_SENSITIVE;
+	static const relpipe::common::type::StringX OPTION_INVERT_MATCH;
+
+	Configuration parse(const std::vector<relpipe::common::type::StringX>& arguments) {
+		Configuration c;
+		RelationConfiguration currentRelation;
+		RewriteRule currentRule(currentRelation.caseSensitive, currentRelation.invertMatch);
+
+		for (int i = 0; i < arguments.size();) {
+			relpipe::common::type::StringX option = readNext(arguments, i);
+
+			if (option == OPTION_RELATION) {
+				addRelation(c, currentRelation, currentRule); // previous relation
+				currentRelation.relation = readNext(arguments, i);
+			} else if (option == OPTION_ATTRIBUTE) {
+				addRule(currentRelation, currentRule); // previous rule
+				currentRule.attribute = readNext(arguments, i);
+			} else if (option == OPTION_VALUE) {
+				currentRule.value = readNext(arguments, i);
+			} else if (option == OPTION_REPLACEMENT) {
+				currentRule.replacement = readNext(arguments, i);
+			} else if (option == OPTION_CASE_SENSITIVE) {
+				ENTITY entity = parseEntity(readNext(arguments, i));
+				bool value = parseBoolean(readNext(arguments, i));
+				if (currentRule.attribute.size()) currentRule.caseSensitive[entity] = value;
+				else currentRelation.caseSensitive[entity] = value;
+			} else if (option == OPTION_INVERT_MATCH) {
+				ENTITY entity = parseEntity(readNext(arguments, i));
+				bool value = parseBoolean(readNext(arguments, i));
+				if (currentRule.attribute.size()) currentRule.invertMatch[entity] = value;
+				else currentRelation.invertMatch[entity] = value;
+			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+		}
+		addRelation(c, currentRelation, currentRule); // last relation
+
+		return c;
+	}
+
+	virtual ~CLIParser() {
+	}
+};
+
+const relpipe::common::type::StringX CLIParser::OPTION_RELATION = L"--relation";
+const relpipe::common::type::StringX CLIParser::OPTION_ATTRIBUTE = L"--attribute";
+const relpipe::common::type::StringX CLIParser::OPTION_VALUE = L"--value";
+const relpipe::common::type::StringX CLIParser::OPTION_REPLACEMENT = L"--replacement";
+const relpipe::common::type::StringX CLIParser::OPTION_CASE_SENSITIVE = L"--case-sensitive";
+const relpipe::common::type::StringX CLIParser::OPTION_INVERT_MATCH = L"--invert-match";
+
+}
+}
+}
--- a/src/CMakeLists.txt	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/CMakeLists.txt	Sat May 15 18:18:10 2021 +0200
@@ -17,7 +17,7 @@
 
 # Relpipe libraries:
 INCLUDE(FindPkgConfig)
-pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-cli.cpp)
+pkg_check_modules (RELPIPE_LIBS relpipe-lib-reader.cpp relpipe-lib-writer.cpp relpipe-lib-common.cpp relpipe-lib-cli.cpp)
 include_directories(${RELPIPE_LIBS_INCLUDE_DIRS})
 link_directories(${RELPIPE_LIBS_LIBRARY_DIRS})
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Configuration.h	Sat May 15 18:18:10 2021 +0200
@@ -0,0 +1,87 @@
+/**
+ * Relational pipes
+ * Copyright © 2021 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/>.
+ */
+#pragma once
+
+#include <map>
+#include <regex>
+#include <locale>
+
+#include <relpipe/common/type/typedefs.h>
+
+
+namespace relpipe {
+namespace tr {
+namespace sed {
+
+enum class ENTITY {
+	RELATION,
+	ATTRIBUTE,
+	VALUE
+};
+
+class RewriteRule {
+public:
+
+	RewriteRule(std::map<ENTITY, relpipe::common::type::Boolean> caseSensitive, std::map<ENTITY, relpipe::common::type::Boolean> invertMatch) : caseSensitive(caseSensitive), invertMatch(invertMatch) {
+	}
+
+	virtual ~RewriteRule() {
+	}
+
+	relpipe::common::type::StringX attribute;
+	relpipe::common::type::StringX value;
+	relpipe::common::type::StringX replacement;
+	std::wregex attributePattern;
+	std::wregex valuePattern;
+	std::map<ENTITY, relpipe::common::type::Boolean> caseSensitive;
+	std::map<ENTITY, relpipe::common::type::Boolean> invertMatch;
+};
+
+class RelationConfiguration {
+public:
+
+	RelationConfiguration() {
+		caseSensitive[ENTITY::RELATION] = true;
+		caseSensitive[ENTITY::ATTRIBUTE] = true;
+		caseSensitive[ENTITY::VALUE] = true;
+		invertMatch[ENTITY::RELATION] = false;
+		invertMatch[ENTITY::ATTRIBUTE] = false;
+		invertMatch[ENTITY::VALUE] = false;
+	}
+
+	virtual ~RelationConfiguration() {
+
+	}
+
+	relpipe::common::type::StringX relation;
+	std::wregex relationPattern;
+	std::vector<RewriteRule> rules;
+	std::map<ENTITY, relpipe::common::type::Boolean> caseSensitive;
+	std::map<ENTITY, relpipe::common::type::Boolean> invertMatch;
+};
+
+class Configuration {
+public:
+	std::vector<RelationConfiguration> relationConfigurations;
+
+	virtual ~Configuration() {
+	}
+};
+
+}
+}
+}
--- a/src/SedHandler.h	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/SedHandler.h	Sat May 15 18:18:10 2021 +0200
@@ -34,6 +34,8 @@
 
 #include <relpipe/cli/RelpipeCLIException.h>
 
+#include "Configuration.h"
+
 namespace relpipe {
 namespace tr {
 namespace sed {
@@ -46,28 +48,15 @@
 class SedHandler : public RelationalReaderStringHandler {
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
+	Configuration configuration;
+	RelationConfiguration* currentFilter = nullptr;
 
-	wregex relationNameRegEx;
-	wregex attributeNameRegEx;
-	wregex searchRegEx;
-	string_t replacement;
-
-	vector<boolean_t> currentReplacableAttributes;
+	std::vector<std::vector<RewriteRule*>> currentRules;
 	integer_t currentAttributeIndex = 0;
 
 public:
 
-	SedHandler(ostream& output, const vector<string_t>& arguments) {
-		relationalWriter.reset(writer::Factory::create(output));
-
-		if (arguments.size() == 4) {
-			relationNameRegEx = wregex(arguments[0]);
-			attributeNameRegEx = wregex(arguments[1]);
-			searchRegEx = wregex(arguments[2]);
-			replacement = arguments[3];
-		} else {
-			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-sed <relationNameRegExp> <attributeNameRegExp> <searchRegExp> <replacement>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
-		}
+	SedHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
 	}
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
@@ -78,10 +67,14 @@
 		}
 
 
-		currentReplacableAttributes.resize(attributes.size(), false);
-		if (regex_match(name, relationNameRegEx)) {
-			for (int i = 0; i < currentReplacableAttributes.size(); i++) {
-				currentReplacableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
+		currentRules.resize(attributes.size());
+		for (RelationConfiguration& rc : configuration.relationConfigurations) {
+			if (std::regex_match(name, rc.relationPattern) ^ rc.invertMatch[ENTITY::RELATION]) {
+				for (int i = 0; i < currentRules.size(); i++) {
+					for (RewriteRule& rule : rc.rules) {
+						if (std::regex_match(attributes[i].getAttributeName(), rule.attributePattern) ^ rule.invertMatch[ENTITY::ATTRIBUTE]) currentRules[i].push_back(&rule);
+					}
+				}
 			}
 		}
 
@@ -89,14 +82,16 @@
 	}
 
 	void attribute(const string_t& value) override {
-		if (currentReplacableAttributes[currentAttributeIndex]) {
-			relationalWriter->writeAttribute(regex_replace(value, searchRegEx, replacement));
-		} else {
-			relationalWriter->writeAttribute(value);
+		string_t newValue = value;
+
+		for (RewriteRule* rule : currentRules[currentAttributeIndex]) {
+			if (rule) newValue = std::regex_replace(value, rule->valuePattern, rule->replacement);
 		}
 
+		relationalWriter->writeAttribute(newValue);
+
 		currentAttributeIndex++;
-		currentAttributeIndex = currentAttributeIndex % currentReplacableAttributes.size();
+		currentAttributeIndex = currentAttributeIndex % currentRules.size();
 	}
 
 	void endOfPipe() {
--- a/src/relpipe-tr-sed.cpp	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/relpipe-tr-sed.cpp	Sat May 15 18:18:10 2021 +0200
@@ -31,10 +31,11 @@
 #include <relpipe/writer/Factory.h>
 #include <relpipe/writer/TypeId.h>
 
+#include "Configuration.h"
+#include "CLIParser.h"
 #include "SedHandler.h"
 
 using namespace relpipe::cli;
-using namespace relpipe::reader;
 using namespace relpipe::tr::sed;
 
 int main(int argc, char**argv) {
@@ -45,8 +46,12 @@
 	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 
 	try {
-		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
-		SedHandler handler(std::cout, cli.arguments());
+		CLIParser cliParser;
+		Configuration configuration = cliParser.parse(cli.arguments());
+		
+		std::shared_ptr<writer::RelationalWriter> writer(relpipe::writer::Factory::create(std::cout));
+		std::shared_ptr<RelationalReader> reader(relpipe::reader::Factory::create(std::cin));
+		SedHandler handler(writer, configuration);
 		reader->addHandler(&handler);
 		reader->process();