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