--match-exact option added (search for resolvables with exact names)
authorJan Kupec <jkupec@suse.cz>
Mon, 16 Oct 2006 15:27:26 +0000 (15:27 +0000)
committerJan Kupec <jkupec@suse.cz>
Mon, 16 Oct 2006 15:27:26 +0000 (15:27 +0000)
tools/zmart/zypper-search.cc
tools/zmart/zypper-search.h
tools/zmart/zypper.cc

index 483d434..7a9db06 100644 (file)
@@ -12,6 +12,17 @@ using namespace boost;
 extern RuntimeData gData;
 extern Settings gSettings;
 
+void ZyppSearchOptions::resolveConflicts() {
+  if (matchExact()) {
+    // --match-all does not make sense here
+    setMatchAny();
+    // the same goes for search in descriptions
+    setSearchDescriptions(false);
+  }
+
+  // ??? should we notify user about conflict resolutions?
+}
+
 ZyppSearch::ZyppSearch (const ZyppSearchOptions & options, const vector<string> & qstrings) :
     _options(options), _qstrings(qstrings) {
   init();
@@ -51,17 +62,28 @@ bool ZyppSearch::init () const {
 void ZyppSearch::doSearch(Table & table) {
   ResPool pool = getZYpp()->pool();
 
-  setupRegexp();
-
   // search for specific resolvable type only
   if (_options.kind() != Resolvable::Kind()) {
+    setupRegexp();
     for (ResPool::byKind_iterator it = pool.byKindBegin(_options.kind());
         it != pool.byKindEnd(_options.kind()); ++it) {
       if (match(*it)) table << createRow(*it);
     }
   }
-  // search for all resolvables
+  // search for exact package using byName_iterator
+  // usable only if there is only one query string and if this string
+  // doesn't contain wildcards
+  if (_options.matchExact() && _qstrings.size() == 1 &&
+      _qstrings[0].find('*') == string::npos &&
+      _qstrings[0].find('?') == string::npos) {
+    for (ResPool::byName_iterator it = pool.byNameBegin(_qstrings[0]);
+        it != pool.byNameEnd(_qstrings[0]); ++it) {
+      table << createRow(*it);
+    }
+  }
+  // search among all resolvables
   else {
+    setupRegexp();
     for (ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it) {
       if (match(*it)) table << createRow(*it);
     }
@@ -92,15 +114,21 @@ void ZyppSearch::setupRegexp() {
   string regstr;
 
   if (_qstrings.size() == 0) regstr = ".*";
-  else if (_qstrings.size() == 1)
-    regstr = ".*" + WB + wildcards2regex(_qstrings[0]) + WB + ".*";
+  else if (_qstrings.size() == 1) {
+    if (_options.matchExact())
+      regstr = wildcards2regex(_qstrings[0]);
+    else
+      regstr = ".*" + WB + wildcards2regex(_qstrings[0]) + WB + ".*";
+  }
   else {
     vector<string>::const_iterator it = _qstrings.begin();
 
     if (_options.matchAll())
       regstr = "(?=.*" + WB + wildcards2regex(*it) + WB + ")";
-    else
-      regstr = ".*" + WB + "(" + wildcards2regex(*it);
+    else {
+      if (!_options.matchExact()) regstr = ".*";
+      regstr += WB + "(" + wildcards2regex(*it);
+    }
 
     ++it;
 
@@ -113,8 +141,10 @@ void ZyppSearch::setupRegexp() {
 
     if (_options.matchAll())
       regstr += ".*";
-    else
-      regstr += ")" + WB + ".*";
+    else {
+      regstr += ")" + WB;
+      if (!_options.matchExact()) regstr += ".*";
+    }
   }
 
   cerr_vv << "using regex: " << regstr << endl;
index a0dad45..bedcfaa 100644 (file)
@@ -27,11 +27,14 @@ public:
     _match_words(false), _kind(zypp::Resolvable::Kind())
     {}
 
+  void resolveConflicts();
+
   bool installedOnly() const { return _installed_only; }
   bool uninstalledOnly() const { return _uninstalled_only; }
   bool matchAll() const { return _match_all; }
   bool matchAny() const { return !_match_all; }
   bool matchWords() const { return _match_words; }
+  bool matchExact() const { return _match_exact; }
   bool searchDescriptions() const { return _search_descriptions; }
   bool caseSensitive() const { return _case_sensitive; }
   zypp::Resolvable::Kind kind() const { return _kind; }
@@ -45,6 +48,7 @@ public:
   void setMatchAll(const bool match_all = true) { _match_all = match_all; }
   void setMatchAny(const bool match_any = true) { _match_all = !match_any; }
   void setMatchWords(const bool match_words = true) { _match_words = match_words; }
+  void setMatchExact(const bool match_exact = true) { _match_exact = match_exact; }
   void setSearchDescriptions(const bool search_descriptions = true) { _search_descriptions = search_descriptions; }
   void setCaseSensitive(const bool case_sensitive = true) { _case_sensitive = case_sensitive; }
   void setKind(const zypp::Resolvable::Kind & kind) { _kind = kind; }
@@ -54,6 +58,7 @@ private:
   bool _uninstalled_only;
   bool _match_all;
   bool _match_words;
+  bool _match_exact;
   bool _search_descriptions;
   bool _case_sensitive;
   zypp::Resolvable::Kind _kind;
index 46a7ca1..91469f1 100644 (file)
@@ -309,6 +309,7 @@ int main(int argc, char **argv)
       {"match-any", no_argument, 0, 0},
       {"match-substrings", no_argument, 0, 0},
       {"match-words", no_argument, 0, 0},
+      {"match-exact", no_argument, 0, 0},
       {"search-descriptions", no_argument, 0, 'd'},
       {"case-sensitive", no_argument, 0, 'c'},
       {"type",    required_argument, 0, 't'},
@@ -327,6 +328,7 @@ int main(int argc, char **argv)
       "    --match-any            Search for a match to any of the search strings\n"
       "    --match-substrings     Matches for search strings may be partial words (default)\n"
       "    --match-words          Matches for search strings may only be whole words\n"
+      "    --match-exact          Searches for an exact package name\n"
       "-d, --search-descriptions  Search also in package summaries and descriptions.\n"
       "-c, --case-sensitive       Perform case-sensitive search.\n"
       "-i, --installed-only       Show only packages that are already installed.\n"
@@ -334,6 +336,8 @@ int main(int argc, char **argv)
       "-t, --type                 Search only for packages of specified type.\n"
       "    --sort-by-name         Sort packages by name (default).\n"
       "    --sort-by-catalog      Sort packages by catalog.\n" // ??? catalog/source?
+      "\n"
+      "* and ? wildcards can also be used within search strings.\n"
       ;
   }
   else {
@@ -651,6 +655,7 @@ int main(int argc, char **argv)
     if (copts.count("installed-only")) options.setInstalledOnly();
     if (copts.count("match-any")) options.setMatchAny();
     if (copts.count("match-words")) options.setMatchWords();
+    if (copts.count("match-exact")) options.setMatchExact();
     if (copts.count("search-descriptions")) options.setSearchDescriptions();
     if (copts.count("case-sensitive")) options.setCaseSensitive();
 
@@ -663,6 +668,8 @@ int main(int argc, char **argv)
       }
       options.setKind(kind);
     }
+    
+    options.resolveConflicts();
 
     Table t;
     t.style(Ascii);