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