# HG changeset patch # User František Kučera # Date 1620758542 -7200 # Node ID 98be80d2e65b2c6d661da6d4ccd84113f19aa0a6 # Parent c69670b7b4ef487db0ed542dc78f7befdd5111d4 new CLI options: --case-sensitive, --invert-match diff -r c69670b7b4ef -r 98be80d2e65b bash-completion.sh --- a/bash-completion.sh Tue May 11 18:30:50 2021 +0200 +++ b/bash-completion.sh Tue May 11 20:42:22 2021 +0200 @@ -22,15 +22,31 @@ w2=${COMP_WORDS[COMP_CWORD-2]} w3=${COMP_WORDS[COMP_CWORD-3]} + BOOLEAN_VALUES=( + "true" + "false" + ) + + ENTITY_VALUES=( + "relation" + "attribute" + "value" + ) if [[ "$w1" == "--relation" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'") elif [[ "$w1" == "--attribute" && "x$w0" == "x" ]]; then COMPREPLY=("'.*'") elif [[ "$w1" == "--pattern" && "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" "--pattern" + "--case-sensitive" + "--invert-match" ) COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0")) fi diff -r c69670b7b4ef -r 98be80d2e65b src/CLIParser.h --- a/src/CLIParser.h Tue May 11 18:30:50 2021 +0200 +++ b/src/CLIParser.h Tue May 11 20:42:22 2021 +0200 @@ -36,8 +36,28 @@ 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 if (value == L"value") return RelationConfiguration::ENTITY::VALUE; + 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); + currentRelation.attributePattern = currentRelation.caseSensitive[E::ATTRIBUTE] ? std::wregex(currentRelation.attribute) : std::wregex(currentRelation.attribute, std::regex_constants::icase); + currentRelation.valuePattern = currentRelation.caseSensitive[E::VALUE] ? std::wregex(currentRelation.value) : std::wregex(currentRelation.value, std::regex_constants::icase); c.relationConfigurations.push_back(currentRelation); currentRelation = RelationConfiguration(); } @@ -48,6 +68,8 @@ static const relpipe::common::type::StringX OPTION_RELATION; static const relpipe::common::type::StringX OPTION_ATTRIBUTE; static const relpipe::common::type::StringX OPTION_PATTERN; + 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; @@ -59,11 +81,16 @@ 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)); + currentRelation.attribute = readNext(arguments, i); } else if (option == OPTION_PATTERN) { - currentRelation.valuePattern = std::wregex(readNext(arguments, i)); + currentRelation.value = 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 @@ -78,6 +105,8 @@ 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"; +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 c69670b7b4ef -r 98be80d2e65b src/Configuration.h --- a/src/Configuration.h Tue May 11 18:30:50 2021 +0200 +++ b/src/Configuration.h Tue May 11 20:42:22 2021 +0200 @@ -16,7 +16,7 @@ */ #pragma once -#include +#include #include #include @@ -30,13 +30,32 @@ class RelationConfiguration { public: + enum class ENTITY { + RELATION, + ATTRIBUTE, + VALUE + }; + + RelationConfiguration() { + caseSensitive[ENTITY::RELATION] = true; + caseSensitive[ENTITY::ATTRIBUTE] = true; + caseSensitive[ENTITY::VALUE] = true; + invertMatch[ENTITY::RELATION] = false; + invertMatch[ENTITY::ATTRIBUTE] = false; + invertMatch[ENTITY::VALUE] = false; + } + virtual ~RelationConfiguration() { } - relpipe::common::type::StringX relation; // TODO: remove and use just relationPattern, see CLIParser + relpipe::common::type::StringX relation; + relpipe::common::type::StringX attribute; + relpipe::common::type::StringX value; std::wregex relationPattern; std::wregex attributePattern; std::wregex valuePattern; + std::map caseSensitive; + std::map invertMatch; }; class Configuration { diff -r c69670b7b4ef -r 98be80d2e65b src/GrepHandler.h --- a/src/GrepHandler.h Tue May 11 18:30:50 2021 +0200 +++ b/src/GrepHandler.h Tue May 11 20:42:22 2021 +0200 @@ -73,7 +73,7 @@ currentSearchableAttributes.resize(attributes.size(), false); currentFilter = nullptr; for (int i = 0; i < configuration.relationConfigurations.size(); i++) { - if (regex_match(name, configuration.relationConfigurations[i].relationPattern)) { + if (regex_match(name, configuration.relationConfigurations[i].relationPattern) ^ configuration.relationConfigurations[i].invertMatch[RelationConfiguration::ENTITY::RELATION]) { currentFilter = &configuration.relationConfigurations[i]; break; } @@ -81,7 +81,7 @@ if (currentFilter) { for (int i = 0; i < currentSearchableAttributes.size(); i++) { - currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern); + currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern) ^ currentFilter->invertMatch[RelationConfiguration::ENTITY::ATTRIBUTE]; } } @@ -94,11 +94,13 @@ if (currentSearchableAttributes[currentAttributeIndex]) { includeCurrentRecord |= regex_search(value, currentFilter->valuePattern); + // includeCurrentRecord ^= currentFilter->invertMatch[RelationConfiguration::ENTITY::VALUE]; // might be inverted here, but it would be probably less useful } currentAttributeIndex++; if (currentAttributeIndex > 0 && currentAttributeIndex % currentSearchableAttributes.size() == 0) { + includeCurrentRecord ^= currentFilter->invertMatch[RelationConfiguration::ENTITY::VALUE]; if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v); includeCurrentRecord = false; } diff -r c69670b7b4ef -r 98be80d2e65b src/relpipe-tr-grep.cpp --- a/src/relpipe-tr-grep.cpp Tue May 11 18:30:50 2021 +0200 +++ b/src/relpipe-tr-grep.cpp Tue May 11 20:42:22 2021 +0200 @@ -43,12 +43,12 @@ CLI::untieStdIO(); CLI cli(argc, argv); - CLIParser cliParser; - Configuration configuration = cliParser.parse(cli.arguments()); - int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR; try { + 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)); GrepHandler handler(writer, configuration);