aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Tom Trauth <coffee71@optimum.net> 2011-10-21 20:27:46 -0400
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-10-22 18:24:03 +0100
commitf12433cd2f6d22cec2bb68baa0f597690d5c18c9 (patch)
tree9495c1a9c5d41858ab2a256dc91fd936fafaa497
parent5d7d834f9c35adc9164e629b4780a5454de800ab (diff)
downloadpaludis-f12433cd2f6d22cec2bb68baa0f597690d5c18c9.tar.gz
paludis-f12433cd2f6d22cec2bb68baa0f597690d5c18c9.tar.xz
Add -C/--case-sensitive option to the cave search command to make matching case sensitive
-rw-r--r--src/clients/cave/cmd_match.cc30
-rw-r--r--src/clients/cave/cmd_search_cmdline.cc1
-rw-r--r--src/clients/cave/cmd_search_cmdline.hh1
-rw-r--r--src/clients/cave/match_extras.cc4
-rw-r--r--src/clients/cave/match_extras.hh2
5 files changed, 23 insertions, 15 deletions
diff --git a/src/clients/cave/cmd_match.cc b/src/clients/cave/cmd_match.cc
index adc9f7e..0acc14c 100644
--- a/src/clients/cave/cmd_match.cc
+++ b/src/clients/cave/cmd_match.cc
@@ -67,7 +67,7 @@ namespace
struct ExtrasHandle :
Singleton<ExtrasHandle>
{
- typedef bool (* MatchFunction)(const std::string &, const std::string &);
+ typedef bool (* MatchFunction)(const std::string &, const std::string &, bool);
void * handle;
MatchFunction match_function;
@@ -120,29 +120,35 @@ namespace
}
};
- bool match_text(const std::string & text, const std::string & pattern)
+ bool match_text(const std::string & text, const std::string & pattern, bool case_sensitive)
{
- return 0 != strcasestr(text.c_str(), pattern.c_str());
+ if (case_sensitive)
+ return 0 != strstr(text.c_str(), pattern.c_str());
+ else
+ return 0 != strcasestr(text.c_str(), pattern.c_str());
}
- bool match_exact(const std::string & text, const std::string & pattern)
+ bool match_exact(const std::string & text, const std::string & pattern, bool case_sensitive)
{
- return 0 == strcasecmp(text.c_str(), pattern.c_str());
+ if (case_sensitive)
+ return 0 == strcmp(text.c_str(), pattern.c_str());
+ else
+ return 0 == strcasecmp(text.c_str(), pattern.c_str());
}
- bool match_regex(const std::string & text, const std::string & pattern)
+ bool match_regex(const std::string & text, const std::string & pattern, bool case_sensitive)
{
- return ExtrasHandle::get_instance()->match_function(text, pattern);
+ return ExtrasHandle::get_instance()->match_function(text, pattern, case_sensitive);
}
- bool match(const std::string & text, const std::string & pattern, const std::string & algorithm)
+ bool match(const std::string & text, const std::string & pattern, bool case_sensitive, const std::string & algorithm)
{
if (algorithm == "text")
- return match_text(text, pattern);
+ return match_text(text, pattern, case_sensitive);
else if (algorithm == "exact")
- return match_exact(text, pattern);
+ return match_exact(text, pattern, case_sensitive);
else if (algorithm == "regex")
- return match_regex(text, pattern);
+ return match_regex(text, pattern, case_sensitive);
else
throw args::DoHelp("Unknown algoritm '" + algorithm + "'");
}
@@ -444,7 +450,7 @@ MatchCommand::run_hosted(
p != p_end ; ++p)
{
bool current(texts.end() != std::find_if(texts.begin(), texts.end(),
- std::bind(&match, std::placeholders::_1, *p, match_options.a_type.argument())));
+ std::bind(&match, std::placeholders::_1, *p, match_options.a_case_sensitive.specified(), match_options.a_type.argument())));
if (match_options.a_not.specified())
current = ! current;
diff --git a/src/clients/cave/cmd_search_cmdline.cc b/src/clients/cave/cmd_search_cmdline.cc
index 6891766..c17c0af 100644
--- a/src/clients/cave/cmd_search_cmdline.cc
+++ b/src/clients/cave/cmd_search_cmdline.cc
@@ -42,6 +42,7 @@ SearchCommandLineMatchOptions::SearchCommandLineMatchOptions(args::ArgsHandler *
("regex", 'r', "Match using pcre regular expressions, ignoring case"),
"text"
),
+ a_case_sensitive(&g_pattern_options, "case-sensitive", 'C', "Make matching case sensitive.", true),
a_and(&g_pattern_options, "and", '&', "If multiple patterns are specified, require that "
"all patterns match. Default is to succeed if any pattern matches.", true),
a_not(&g_pattern_options, "not", '!', "Invert the results of pattern matches.", true),
diff --git a/src/clients/cave/cmd_search_cmdline.hh b/src/clients/cave/cmd_search_cmdline.hh
index 8281e47..04e6b1c 100644
--- a/src/clients/cave/cmd_search_cmdline.hh
+++ b/src/clients/cave/cmd_search_cmdline.hh
@@ -46,6 +46,7 @@ namespace paludis
args::ArgsGroup g_pattern_options;
args::EnumArg a_type;
+ args::SwitchArg a_case_sensitive;
args::SwitchArg a_and;
args::SwitchArg a_not;
diff --git a/src/clients/cave/match_extras.cc b/src/clients/cave/match_extras.cc
index dacbccd..9b0b08b 100644
--- a/src/clients/cave/match_extras.cc
+++ b/src/clients/cave/match_extras.cc
@@ -24,9 +24,9 @@
using namespace paludis;
extern "C" bool
-cave_match_extras_match_regex(const std::string & text, const std::string & pattern_str)
+cave_match_extras_match_regex(const std::string & text, const std::string & pattern_str, bool case_sensitive)
{
- const pcrecpp::RE pattern(pattern_str, pcrecpp::RE_Options().set_caseless(true));
+ const pcrecpp::RE pattern(pattern_str, pcrecpp::RE_Options().set_caseless(!case_sensitive));
if (! pattern.error().empty())
throw args::DoHelp("Pattern '" + pattern_str + "' error: " + pattern.error());
diff --git a/src/clients/cave/match_extras.hh b/src/clients/cave/match_extras.hh
index 73812d3..9e4ad7f 100644
--- a/src/clients/cave/match_extras.hh
+++ b/src/clients/cave/match_extras.hh
@@ -23,6 +23,6 @@
#include <paludis/util/attributes.hh>
#include <string>
-extern "C" bool cave_match_extras_match_regex(const std::string &, const std::string &) PALUDIS_VISIBLE PALUDIS_ATTRIBUTE((warn_unused_result));
+extern "C" bool cave_match_extras_match_regex(const std::string &, const std::string &, bool) PALUDIS_VISIBLE PALUDIS_ATTRIBUTE((warn_unused_result));
#endif