# HG changeset patch # User František Kučera # Date 1620763425 -7200 # Node ID bc15f5471b6aa10c15e3cd4aaae2862c3ca11fb3 # Parent f9b72d26383892dff539a96e9614426b42f82d32 new CLI interface: --relation, --attribute diff -r f9b72d263838 -r bc15f5471b6a bash-completion.sh --- /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 . + +_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 diff -r f9b72d263838 -r bc15f5471b6a nbproject/configurations.xml --- 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 @@ ../relpipe-lib-reader.cpp/include ../relpipe-lib-writer.cpp/include + ../relpipe-lib-common.cpp/include ../relpipe-lib-cli.cpp/include build/Debug/src diff -r f9b72d263838 -r bc15f5471b6a src/CLIParser.h --- /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 . + */ +#pragma once + +#include + +#include +#include +#include + +#include "Configuration.h" + +namespace relpipe { +namespace tr { +namespace cut { + +class CLIParser { +private: + + relpipe::common::type::StringX readNext(std::vector 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& 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"; + +} +} +} diff -r f9b72d263838 -r bc15f5471b6a src/CMakeLists.txt --- 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}) diff -r f9b72d263838 -r bc15f5471b6a src/Configuration.h --- /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 . + */ +#pragma once + +#include +#include +#include + +#include + + +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 attributes; + std::wregex relationPattern; + std::vector attributePatterns; + std::map caseSensitive; + std::map invertMatch; +}; + +class Configuration { +public: + std::vector relationConfigurations; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r f9b72d263838 -r bc15f5471b6a src/CutHandler.h --- 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 +#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 relationalWriter; - - wregex relationNameRegEx; - vector attributeNameRegExes; + Configuration configuration; + RelationConfiguration* currentFilter = nullptr; vector currentAttributeMapping; vector currentRecord; integer_t currentAttributeIndex = 0; - boolean_t filterCurrentRelation = false; public: - CutHandler(ostream& output, const vector& 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 [ ...]", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); - } + CutHandler(shared_ptr relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { } void startRelation(string_t name, vector attributes) override { @@ -78,12 +70,21 @@ vector 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++; diff -r f9b72d263838 -r bc15f5471b6a src/relpipe-tr-cut.cpp --- 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 #include +#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 reader(Factory::create(std::cin)); - CutHandler handler(std::cout, cli.arguments()); + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); + + std::shared_ptr writer(relpipe::writer::Factory::create(std::cout)); + std::shared_ptr reader(relpipe::reader::Factory::create(std::cin)); + CutHandler handler(writer, configuration); reader->addHandler(&handler); reader->process();