]> git.saurik.com Git - apt.git/blob - apt-private/private-list.cc
Merge remote-tracking branch 'upstream/debian/sid' into feature/apt-manpage
[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 #ifdef PACKAGE_MATCHER_ABI_COMPAT
44 #define PackageMatcher PackageNameMatchesFnmatch
45 #endif
46 public:
47 PackageNameMatcher(const char **patterns)
48 {
49 for(int i=0; patterns[i] != NULL; ++i)
50 {
51 std::string pattern = patterns[i];
52 #ifdef PACKAGE_MATCHER_ABI_COMPAT
53 APT::CacheFilter::PackageNameMatchesFnmatch *cachefilter = NULL;
54 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
55 #else
56 APT::CacheFilter::PackageMatcher *cachefilter = NULL;
57 if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
58 cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
59 else
60 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
61 #endif
62 filters.push_back(cachefilter);
63 }
64 }
65 virtual ~PackageNameMatcher()
66 {
67 for(J=filters.begin(); J != filters.end(); ++J)
68 delete *J;
69 }
70 virtual bool operator () (const pkgCache::PkgIterator &P)
71 {
72 for(J=filters.begin(); J != filters.end(); ++J)
73 {
74 APT::CacheFilter::PackageMatcher *cachefilter = *J;
75 if((*cachefilter)(P))
76 return true;
77 }
78 return false;
79 }
80
81 private:
82 std::vector<APT::CacheFilter::PackageMatcher*> filters;
83 std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
84 #undef PackageMatcher
85 };
86 /*}}}*/
87 static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
88 pkgCache::PkgIterator P,
89 std::ostream &outs,
90 bool include_summary=true)
91 {
92 for (pkgCache::VerIterator Ver = P.VersionList();
93 Ver.end() == false; ++Ver)
94 {
95 ListSingleVersion(CacheFile, records, Ver, outs, include_summary);
96 outs << "\n";
97 }
98 }
99 /*}}}*/
100 // list - list package based on criteria /*{{{*/
101 // ---------------------------------------------------------------------
102 bool List(CommandLine &Cmd)
103 {
104 pkgCacheFile CacheFile;
105 pkgCache *Cache = CacheFile.GetPkgCache();
106 pkgRecords records(CacheFile);
107
108 if (unlikely(Cache == NULL))
109 return false;
110
111 const char **patterns;
112 const char *all_pattern[] = { "*", NULL};
113
114 if (strv_length(Cmd.FileList + 1) == 0)
115 {
116 patterns = all_pattern;
117 } else {
118 patterns = Cmd.FileList + 1;
119 }
120
121 std::map<std::string, std::string> output_map;
122 std::map<std::string, std::string>::const_iterator K;
123
124 bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary");
125
126 PackageNameMatcher matcher(patterns);
127 LocalitySortedVersionSet bag;
128 OpTextProgress progress(*_config);
129 progress.OverallProgress(0,
130 Cache->Head().PackageCount,
131 Cache->Head().PackageCount,
132 _("Listing"));
133 GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
134 for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
135 {
136 std::stringstream outs;
137 if(_config->FindB("APT::Cmd::All-Versions", false) == true)
138 {
139 ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary);
140 output_map.insert(std::make_pair<std::string, std::string>(
141 V.ParentPkg().Name(), outs.str()));
142 } else {
143 ListSingleVersion(CacheFile, records, V, outs, includeSummary);
144 output_map.insert(std::make_pair<std::string, std::string>(
145 V.ParentPkg().Name(), outs.str()));
146 }
147 }
148
149 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
150 // output the sorted map
151 for (K = output_map.begin(); K != output_map.end(); ++K)
152 std::cout << (*K).second << std::endl;
153
154
155 return true;
156 }
157