]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/configuration.cc
fileutl: simple_buffer: Add write() and full() methods
[apt.git] / apt-pkg / contrib / configuration.cc
index 4de17e3e1933ee8966403c8ff1ac65f245f91814..d4bb72a8b61ea606e161163308f008e2941b588b 100644 (file)
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
+#include <apt-pkg/macros.h>
 
 
+#include <ctype.h>
+#include <regex.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <algorithm>
+#include <string>
 #include <vector>
 #include <fstream>
 #include <vector>
 #include <fstream>
-#include <iostream>
 
 #include <apti18n.h>
 
 
 #include <apti18n.h>
 
@@ -42,8 +50,7 @@ Configuration::Configuration() : ToFree(true)
 }
 Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
 {
 }
 Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
 {
-};
-
+}
                                                                        /*}}}*/
 // Configuration::~Configuration - Destructor                          /*{{{*/
 // ---------------------------------------------------------------------
                                                                        /*}}}*/
 // Configuration::~Configuration - Destructor                          /*{{{*/
 // ---------------------------------------------------------------------
@@ -246,19 +253,25 @@ string Configuration::FindDir(const char *Name,const char *Default) const
 // Configuration::FindVector - Find a vector of values                 /*{{{*/
 // ---------------------------------------------------------------------
 /* Returns a vector of config values under the given item */
 // Configuration::FindVector - Find a vector of values                 /*{{{*/
 // ---------------------------------------------------------------------
 /* Returns a vector of config values under the given item */
-vector<string> Configuration::FindVector(const char *Name) const
+vector<string> Configuration::FindVector(const char *Name, std::string const &Default, bool const Keys) const
 {
    vector<string> Vec;
    const Item *Top = Lookup(Name);
    if (Top == NULL)
 {
    vector<string> Vec;
    const Item *Top = Lookup(Name);
    if (Top == NULL)
-      return Vec;
+      return VectorizeString(Default, ',');
+
+   if (Top->Value.empty() == false)
+      return VectorizeString(Top->Value, ',');
 
    Item *I = Top->Child;
    while(I != NULL)
    {
 
    Item *I = Top->Child;
    while(I != NULL)
    {
-      Vec.push_back(I->Value);
+      Vec.push_back(Keys ? I->Tag : I->Value);
       I = I->Next;
    }
       I = I->Next;
    }
+   if (Vec.empty() == true)
+      return VectorizeString(Default, ',');
+
    return Vec;
 }
                                                                        /*}}}*/
    return Vec;
 }
                                                                        /*}}}*/
@@ -420,6 +433,18 @@ void Configuration::Clear(string const &Name, string const &Value)
       }
    }
      
       }
    }
      
+}
+                                                                       /*}}}*/
+// Configuration::Clear - Clear everything                             /*{{{*/
+// ---------------------------------------------------------------------
+void Configuration::Clear()
+{
+   const Configuration::Item *Top = Tree(0);
+   while( Top != 0 )
+   {
+      Clear(Top->FullTag());
+      Top = Top->Next;
+   }
 }
                                                                        /*}}}*/
 // Configuration::Clear - Clear an entire tree                         /*{{{*/
 }
                                                                        /*}}}*/
 // Configuration::Clear - Clear an entire tree                         /*{{{*/
@@ -460,6 +485,59 @@ void Configuration::Clear(string const &Name)
    }
 }
                                                                        /*}}}*/
    }
 }
                                                                        /*}}}*/
+void Configuration::MoveSubTree(char const * const OldRootName, char const * const NewRootName)/*{{{*/
+{
+   // prevent NewRoot being a subtree of OldRoot
+   if (OldRootName == nullptr)
+      return;
+   if (NewRootName != nullptr)
+   {
+      if (strcmp(OldRootName, NewRootName) == 0)
+        return;
+      std::string const oldroot = std::string(OldRootName) + "::";
+      if (strcasestr(NewRootName, oldroot.c_str()) != NULL)
+        return;
+   }
+
+   Item * Top;
+   Item const * const OldRoot = Top = Lookup(OldRootName, false);
+   if (Top == nullptr)
+      return;
+   std::string NewRoot;
+   if (NewRootName != nullptr)
+      NewRoot.append(NewRootName).append("::");
+
+   Top->Value.clear();
+   Item * const Stop = Top;
+   Top = Top->Child;
+   Stop->Child = 0;
+   for (; Top != 0;)
+   {
+      if (Top->Child != 0)
+      {
+        Top = Top->Child;
+        continue;
+      }
+
+      while (Top != 0 && Top->Next == 0)
+      {
+        Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
+        Item const * const Tmp = Top;
+        Top = Top->Parent;
+        delete Tmp;
+
+        if (Top == Stop)
+           return;
+      }
+
+      Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
+      Item const * const Tmp = Top;
+      if (Top != 0)
+        Top = Top->Next;
+      delete Tmp;
+   }
+}
+                                                                       /*}}}*/
 // Configuration::Exists - Returns true if the Name exists             /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // Configuration::Exists - Returns true if the Name exists             /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -598,19 +676,19 @@ string Configuration::Item::FullTag(const Item *Stop) const
    tag/value. AsSectional enables Sectional parsing.*/
 bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
                    unsigned const &Depth)
    tag/value. AsSectional enables Sectional parsing.*/
 bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
                    unsigned const &Depth)
-{   
+{
    // Open the stream for reading
    // Open the stream for reading
-   ifstream F(FName.c_str(),ios::in); 
-   if (!F != 0)
+   ifstream F(FName.c_str(),ios::in);
+   if (F.fail() == true)
       return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
 
    string LineBuffer;
    string Stack[100];
    unsigned int StackPos = 0;
       return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
 
    string LineBuffer;
    string Stack[100];
    unsigned int StackPos = 0;
-   
+
    // Parser state
    string ParentTag;
    // Parser state
    string ParentTag;
-   
+
    int CurLine = 0;
    bool InComment = false;
    while (F.eof() == false)
    int CurLine = 0;
    bool InComment = false;
    while (F.eof() == false)
@@ -811,7 +889,7 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio
            // Go down a level
            if (TermChar == '{')
            {
            // Go down a level
            if (TermChar == '{')
            {
-              if (StackPos <= 100)
+              if (StackPos < sizeof(Stack)/sizeof(std::string))
                  Stack[StackPos++] = ParentTag;
               
               /* Make sectional tags incorperate the section into the
                  Stack[StackPos++] = ParentTag;
               
               /* Make sectional tags incorperate the section into the
@@ -958,7 +1036,7 @@ Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
         continue;
       }
    }
         continue;
       }
    }
-   if (strings.size() == 0)
+   if (strings.empty() == true)
       patterns.push_back(NULL);
 }
                                                                        /*}}}*/
       patterns.push_back(NULL);
 }
                                                                        /*}}}*/