]> git.saurik.com Git - apt.git/blame - apt-private/private-list.cc
Merge branch 'debian/sid' into debian/experimental
[apt.git] / apt-private / private-list.cc
CommitLineData
b9179170
MV
1// Include Files /*{{{*/
2#include <config.h>
3
4#include <apt-pkg/error.h>
5#include <apt-pkg/cachefile.h>
6#include <apt-pkg/cachefilter.h>
7#include <apt-pkg/cacheset.h>
8#include <apt-pkg/init.h>
9#include <apt-pkg/progress.h>
10#include <apt-pkg/sourcelist.h>
11#include <apt-pkg/cmndline.h>
12#include <apt-pkg/strutl.h>
13#include <apt-pkg/fileutl.h>
14#include <apt-pkg/pkgrecords.h>
15#include <apt-pkg/srcrecords.h>
16#include <apt-pkg/version.h>
17#include <apt-pkg/policy.h>
18#include <apt-pkg/tagfile.h>
19#include <apt-pkg/algorithms.h>
20#include <apt-pkg/sptr.h>
21#include <apt-pkg/pkgsystem.h>
22#include <apt-pkg/indexfile.h>
23#include <apt-pkg/metaindex.h>
24
25#include <sstream>
26#include <vector>
27#include <utility>
28#include <cassert>
29#include <locale.h>
30#include <iostream>
31#include <unistd.h>
32#include <errno.h>
33#include <regex.h>
34#include <stdio.h>
35#include <algorithm>
36
37#include "private-cmndline.h"
38#include "private-list.h"
39#include "private-output.h"
40#include "private-cacheset.h"
41
42#include <apti18n.h>
43 /*}}}*/
44
ee0167c4 45struct PackageSortAlphabetic /*{{{*/
b9179170
MV
46{
47 bool operator () (const pkgCache::PkgIterator &p_lhs,
48 const pkgCache::PkgIterator &p_rhs)
49 {
50 const std::string &l_name = p_lhs.FullName(true);
51 const std::string &r_name = p_rhs.FullName(true);
52 return (l_name < r_name);
53 }
54};
dd4d9729
MV
55#ifdef PACKAGE_MATCHER_ABI_COMPAT
56#define PackageMatcher PackageNameMatchesFnmatch
57#endif
b9179170
MV
58 public:
59 PackageNameMatcher(const char **patterns)
60 {
9ce3cfc9 61 for(int i=0; patterns[i] != NULL; ++i)
b9179170
MV
62 {
63 std::string pattern = patterns[i];
64 APT::CacheFilter::PackageMatcher *cachefilter = NULL;
c1a61d1c 65 if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
b9179170
MV
66 cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
67 else
68 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
69 filters.push_back(cachefilter);
70 }
71 }
72 virtual ~PackageNameMatcher()
73 {
9ce3cfc9 74 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
75 delete *J;
76 }
77 virtual bool operator () (const pkgCache::PkgIterator &P)
78 {
9ce3cfc9 79 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
80 {
81 APT::CacheFilter::PackageMatcher *cachefilter = *J;
82 if((*cachefilter)(P))
83 return true;
84 }
85 return false;
86 }
87
88private:
89 std::vector<APT::CacheFilter::PackageMatcher*> filters;
90 std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
dd4d9729 91 #undef PackageMatcher
b9179170 92};
ee0167c4
DK
93 /*}}}*/
94void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/
b9179170 95 pkgCache::PkgIterator P,
14109555
MV
96 std::ostream &outs,
97 bool include_summary=true)
b9179170
MV
98{
99 for (pkgCache::VerIterator Ver = P.VersionList();
796673c3 100 Ver.end() == false; ++Ver)
14109555
MV
101 {
102 ListSingleVersion(CacheFile, records, Ver, outs, include_summary);
103 outs << "\n";
104 }
b9179170 105}
ee0167c4 106 /*}}}*/
b9179170
MV
107// list - list package based on criteria /*{{{*/
108// ---------------------------------------------------------------------
109bool List(CommandLine &Cmd)
110{
111 pkgCacheFile CacheFile;
112 pkgCache *Cache = CacheFile.GetPkgCache();
113 pkgRecords records(CacheFile);
114
115 if (unlikely(Cache == NULL))
116 return false;
117
118 const char **patterns;
119 const char *all_pattern[] = { "*", NULL};
120
121 if (strv_length(Cmd.FileList + 1) == 0)
122 {
123 patterns = all_pattern;
124 } else {
125 patterns = Cmd.FileList + 1;
126 }
127
128 std::map<std::string, std::string> output_map;
129 std::map<std::string, std::string>::const_iterator K;
130
e1dc051a
MV
131 bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary");
132
b9179170
MV
133 PackageNameMatcher matcher(patterns);
134 LocalitySortedVersionSet bag;
14109555 135 OpTextProgress progress(*_config);
b9179170
MV
136 progress.OverallProgress(0,
137 Cache->Head().PackageCount,
138 Cache->Head().PackageCount,
139 _("Listing"));
140 GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
9ce3cfc9 141 for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
b9179170
MV
142 {
143 std::stringstream outs;
c1a61d1c 144 if(_config->FindB("APT::Cmd::All-Versions", false) == true)
b9179170 145 {
14109555 146 ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary);
b9179170
MV
147 output_map.insert(std::make_pair<std::string, std::string>(
148 V.ParentPkg().Name(), outs.str()));
149 } else {
e1dc051a 150 ListSingleVersion(CacheFile, records, V, outs, includeSummary);
b9179170
MV
151 output_map.insert(std::make_pair<std::string, std::string>(
152 V.ParentPkg().Name(), outs.str()));
153 }
154 }
155
156 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
157 // output the sorted map
9ce3cfc9 158 for (K = output_map.begin(); K != output_map.end(); ++K)
b9179170
MV
159 std::cout << (*K).second << std::endl;
160
161
162 return true;
163}
164