]> git.saurik.com Git - apt.git/commitdiff
- use the new MatchAgainstConfig for the DefaultRootSetFunc
authorDavid Kalnischkies <kalnischkies@gmail.com>
Sat, 26 Jun 2010 11:29:24 +0000 (13:29 +0200)
committerDavid Kalnischkies <kalnischkies@gmail.com>
Sat, 26 Jun 2010 11:29:24 +0000 (13:29 +0200)
* apt-pkg/contrib/configuration.{cc,h}:
  - add a wrapper to match strings against configurable regex patterns

apt-pkg/contrib/configuration.cc
apt-pkg/contrib/configuration.h
apt-pkg/depcache.cc
apt-pkg/depcache.h
debian/changelog

index 9129d92f02a1f35b6e8d6e84217d61e161d552f8..81cc87d15e93b434e1a0003c65bdcc96b6ec7866 100644 (file)
@@ -843,3 +843,46 @@ bool ReadConfigDir(Configuration &Conf,const string &Dir,
    return true;
 }
                                                                        /*}}}*/
+// MatchAgainstConfig Constructor                                      /*{{{*/
+Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
+{
+   std::vector<std::string> const strings = _config->FindVector(Config);
+   for (std::vector<std::string>::const_iterator s = strings.begin();
+       s != strings.end(); ++s)
+   {
+      regex_t *p = new regex_t;
+      if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0)
+        patterns.push_back(p);
+      else
+      {
+        regfree(p);
+        delete p;
+        _error->Warning("Regex compilation error for '%s' in configuration option '%s'",
+                               s->c_str(), Config);
+      }
+    }
+
+}
+                                                                       /*}}}*/
+// MatchAgainstConfig Destructor                                       /*{{{*/
+Configuration::MatchAgainstConfig::~MatchAgainstConfig()
+{
+   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
+       p != patterns.end(); ++p)
+   {
+      regfree(*p);
+      delete *p;
+   }
+}
+                                                                       /*}}}*/
+// MatchAgainstConfig::Match - returns true if a pattern matches       /*{{{*/
+bool Configuration::MatchAgainstConfig::Match(char const * str) const
+{
+   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
+       p != patterns.end(); ++p)
+      if (regexec(*p, str, 0, 0, 0) == 0)
+        return true;
+
+   return false;
+}
+                                                                       /*}}}*/
index 2494c1d7c6d24713c0c2205c90836035555b01fd..cbe18e4e56e6e7fa24f03f286c3582ee538405b3 100644 (file)
@@ -28,7 +28,7 @@
 #ifndef PKGLIB_CONFIGURATION_H
 #define PKGLIB_CONFIGURATION_H
 
-
+#include <regex.h>
 
 #include <string>
 #include <vector>
@@ -104,6 +104,23 @@ class Configuration
    Configuration(const Item *Root);
    Configuration();
    ~Configuration();
+
+   /** \brief match a string against a configurable list of patterns */
+   class MatchAgainstConfig
+   {
+     std::vector<regex_t *> patterns;
+
+   public:
+     MatchAgainstConfig(char const * Config);
+     virtual ~MatchAgainstConfig();
+
+     /** \brief Returns \b true for a string matching one of the patterns */
+     bool Match(char const * str) const;
+     bool Match(std::string const &str) const { return Match(str.c_str()); };
+
+     /** \brief returns if the matcher setup was successful */
+     bool wasConstructedSuccessfully() const { return patterns.empty() == false; }
+   };
 };
 
 extern Configuration *_config;
index d082b8404075f98eb4b36e8a1ee825289db62c39..8bca3e36e0eca5a27b7c7307bca240beea7d24ae 100644 (file)
@@ -1635,54 +1635,6 @@ bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
    else if(Dep->Type == pkgCache::Dep::Suggests)
      return _config->FindB("APT::Install-Suggests", false);
 
-   return false;
-}
-                                                                       /*}}}*/
-pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()                  /*{{{*/
-  : constructedSuccessfully(false)
-{
-  Configuration::Item const *Opts;
-  Opts = _config->Tree("APT::NeverAutoRemove");
-  if (Opts != 0 && Opts->Child != 0)
-    {
-      Opts = Opts->Child;
-      for (; Opts != 0; Opts = Opts->Next)
-       {
-         if (Opts->Value.empty() == true)
-           continue;
-
-         regex_t *p = new regex_t;
-         if(regcomp(p,Opts->Value.c_str(),
-                    REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
-           {
-             regfree(p);
-             delete p;
-             _error->Error("Regex compilation error for APT::NeverAutoRemove");
-             return;
-           }
-
-         rootSetRegexp.push_back(p);
-       }
-    }
-
-  constructedSuccessfully = true;
-}
-                                                                       /*}}}*/
-pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()                 /*{{{*/
-{
-  for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
-    {
-      regfree(rootSetRegexp[i]);
-      delete rootSetRegexp[i];
-    }
-}
-                                                                       /*}}}*/
-bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg) /*{{{*/
-{
-   for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
-      if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
-        return true;
-
    return false;
 }
                                                                        /*}}}*/
index 72d7cce5d5c68c0dba16d380db8b692d83c64cf3..66c099b8073a3afe29566eeaa0f7857807b8fb52 100644 (file)
 #ifndef PKGLIB_DEPCACHE_H
 #define PKGLIB_DEPCACHE_H
 
-
+#include <apt-pkg/configuration.h>
 #include <apt-pkg/pkgcache.h>
 #include <apt-pkg/progress.h>
-
-#include <regex.h>
+#include <apt-pkg/error.h>
 
 #include <vector>
 #include <memory>
@@ -184,22 +183,13 @@ class pkgDepCache : protected pkgCache::Namespace
    /** \brief Returns \b true for packages matching a regular
     *  expression in APT::NeverAutoRemove.
     */
-   class DefaultRootSetFunc : public InRootSetFunc
+   class DefaultRootSetFunc : public InRootSetFunc, public Configuration::MatchAgainstConfig
    {
-     std::vector<regex_t *> rootSetRegexp;
-     bool constructedSuccessfully;
-
    public:
-     DefaultRootSetFunc();
-     ~DefaultRootSetFunc();
-
-     /** \return \b true if the class initialized successfully, \b
-      *  false otherwise.  Used to avoid throwing an exception, since
-      *  APT classes generally don't.
-      */
-     bool wasConstructedSuccessfully() const { return constructedSuccessfully; }
+     DefaultRootSetFunc() : Configuration::MatchAgainstConfig("APT::NeverRemove") {};
+     virtual ~DefaultRootSetFunc() {};
 
-     bool InRootSet(const pkgCache::PkgIterator &pkg);
+     bool InRootSet(const pkgCache::PkgIterator &pkg) { return pkg.end() == true && Match(pkg.Name()); };
    };
 
    struct StateCache
index 06bde48fcf4c0dca4d3fff31ab555e59288cde2f..4500614a0bf1457faa6ab3b3da9c6c7e3376d6b9 100644 (file)
@@ -16,14 +16,17 @@ apt (0.7.26~exp8) UNRELEASED; urgency=low
   * apt-pkg/depcache.cc:
     - SetCandidateVer for all pseudo packages
     - SetReInstall for the "all" package of a pseudo package
+    - use the new MatchAgainstConfig for the DefaultRootSetFunc
   * apt-pkg/contrib/error.{cc,h}:
     - complete rewrite but use the same API
     - add NOTICE and DEBUG as new types of a message
     - add a simple stack handling to be able to delay error handling
   * apt-pkg/aptconfiguration.cc:
     - show a deprecation notice for APT::Acquire::Translation
+  * apt-pkg/contrib/configuration.{cc,h}:
+    - add a wrapper to match strings against configurable regex patterns
 
- -- David Kalnischkies <kalnischkies@gmail.com>  Sat, 26 Jun 2010 09:01:40 +0200
+ -- David Kalnischkies <kalnischkies@gmail.com>  Sat, 26 Jun 2010 13:28:48 +0200
 
 apt (0.7.26~exp7) experimental; urgency=low