private-list.cc 5.0 KB

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