cachefilter.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 <string>
  12. #include <regex.h>
  13. #include <apti18n.h>
  14. /*}}}*/
  15. namespace APT {
  16. namespace CacheFilter {
  17. PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
  18. pattern = new regex_t;
  19. int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
  20. if (Res == 0)
  21. return;
  22. delete pattern;
  23. pattern = NULL;
  24. char Error[300];
  25. regerror(Res, pattern, Error, sizeof(Error));
  26. _error->Error(_("Regex compilation error - %s"), Error);
  27. }
  28. /*}}}*/
  29. bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  30. if (unlikely(pattern == NULL))
  31. return false;
  32. else
  33. return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
  34. }
  35. /*}}}*/
  36. bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
  37. if (unlikely(pattern == NULL))
  38. return false;
  39. else
  40. return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
  41. }
  42. /*}}}*/
  43. PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
  44. if (pattern == NULL)
  45. return;
  46. regfree(pattern);
  47. delete pattern;
  48. }
  49. /*}}}*/
  50. }
  51. }