packageset.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Simple wrapper around a std::set to provide a similar interface to
  5. a set of packages as to the complete set of all packages in the
  6. pkgCache.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/packageset.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apti18n.h>
  14. #include <regex.h>
  15. /*}}}*/
  16. namespace APT {
  17. // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
  18. PackageSet PackageSet::FromRegEx(pkgCache &Cache, const char * const pattern, std::ostream &out) {
  19. PackageSet pkgset;
  20. const char * I;
  21. for (I = pattern; *I != 0; I++)
  22. if (*I == '.' || *I == '?' || *I == '+' || *I == '*' ||
  23. *I == '|' || *I == '[' || *I == '^' || *I == '$')
  24. break;
  25. if (*I == 0)
  26. return pkgset;
  27. regex_t Pattern;
  28. int Res;
  29. if ((Res = regcomp(&Pattern, pattern , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
  30. char Error[300];
  31. regerror(Res, &Pattern, Error, sizeof(Error));
  32. _error->Error(_("Regex compilation error - %s"), Error);
  33. return pkgset;
  34. }
  35. for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp)
  36. {
  37. if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
  38. continue;
  39. pkgCache::PkgIterator Pkg = Grp.FindPkg("native");
  40. if (unlikely(Pkg.end() == true))
  41. // FIXME: Fallback to different architectures here?
  42. continue;
  43. ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
  44. Pkg.Name(), pattern);
  45. pkgset.insert(Pkg);
  46. }
  47. regfree(&Pattern);
  48. return pkgset;
  49. }
  50. /*}}}*/
  51. }