doSearch() changed to take a functor as an argument
authorJan Kupec <jkupec@suse.cz>
Fri, 20 Oct 2006 15:59:31 +0000 (15:59 +0000)
committerJan Kupec <jkupec@suse.cz>
Fri, 20 Oct 2006 15:59:31 +0000 (15:59 +0000)
enum used instead of bools _installed_only, _uninstalled_only
fixed search by type bug

tools/zmart/zypper-search.cc
tools/zmart/zypper-search.h
tools/zmart/zypper.cc

index d2fffc1..b14a6aa 100644 (file)
@@ -45,12 +45,12 @@ bool ZyppSearch::init () const {
       " You will not be able to install stuff" << endl;
   }
 
-  if (!_options.uninstalledOnly()) {
+  if (_options.installedFilter() != ZyppSearchOptions::UNINSTALLED_ONLY) {
     cerr_v << "loading target" << endl;
     load_target();
   }
 
-  if (!_options.installedOnly()) {
+  if (_options.installedFilter() != ZyppSearchOptions::INSTALLED_ONLY) {
     cerr_v << "loading sources" << endl;
     load_sources();
   }
@@ -58,35 +58,36 @@ bool ZyppSearch::init () const {
   return true;
 }
 
-// TODO comment
-void ZyppSearch::doSearch(Table & table) {
+void ZyppSearch::doSearch(const boost::function<void(const PoolItem &)> & f) {
   ResPool pool = getZYpp()->pool();
 
   // search for specific resolvable type only
   if (_options.kind() != Resolvable::Kind()) {
+    cerr_vv << "Search by type" << endl;
     setupRegexp();
     for (ResPool::byKind_iterator it = pool.byKindBegin(_options.kind());
         it != pool.byKindEnd(_options.kind()); ++it) {
-      if (match(*it)) table << createRow(*it);
+      if (match(*it)) f(*it);
     }
   }
   // 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 &&
+  else if (_options.matchExact() && _qstrings.size() == 1 &&
       _qstrings[0].find('*') == string::npos &&
       _qstrings[0].find('?') == string::npos) {
     cerr_vv << "Exact name match" << endl;
     for (ResPool::byName_iterator it = pool.byNameBegin(_qstrings[0]);
         it != pool.byNameEnd(_qstrings[0]); ++it) {
-      table << createRow(*it);
+      f(*it); //table << createRow(*it);
     }
   }
   // search among all resolvables
   else {
+    cerr_vv << "Search among all resolvables" << endl;
     setupRegexp();
     for (ResPool::const_iterator it = pool.begin(); it != pool.end(); ++it) {
-      if (match(*it)) table << createRow(*it);
+      if (match(*it)) f(*it); //table << createRow(*it);
     }
   }
 }
@@ -207,16 +208,6 @@ bool ZyppSearch::match(const PoolItem & pool_item) {
       false);
 }
 
-TableRow ZyppSearch::createRow(const PoolItem & pool_item) {
-  TableRow row;
-  row << (pool_item.status().isInstalled() ? "i" : "")
-      << pool_item.resolvable()->source().alias()
-      << "" // TODO what about Bundle?
-      << pool_item.resolvable()->name()
-      << pool_item.resolvable()->edition().asString()
-      << pool_item.resolvable()->arch().asString();
-  return row;
-}
 // Local Variables:
 // c-basic-offset: 2
 // End:
index 471562c..fcdc117 100644 (file)
 
 #include <string>
 #include <boost/regex.hpp>
+#include <boost/function.hpp>
 #include <zypp/ZYpp.h>
 
-#include "zypper-tabulator.h"
-
 /**
  * @brief: search options
  */
 class ZyppSearchOptions {
 public:
+  enum InsFilter {
+    ALL,
+    INSTALLED_ONLY,
+    UNINSTALLED_ONLY
+  };
+
   ZyppSearchOptions () :
-    _installed_only(false), _uninstalled_only(false),
+    _ifilter(ALL),
     _match_all(true), _match_words(false), _match_exact(false),
     _search_descriptions(false), _case_sensitive(false),
     _kind(zypp::Resolvable::Kind())
@@ -31,8 +36,7 @@ public:
 
   void resolveConflicts();
 
-  bool installedOnly() const { return _installed_only; }
-  bool uninstalledOnly() const { return _uninstalled_only; }
+  InsFilter installedFilter() const { return _ifilter; }
   bool matchAll() const { return _match_all; }
   bool matchAny() const { return !_match_all; }
   bool matchWords() const { return _match_words; }
@@ -41,12 +45,7 @@ public:
   bool caseSensitive() const { return _case_sensitive; }
   zypp::Resolvable::Kind kind() const { return _kind; }
 
-  void setInstalledOnly(const bool installed_only = true) {
-    _installed_only = installed_only;
-  }
-  void setUnInstalledOnly(const bool uninstalled_only = true) {
-    _uninstalled_only = uninstalled_only;
-  }
+  void setInstalledFilter(const InsFilter ifilter) { _ifilter =  ifilter; }
   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; }
@@ -56,8 +55,7 @@ public:
   void setKind(const zypp::Resolvable::Kind & kind) { _kind = kind; }
 
 private:
-  bool _installed_only;
-  bool _uninstalled_only;
+  InsFilter _ifilter;
   bool _match_all;
   bool _match_words;
   bool _match_exact;
@@ -72,7 +70,7 @@ private:
 class ZyppSearch {
 public:
   ZyppSearch (const ZyppSearchOptions & options, const vector<std::string> & qstrings = vector<string>());
-  void doSearch(Table & table);
+  void doSearch(const boost::function<void(const zypp::PoolItem &)> & f);
 
 private:
   const ZyppSearchOptions & _options;
index 5545a4c..8eb9580 100644 (file)
@@ -622,6 +622,11 @@ int main(int argc, char **argv)
 
   // --------------------------( search )-------------------------------------
 
+  // FIXME --uninstalled-only does not really exclude installed resolvables
+  // FIXME search for all resolvables displays installed packages twice
+  // FIXME source (catalog) information missing for installed packages
+  // TODO print rug's v status  
+
   if (command == "search" || command == "se") {
     ZyppSearchOptions options;
 
@@ -631,9 +636,9 @@ int main(int argc, char **argv)
     }
 
     if (gSettings.disable_system_resolvables || copts.count("uninstalled-only"))
-      options.setUnInstalledOnly();
+      options.setInstalledFilter(ZyppSearchOptions::UNINSTALLED_ONLY);
 
-    if (copts.count("installed-only")) options.setInstalledOnly();
+    if (copts.count("installed-only")) options.setInstalledFilter(ZyppSearchOptions::INSTALLED_ONLY);
     if (copts.count("match-any")) options.setMatchAny();
     if (copts.count("match-words")) options.setMatchWords();
     if (copts.count("match-exact")) options.setMatchExact();
@@ -649,20 +654,14 @@ int main(int argc, char **argv)
       }
       options.setKind(kind);
     }
-    
+
     options.resolveConflicts();
 
     Table t;
     t.style(Ascii);
 
-    TableHeader header;
-    // do not change the header as the structure of the table is at the time
-    // hardcoded in ZyppSearch::createRow(const PoolItem & pool_item)
-    header << "S" << "Catalog" << "Bundle" << "Name" << "Version" << "Arch";
-    t << header; 
-
     ZyppSearch search(options,arguments);
-    search.doSearch(t);
+    search.doSearch(FillTable(t));
 
     if (t.empty())
       cout << "No packages found." << endl;