src/SedHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 22:05:00 +0200
branchv_0
changeset 19 9bac174d11b6
parent 15 09981db6ad87
child 25 0cfbaf5c57a6
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
19
9bac174d11b6 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/writer/Factory.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
#include <relpipe/cli/RelpipeCLIException.h>
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    36
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace relpipe {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace tr {
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
namespace sed {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
using namespace std;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
using namespace relpipe;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
using namespace relpipe::reader;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
using namespace relpipe::reader::handlers;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
15
09981db6ad87 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    46
class SedHandler : public RelationalReaderStringHandler {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
private:
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
	shared_ptr<writer::RelationalWriter> relationalWriter;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
	wregex relationNameRegEx;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
	wregex attributeNameRegEx;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
	wregex searchRegEx;
8
de3def6505a9 working regex_replace
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    53
	string_t replacement;
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    54
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
	vector<boolean_t> currentReplacableAttributes;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
	integer_t currentAttributeIndex = 0;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
public:
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	SedHandler(ostream& output, const vector<string_t>& arguments) {
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
		relationalWriter.reset(writer::Factory::create(output));
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
		if (arguments.size() == 4) {
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
			relationNameRegEx = wregex(arguments[0]);
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
			attributeNameRegEx = wregex(arguments[1]);
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
			searchRegEx = wregex(arguments[2]);
8
de3def6505a9 working regex_replace
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    67
			replacement = arguments[3];
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
		} else {
9
f5c9a6daf4ea small text update
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    69
			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-sed <relationNameRegExp> <attributeNameRegExp> <searchRegExp> <replacement>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    70
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
		vector<writer::AttributeMetadata> writerMetadata;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		for (AttributeMetadata readerMetadata : attributes) {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		}
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
		currentReplacableAttributes.resize(attributes.size(), false);
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
		if (regex_match(name, relationNameRegEx)) {
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
			for (int i = 0; i < currentReplacableAttributes.size(); i++) {
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
				currentReplacableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
			}
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
		}
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		relationalWriter->startRelation(name, writerMetadata, true);
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	void attribute(const string_t& value) override {
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    92
		if (currentReplacableAttributes[currentAttributeIndex]) {
8
de3def6505a9 working regex_replace
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    93
			relationalWriter->writeAttribute(regex_replace(value, searchRegEx, replacement));
7
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    94
		} else {
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    95
			relationalWriter->writeAttribute(value);
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    96
		}
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    97
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    98
		currentAttributeIndex++;
92d85e02b276 regex match relation and attribute + replace with a fixed value
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
		currentAttributeIndex = currentAttributeIndex % currentReplacableAttributes.size();
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	void endOfPipe() {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
};
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
}