# HG changeset patch # User František Kučera # Date 1620750650 -7200 # Node ID c69670b7b4ef487db0ed542dc78f7befdd5111d4 # Parent 87d22d525a3ea038efc9863b1b828acd1db874f4 new CLI interface: --relation, --attribute, --pattern diff -r 87d22d525a3e -r c69670b7b4ef bash-completion.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bash-completion.sh Tue May 11 18:30:50 2021 +0200 @@ -0,0 +1,39 @@ +# 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_grep_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]} + + + if [[ "$w1" == "--relation" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'") + elif [[ "$w1" == "--attribute" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'") + elif [[ "$w1" == "--pattern" && "x$w0" == "x" ]]; then COMPREPLY=("''") + else + OPTIONS=( + "--relation" + "--attribute" + "--pattern" + ) + COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0")) + fi +} + +complete -F _relpipe_tr_grep_completion relpipe-tr-grep diff -r 87d22d525a3e -r c69670b7b4ef nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Oct 24 00:08:19 2020 +0200 +++ b/nbproject/configurations.xml Tue May 11 18:30:50 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 87d22d525a3e -r c69670b7b4ef src/CLIParser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CLIParser.h Tue May 11 18:30:50 2021 +0200 @@ -0,0 +1,84 @@ +/** + * 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 grep { + +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); + } + + void addRelation(Configuration& c, RelationConfiguration& currentRelation) { + if (currentRelation.relation.size()) { + 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_PATTERN; + + 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); + currentRelation.relationPattern = std::wregex(currentRelation.relation); + } else if (option == OPTION_ATTRIBUTE) { + currentRelation.attributePattern = std::wregex(readNext(arguments, i)); + } else if (option == OPTION_PATTERN) { + currentRelation.valuePattern = std::wregex(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_PATTERN = L"--pattern"; + +} +} +} diff -r 87d22d525a3e -r c69670b7b4ef src/CMakeLists.txt --- a/src/CMakeLists.txt Sat Oct 24 00:08:19 2020 +0200 +++ b/src/CMakeLists.txt Tue May 11 18:30:50 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 87d22d525a3e -r c69670b7b4ef src/Configuration.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Configuration.h Tue May 11 18:30:50 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 . + */ +#pragma once + +#include +#include +#include + +#include + + +namespace relpipe { +namespace tr { +namespace grep { + +class RelationConfiguration { +public: + + virtual ~RelationConfiguration() { + } + + relpipe::common::type::StringX relation; // TODO: remove and use just relationPattern, see CLIParser + std::wregex relationPattern; + std::wregex attributePattern; + std::wregex valuePattern; +}; + +class Configuration { +public: + std::vector relationConfigurations; + + virtual ~Configuration() { + } +}; + +} +} +} \ No newline at end of file diff -r 87d22d525a3e -r c69670b7b4ef src/GrepHandler.h --- a/src/GrepHandler.h Sat Oct 24 00:08:19 2020 +0200 +++ b/src/GrepHandler.h Tue May 11 18:30:50 2021 +0200 @@ -34,6 +34,8 @@ #include +#include "Configuration.h" + namespace relpipe { namespace tr { namespace grep { @@ -46,29 +48,17 @@ class GrepHandler : public RelationalReaderStringHandler { private: shared_ptr relationalWriter; - - wregex relationNameRegEx; - wregex attributeNameRegEx; - wregex searchRegEx; + Configuration configuration; + RelationConfiguration* currentFilter = nullptr; vector currentSearchableAttributes; vector currentRecord; integer_t currentAttributeIndex = 0; boolean_t includeCurrentRecord = false; - boolean_t filterCurrentRelation = false; public: - GrepHandler(ostream& output, const vector& arguments) { - relationalWriter.reset(writer::Factory::create(output)); - - if (arguments.size() == 3) { - relationNameRegEx = wregex(arguments[0]); - attributeNameRegEx = wregex(arguments[1]); - searchRegEx = wregex(arguments[2]); - } else { - throw cli::RelpipeCLIException(L"Usage: relpipe-tr-grep ", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); - } + GrepHandler(shared_ptr relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { } void startRelation(string_t name, vector attributes) override { @@ -81,10 +71,17 @@ currentRecord.resize(attributes.size()); currentSearchableAttributes.resize(attributes.size(), false); - filterCurrentRelation = regex_match(name, relationNameRegEx); - if (filterCurrentRelation) { + currentFilter = nullptr; + for (int i = 0; i < configuration.relationConfigurations.size(); i++) { + if (regex_match(name, configuration.relationConfigurations[i].relationPattern)) { + currentFilter = &configuration.relationConfigurations[i]; + break; + } + } + + if (currentFilter) { for (int i = 0; i < currentSearchableAttributes.size(); i++) { - currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx); + currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern); } } @@ -92,11 +89,11 @@ } void attribute(const string_t& value) override { - if (filterCurrentRelation) { + if (currentFilter) { currentRecord[currentAttributeIndex] = value; if (currentSearchableAttributes[currentAttributeIndex]) { - includeCurrentRecord |= regex_search(value, searchRegEx); + includeCurrentRecord |= regex_search(value, currentFilter->valuePattern); } currentAttributeIndex++; diff -r 87d22d525a3e -r c69670b7b4ef src/relpipe-tr-grep.cpp --- a/src/relpipe-tr-grep.cpp Sat Oct 24 00:08:19 2020 +0200 +++ b/src/relpipe-tr-grep.cpp Tue May 11 18:30:50 2021 +0200 @@ -31,10 +31,11 @@ #include #include +#include "Configuration.h" +#include "CLIParser.h" #include "GrepHandler.h" using namespace relpipe::cli; -using namespace relpipe::reader; using namespace relpipe::tr::grep; int main(int argc, char**argv) { @@ -42,11 +43,15 @@ CLI::untieStdIO(); CLI cli(argc, argv); + CLIParser cliParser; + Configuration configuration = cliParser.parse(cli.arguments()); + int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { - std::shared_ptr reader(Factory::create(std::cin)); - GrepHandler handler(std::cout, cli.arguments()); + std::shared_ptr writer(relpipe::writer::Factory::create(std::cout)); + std::shared_ptr reader(relpipe::reader::Factory::create(std::cin)); + GrepHandler handler(writer, configuration); reader->addHandler(&handler); reader->process();