src/SedHandler.h
branchv_0
changeset 25 0cfbaf5c57a6
parent 19 9bac174d11b6
child 26 576d4965434f
--- 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() {