src/CutHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 12 Dec 2018 23:45:46 +0100
branchv_0
changeset 9 f06781a5071b
parent 8 src/GrepHandler.h@f66c759d1111
child 10 9ec1290b4a9d
permissions -rw-r--r--
project skeleton
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
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * GNU General Public License for more details.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 */
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <vector>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <iostream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <sstream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <locale>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <codecvt>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <regex>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/typedefs.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/writer/Factory.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    36
#include <relpipe/cli/RelpipeCLIException.h>
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    37
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace relpipe {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace tr {
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
namespace grep {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    42
using namespace std;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
using namespace relpipe;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
using namespace relpipe::reader;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
using namespace relpipe::reader::handlers;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
class GrepHandler : public RelationalReaderStringHadler {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
private:
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    49
	shared_ptr<writer::RelationalWriter> relationalWriter;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    50
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    51
	wregex relationNameRegEx;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    52
	wregex attributeNameRegEx;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    53
	wregex searchRegEx;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    54
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    55
	vector<boolean_t> currentSearchableAttributes;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    56
	vector<string_t> currentRecord;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    57
	integer_t currentAttributeIndex = 0;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    58
	boolean_t includeCurrentRecord = false;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    59
	boolean_t filterCurrentRelation = false;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
public:
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    63
	GrepHandler(ostream& output, const vector<string_t>& arguments) {
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    64
		relationalWriter.reset(writer::Factory::create(output));
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
		if (arguments.size() == 3) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    67
			relationNameRegEx = wregex(arguments[0]);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    68
			attributeNameRegEx = wregex(arguments[1]);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    69
			searchRegEx = wregex(arguments[2]);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    70
		} else {
9
f06781a5071b project skeleton
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    71
			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    72
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    75
	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
    76
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
		vector<writer::AttributeMetadata> writerMetadata;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		for (AttributeMetadata readerMetadata : attributes) {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			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
    80
		}
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    81
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    82
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    83
		currentRecord.resize(attributes.size());
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    84
		currentSearchableAttributes.resize(attributes.size(), false);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    85
		filterCurrentRelation = regex_match(name, relationNameRegEx);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    86
		if (filterCurrentRelation) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    87
			for (int i = 0; i < currentSearchableAttributes.size(); i++) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    88
				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    89
			}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    90
		}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    91
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		relationalWriter->startRelation(name, writerMetadata, true);
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	void attribute(const string_t& value) override {
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
		if (filterCurrentRelation) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    97
			currentRecord[currentAttributeIndex] = value;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    98
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    99
			if (currentSearchableAttributes[currentAttributeIndex]) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   100
				includeCurrentRecord |= regex_search(value, searchRegEx);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
			}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   102
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   103
			currentAttributeIndex++;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   104
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   105
			if (currentAttributeIndex > 0 && currentAttributeIndex % currentSearchableAttributes.size() == 0) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   106
				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   107
				includeCurrentRecord = false;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   108
			}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   109
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   110
			currentAttributeIndex = currentAttributeIndex % currentSearchableAttributes.size();
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   111
		} else {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   112
			relationalWriter->writeAttribute(value);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   113
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	void endOfPipe() {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
};
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
}