summaryrefslogtreecommitdiff
path: root/src/cmdline/cmdline_util.cc
diff options
context:
space:
mode:
authorDaniel Hartwig <mandyke@gmail.com>2012-06-08 17:11:48 +0800
committerDaniel Hartwig <mandyke@gmail.com>2012-06-08 17:11:48 +0800
commit24e4c8dab029ac59437f9392a8d3732c6893a3e3 (patch)
tree65da6aa7aef3c16ac40b3c5a4536a025172d8272 /src/cmdline/cmdline_util.cc
parent85d6f30683f6ede36e7a6a0e78e6e7efa079e056 (diff)
downloadaptitude-24e4c8dab029ac59437f9392a8d3732c6893a3e3.tar.gz
Add pkgset helpers
Ported some code from apt cacheset.cc to populate a pkgset. The function pkgset_from_string will fill a given pkgset with either the exactly named package, or the packages matched by a string predicated by is_pattern. In the future, we should look to directly use the apt cacheset infrastructure. This is currently blocked by our custom implementation of pkgCacheFile (see aptcache.h). Errors that occur in looking up the package (such as no package with that name, or an invalid search pattern) are pushed to the global error list with a customizable error_type (default: ERROR).
Diffstat (limited to 'src/cmdline/cmdline_util.cc')
-rw-r--r--src/cmdline/cmdline_util.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/cmdline/cmdline_util.cc b/src/cmdline/cmdline_util.cc
index 1165e3e4..22045fcb 100644
--- a/src/cmdline/cmdline_util.cc
+++ b/src/cmdline/cmdline_util.cc
@@ -812,5 +812,71 @@ namespace aptitude
return output;
}
+
+ pkgCache::PkgIterator pkg_from_name(const string &str,
+ GlobalError::MsgType error_type)
+ {
+ pkgCache::PkgIterator pkg = (*apt_cache_file)->FindPkg(str.c_str());
+
+ if(pkg.end() == true)
+ _error->Insert(error_type,
+ _("Unable to locate package %s"), str.c_str());
+ return pkg;
+ }
+
+ bool pkgset_from_pattern(pkgset *packages, const string &pattern,
+ GlobalError::MsgType error_type)
+ {
+ using aptitude::matching::pkg_results_list;
+ using aptitude::matching::search_cache;
+ using cwidget::util::ref_ptr;
+
+ if(aptitude::matching::is_pattern(pattern) == false)
+ return false;
+
+ ref_ptr<aptitude::matching::pattern> p(aptitude::matching::parse(pattern));
+ if(p.valid() == false)
+ return _error->Insert(error_type,
+ _("Unable to parse pattern '%s'"), pattern.c_str());
+
+ pkg_results_list matches;
+ ref_ptr<search_cache> search_info(search_cache::create());
+ search(p, search_info,
+ matches,
+ *apt_cache_file,
+ *apt_package_records);
+
+ if(matches.empty() == true)
+ return _error->Insert(error_type,
+ _("Couldn't find any package for pattern '%s'"),
+ pattern.c_str());
+
+ for(pkg_results_list::const_iterator it = matches.begin();
+ it != matches.end();
+ ++it)
+ {
+ packages->insert(it->first);
+ }
+ return true;
+ }
+
+ bool pkgset_from_string(pkgset *packages, const string &str,
+ GlobalError::MsgType error_type)
+ {
+ bool found = true;
+ _error->PushToStack();
+
+ pkgCache::PkgIterator pkg = pkg_from_name(str, error_type);
+ if(pkg.end() == false)
+ packages->insert(pkg);
+ else if(pkgset_from_pattern(packages, str, error_type) == false)
+ found = false;
+
+ if(found == true)
+ _error->RevertToStack();
+ else
+ _error->MergeWithStack();
+ return found;
+ }
}
}