--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/bash-completion.sh Tue May 11 22:03:45 2021 +0200
@@ -0,0 +1,52 @@
+# 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_cut_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"
+ )
+
+ if [[ "$w1" == "--relation" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'")
+ elif [[ "$w1" == "--attribute" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'")
+ 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"
+ "--case-sensitive"
+ "--invert-match"
+ )
+ COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
+ fi
+}
+
+complete -F _relpipe_tr_cut_completion relpipe-tr-cut
--- a/nbproject/configurations.xml Sat Oct 24 00:08:19 2020 +0200
+++ b/nbproject/configurations.xml Tue May 11 22:03:45 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 Tue May 11 22:03:45 2021 +0200
@@ -0,0 +1,107 @@
+/**
+ * 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 cut {
+
+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);
+ }
+
+ RelationConfiguration::ENTITY parseEntity(const relpipe::common::type::StringX& value) {
+ if (value == L"relation") return RelationConfiguration::ENTITY::RELATION;
+ else if (value == L"attribute") return RelationConfiguration::ENTITY::ATTRIBUTE;
+ 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 addRelation(Configuration& c, RelationConfiguration& currentRelation) {
+ if (currentRelation.relation.size()) {
+ using E = RelationConfiguration::ENTITY;
+ currentRelation.relationPattern = currentRelation.caseSensitive[E::RELATION] ? std::wregex(currentRelation.relation) : std::wregex(currentRelation.relation, std::regex_constants::icase);
+ for (const relpipe::common::type::StringX& attribute : currentRelation.attributes) currentRelation.attributePatterns.push_back(currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(attribute) : std::wregex(attribute, std::regex_constants::icase));
+ 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_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;
+
+ for (int i = 0; i < arguments.size();) {
+ relpipe::common::type::StringX option = readNext(arguments, i);
+
+ if (option == OPTION_RELATION) {
+ addRelation(c, currentRelation); // previous relation
+ currentRelation.relation = readNext(arguments, i);
+ } else if (option == OPTION_ATTRIBUTE) {
+ currentRelation.attributes.push_back(readNext(arguments, i));
+ } else if (option == OPTION_CASE_SENSITIVE) {
+ RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
+ currentRelation.caseSensitive[entity] = parseBoolean(readNext(arguments, i));
+ } else if (option == OPTION_INVERT_MATCH) {
+ RelationConfiguration::ENTITY entity = parseEntity(readNext(arguments, i));
+ currentRelation.invertMatch[entity] = parseBoolean(readNext(arguments, i));
+ } else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+ }
+ addRelation(c, currentRelation); // 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_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 Tue May 11 22:03:45 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 Tue May 11 22:03:45 2021 +0200
@@ -0,0 +1,66 @@
+/**
+ * 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 cut {
+
+class RelationConfiguration {
+public:
+
+ enum class ENTITY {
+ RELATION,
+ ATTRIBUTE
+ };
+
+ RelationConfiguration() {
+ caseSensitive[ENTITY::RELATION] = true;
+ caseSensitive[ENTITY::ATTRIBUTE] = true;
+ invertMatch[ENTITY::RELATION] = false;
+ invertMatch[ENTITY::ATTRIBUTE] = false;
+ }
+
+ virtual ~RelationConfiguration() {
+ }
+
+ relpipe::common::type::StringX relation;
+ std::vector<relpipe::common::type::StringX> attributes;
+ std::wregex relationPattern;
+ std::vector<std::wregex> attributePatterns;
+ 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() {
+ }
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/CutHandler.h Sat Oct 24 00:08:19 2020 +0200
+++ b/src/CutHandler.h Tue May 11 22:03:45 2021 +0200
@@ -34,9 +34,11 @@
#include <relpipe/cli/RelpipeCLIException.h>
+#include "Configuration.h"
+
namespace relpipe {
namespace tr {
-namespace grep {
+namespace cut {
using namespace std;
using namespace relpipe;
@@ -46,26 +48,16 @@
class CutHandler : public RelationalReaderStringHandler {
private:
shared_ptr<writer::RelationalWriter> relationalWriter;
-
- wregex relationNameRegEx;
- vector<wregex> attributeNameRegExes;
+ Configuration configuration;
+ RelationConfiguration* currentFilter = nullptr;
vector<integer_t> currentAttributeMapping;
vector<string_t> currentRecord;
integer_t currentAttributeIndex = 0;
- boolean_t filterCurrentRelation = false;
public:
- CutHandler(ostream& output, const vector<string_t>& arguments) {
- relationalWriter.reset(writer::Factory::create(output));
-
- if (arguments.size() >= 2) {
- relationNameRegEx = wregex(arguments[0]);
- for (int i = 1; i < arguments.size(); i++) attributeNameRegExes.push_back(wregex(arguments[i]));
- } else {
- throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> [<otherAttributeNameRegExp> ...]", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
- }
+ CutHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
}
void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
@@ -78,12 +70,21 @@
vector<writer::AttributeMetadata> writerMetadata;
- filterCurrentRelation = regex_match(name, relationNameRegEx);
- if (filterCurrentRelation) {
+ using E = RelationConfiguration::ENTITY;
+
+ currentFilter = nullptr;
+ for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
+ if (regex_match(name, configuration.relationConfigurations[i].relationPattern) ^ configuration.relationConfigurations[i].invertMatch[E::RELATION]) {
+ currentFilter = &configuration.relationConfigurations[i];
+ break;
+ }
+ }
+
+ if (currentFilter) {
currentAttributeMapping.clear();
- for (wregex attributeNameRegEx : attributeNameRegExes) {
+ for (std::wregex attributePattern : currentFilter->attributePatterns) {
for (int i = 0; i < allWriterMetadata.size(); i++) {
- if (regex_match(allWriterMetadata[i].attributeName, attributeNameRegEx)) currentAttributeMapping.push_back(i);
+ if (regex_match(allWriterMetadata[i].attributeName, attributePattern) ^ currentFilter->invertMatch[E::ATTRIBUTE]) currentAttributeMapping.push_back(i);
}
}
@@ -97,7 +98,7 @@
}
void attribute(const string_t& value) override {
- if (filterCurrentRelation) {
+ if (currentFilter) {
currentRecord[currentAttributeIndex] = value;
currentAttributeIndex++;
--- a/src/relpipe-tr-cut.cpp Sat Oct 24 00:08:19 2020 +0200
+++ b/src/relpipe-tr-cut.cpp Tue May 11 22:03:45 2021 +0200
@@ -31,11 +31,12 @@
#include <relpipe/writer/Factory.h>
#include <relpipe/writer/TypeId.h>
+#include "Configuration.h"
+#include "CLIParser.h"
#include "CutHandler.h"
using namespace relpipe::cli;
-using namespace relpipe::reader;
-using namespace relpipe::tr::grep;
+using namespace relpipe::tr::cut;
int main(int argc, char**argv) {
setlocale(LC_ALL, "");
@@ -45,8 +46,12 @@
int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
try {
- std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
- CutHandler 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));
+ CutHandler handler(writer, configuration);
reader->addHandler(&handler);
reader->process();