cachefilter.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /** \file cachefilter.h
  4. Collection of functor classes */
  5. /*}}}*/
  6. // Include Files /*{{{*/
  7. #include <config.h>
  8. #include <apt-pkg/cachefilter.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <string>
  13. #include <regex.h>
  14. #include <fnmatch.h>
  15. #include <apti18n.h>
  16. /*}}}*/
  17. namespace APT {
  18. namespace CacheFilter {
  19. PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
  20. pattern = new regex_t;
  21. int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
  22. if (Res == 0)
  23. return;
  24. delete pattern;
  25. pattern = NULL;
  26. char Error[300];
  27. regerror(Res, pattern, Error, sizeof(Error));
  28. _error->Error(_("Regex compilation error - %s"), Error);
  29. }
  30. /*}}}*/
  31. bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  32. if (unlikely(pattern == NULL))
  33. return false;
  34. else
  35. return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
  36. }
  37. /*}}}*/
  38. bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
  39. if (unlikely(pattern == NULL))
  40. return false;
  41. else
  42. return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
  43. }
  44. /*}}}*/
  45. PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
  46. if (pattern == NULL)
  47. return;
  48. regfree(pattern);
  49. delete pattern;
  50. }
  51. /*}}}*/
  52. // Fnmatch support /*{{{*/
  53. //----------------------------------------------------------------------
  54. bool PackageNameMatchesFnmatch::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  55. return fnmatch(Pattern.c_str(), Pkg.Name(), FNM_CASEFOLD) == 0;
  56. }
  57. /*}}}*/
  58. bool PackageNameMatchesFnmatch::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
  59. return fnmatch(Pattern.c_str(), Grp.Name(), FNM_CASEFOLD) == 0;
  60. }
  61. /*}}}*/
  62. // CompleteArch to <kernel>-<cpu> tuple /*{{{*/
  63. //----------------------------------------------------------------------
  64. /* The complete architecture, consisting of <kernel>-<cpu>. */
  65. static std::string CompleteArch(std::string const &arch) {
  66. if (arch.find('-') != std::string::npos) {
  67. // ensure that only -any- is replaced and not something like company-
  68. std::string complete = std::string("-").append(arch).append("-");
  69. complete = SubstVar(complete, "-any-", "-*-");
  70. complete = complete.substr(1, complete.size()-2);
  71. return complete;
  72. }
  73. else if (arch == "any") return "*-*";
  74. else return "linux-" + arch;
  75. }
  76. /*}}}*/
  77. PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const isPattern) :/*{{{*/
  78. literal(pattern), complete(CompleteArch(pattern)), isPattern(isPattern), d(NULL) {
  79. }
  80. /*}}}*/
  81. bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {/*{{{*/
  82. if (strcmp(literal.c_str(), arch) == 0 ||
  83. strcmp(complete.c_str(), arch) == 0)
  84. return true;
  85. std::string const pkgarch = CompleteArch(arch);
  86. if (isPattern == true)
  87. return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
  88. return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
  89. }
  90. /*}}}*/
  91. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  92. return (*this)(Pkg.Arch());
  93. }
  94. /*}}}*/
  95. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::VerIterator const &Ver) {/*{{{*/
  96. return (*this)(Ver.ParentPkg());
  97. }
  98. /*}}}*/
  99. PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() { /*{{{*/
  100. }
  101. /*}}}*/
  102. }
  103. }