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