cachefilter.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // CompleteArch to <kernel>-<cpu> tuple /*{{{*/
  53. //----------------------------------------------------------------------
  54. /* The complete architecture, consisting of <kernel>-<cpu>. */
  55. static std::string CompleteArch(std::string const &arch) {
  56. if (arch.find('-') != std::string::npos) {
  57. // ensure that only -any- is replaced and not something like company-
  58. std::string complete = std::string("-").append(arch).append("-");
  59. complete = SubstVar(complete, "-any-", "-*-");
  60. complete = complete.substr(1, complete.size()-2);
  61. return complete;
  62. }
  63. else if (arch == "any") return "*-*";
  64. else return "linux-" + arch;
  65. }
  66. /*}}}*/
  67. PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const isPattern) :/*{{{*/
  68. literal(pattern), isPattern(isPattern), d(NULL) {
  69. complete = CompleteArch(pattern);
  70. }
  71. /*}}}*/
  72. bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {/*{{{*/
  73. if (strcmp(literal.c_str(), arch) == 0 ||
  74. strcmp(complete.c_str(), arch) == 0)
  75. return true;
  76. std::string const pkgarch = CompleteArch(arch);
  77. if (isPattern == true)
  78. return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
  79. return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
  80. }
  81. /*}}}*/
  82. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  83. return (*this)(Pkg.Arch());
  84. }
  85. /*}}}*/
  86. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::VerIterator const &Ver) {/*{{{*/
  87. return (*this)(Ver.ParentPkg());
  88. }
  89. /*}}}*/
  90. PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() { /*{{{*/
  91. }
  92. /*}}}*/
  93. }
  94. }