myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3068] The main configuration file can store structure


From: Giuseppe Scrivano
Subject: [myserver-commit] [3068] The main configuration file can store structured data using a forest of trees .
Date: Sun, 03 May 2009 10:55:49 +0000

Revision: 3068
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3068
Author:   gscrivano
Date:     2009-05-03 10:55:49 +0000 (Sun, 03 May 2009)
Log Message:
-----------
The main configuration file can store structured data using a forest of trees.

Modified Paths:
--------------
    trunk/myserver/include/conf/Makefile.am
    trunk/myserver/include/server/server.h
    trunk/myserver/src/conf/Makefile.am
    trunk/myserver/src/server/server.cpp

Added Paths:
-----------
    trunk/myserver/include/conf/nodetree.h
    trunk/myserver/include/conf/xml_conf.h
    trunk/myserver/src/conf/nodetree.cpp
    trunk/myserver/src/conf/xml_conf.cpp

Modified: trunk/myserver/include/conf/Makefile.am
===================================================================
--- trunk/myserver/include/conf/Makefile.am     2009-05-02 18:09:16 UTC (rev 
3067)
+++ trunk/myserver/include/conf/Makefile.am     2009-05-03 10:55:49 UTC (rev 
3068)
@@ -1,4 +1,3 @@
 confincludedir=$(includedir)/myserver/include/conf
-confinclude_HEADERS =
+confinclude_HEADERS = nodetree.h xml_conf.h
 SUBDIRS = mime security vhost
-

Added: trunk/myserver/include/conf/nodetree.h
===================================================================
--- trunk/myserver/include/conf/nodetree.h                              (rev 0)
+++ trunk/myserver/include/conf/nodetree.h      2009-05-03 10:55:49 UTC (rev 
3068)
@@ -0,0 +1,24 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef NODE_TREE_H
+#define NODE_TREE_H
+
+#include <src/conf/nodetree.cpp>
+
+#endif

Added: trunk/myserver/include/conf/xml_conf.h
===================================================================
--- trunk/myserver/include/conf/xml_conf.h                              (rev 0)
+++ trunk/myserver/include/conf/xml_conf.h      2009-05-03 10:55:49 UTC (rev 
3068)
@@ -0,0 +1,47 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef XML_CONF_H
+#define XML_CONF_H
+
+#include <include/conf/nodetree.h>
+#include <include/base/xml/xml_parser.h>
+#include <include/base/hash_map/hash_map.h>
+
+#include <list>
+
+using namespace std;
+
+/*!
+ *Utility class for various operations on XML files.
+ */
+class XmlConf
+{
+ public:
+  static int build (xmlNodePtr root,
+                    list<NodeTree<string>*> *rootNodes,
+                    HashMap<string, NodeTree<string>*> *hashedData);
+
+ protected:
+  static void readNode (xmlNodePtr lcur,
+                        NodeTree<string> *root,
+                        list<NodeTree<string>*> *rootNodes,
+                        HashMap<string, NodeTree<string>*> *hashedData);
+};
+
+#endif

Modified: trunk/myserver/include/server/server.h
===================================================================
--- trunk/myserver/include/server/server.h      2009-05-02 18:09:16 UTC (rev 
3067)
+++ trunk/myserver/include/server/server.h      2009-05-03 10:55:49 UTC (rev 
3068)
@@ -42,6 +42,7 @@
 #include <include/base/multicast/multicast.h>
 #include <include/connections_scheduler/connections_scheduler.h>
 
+#include <include/conf/nodetree.h>
 
 #include <include/conf/security/security_manager.h>
 #include <include/conf/security/auth_method_factory.h>
@@ -192,7 +193,10 @@
   VhostManager *vhostList;
   MimeManager *mimeManager;
   HomeDir homeDir;
-  HashMap<string, string*> hashedData;
+
+  list<NodeTree<string>*> hashedDataTrees;
+  HashMap<string, NodeTree<string>*> hashedData;
+
   HashMap<string, void*> globalData;
   FiltersFactory filtersFactory;
   u_long uid;

Modified: trunk/myserver/src/conf/Makefile.am
===================================================================
--- trunk/myserver/src/conf/Makefile.am 2009-05-02 18:09:16 UTC (rev 3067)
+++ trunk/myserver/src/conf/Makefile.am 2009-05-03 10:55:49 UTC (rev 3068)
@@ -1,5 +1,4 @@
 lib_LIBRARIES = libconf.a
-libconf_a_SOURCES =
+libconf_a_SOURCES = nodetree.cpp xml_conf.cpp
 SUBDIRS = mime security vhost
 INCLUDES = $(all_includes)
-

Added: trunk/myserver/src/conf/nodetree.cpp
===================================================================
--- trunk/myserver/src/conf/nodetree.cpp                                (rev 0)
+++ trunk/myserver/src/conf/nodetree.cpp        2009-05-03 10:55:49 UTC (rev 
3068)
@@ -0,0 +1,90 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <list>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+
+using namespace std;
+
+template <typename T>
+class NodeTree
+{
+public:
+  NodeTree ()
+  {
+    value = NULL;
+    children = NULL;
+  }
+
+  NodeTree (T& v)
+  {
+    value = new T (v);
+    children = NULL;
+  }
+
+  ~NodeTree ()
+  {
+    if (children)
+      {
+        typename list<NodeTree<T>* >::iterator it = children->begin ();
+        while (it != children->end ())
+          {
+            delete *it;
+            it++;
+          }
+        delete children;
+      }
+
+    if (value)
+      {
+        delete value;
+      }
+  }
+
+  void setValue (T *v)
+  {
+    value = v;
+  }
+
+  void addChild (NodeTree<T> *child)
+  {
+    if (children == NULL)
+      children = new list<NodeTree<T>*>;
+
+    children->push_back (child);
+  }
+
+  bool isLeaf ()
+  {
+    return children == NULL;
+  }
+
+  list<NodeTree<T>*> *getChildren ()
+  {
+    return children;
+  }
+
+  const T *getValue ()
+  {
+    return value;
+  }
+protected:
+  list<NodeTree<T>*> *children;
+  T *value;
+};

Added: trunk/myserver/src/conf/xml_conf.cpp
===================================================================
--- trunk/myserver/src/conf/xml_conf.cpp                                (rev 0)
+++ trunk/myserver/src/conf/xml_conf.cpp        2009-05-03 10:55:49 UTC (rev 
3068)
@@ -0,0 +1,87 @@
+/*
+MyServer
+Copyright (C) 2009 Free Software Foundation, Inc.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <list>
+#include <stdlib.h>
+#include <string.h>
+#include <string>
+
+#include <include/conf/xml_conf.h>
+
+
+/*!
+ *Read a forest of trees (structured objects) from XML.
+ *
+ *\param root The XML document root.
+ *\param rootNodes All the stored top-level objects.
+ *\param hashedData Access node by a name.
+ *\return 0 on success.
+ */
+int XmlConf::build (xmlNodePtr root,
+                    list<NodeTree<string>*> *rootNodes,
+                    HashMap<string, NodeTree<string>*> *hashedData)
+{
+  readNode (root, NULL, rootNodes, hashedData);
+  return 0;
+}
+
+void XmlConf::readNode (xmlNodePtr lcur,
+                        NodeTree<string>* root,
+                        list<NodeTree<string>*> *rootNodes,
+                        HashMap<string, NodeTree<string>*> *hashedData)
+{
+  xmlAttr *attrs;
+  for(; lcur; lcur = lcur->next)
+    if (lcur->name && !xmlStrcmp (lcur->name, (const xmlChar *)"DEFINE"))
+      {
+        const char *name = NULL;
+        const char *value = NULL;
+
+        for (attrs = lcur->properties; attrs; attrs = attrs->next)
+          {
+            if (!xmlStrcmp (attrs->name, (const xmlChar *)"name") && 
+                attrs->children && attrs->children->content)
+              name = (const char*)attrs->children->content;
+            
+            if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") && 
+                attrs->children && attrs->children->content)
+              value = (const char*)attrs->children->content;
+          }
+
+        string *v = value ? new string((const char*)value) : NULL;
+        NodeTree<string> *node = new NodeTree<string> ();
+        node->setValue (v);
+ 
+        if(name)
+          {
+            string key((const char*)name);
+            hashedData->put(key, node);
+          }
+
+        if (root)
+          root->addChild (node);
+
+        if (rootNodes)
+          rootNodes->push_back (node);
+
+        if (lcur->children)
+          readNode (lcur->children,
+                    node,
+                    NULL,
+                    hashedData);
+      }
+}

Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp        2009-05-02 18:09:16 UTC (rev 
3067)
+++ trunk/myserver/src/server/server.cpp        2009-05-03 10:55:49 UTC (rev 
3068)
@@ -33,6 +33,9 @@
 #include <include/base/ssl/ssl.h>
 #include <include/base/socket/ssl_socket.h>
 
+#include <include/conf/xml_conf.h>
+
+
 extern "C"
 {
 #ifdef WIN32
@@ -1166,38 +1169,9 @@
  */
 void Server::readHashedData (xmlNodePtr lcur)
 {
-  xmlAttr *attrs;
-  for(;lcur; lcur = lcur->next)
-    {
-      
-      if (lcur->name && !xmlStrcmp(lcur->name, (const xmlChar *)"DEFINE"))
-        {
-          const char *name = NULL;
-          const char *value = NULL;
-          
-          for (attrs = lcur->properties; attrs; attrs = attrs->next)
-            {
-              if (!xmlStrcmp (attrs->name, (const xmlChar *)"name") && 
-                  attrs->children && attrs->children->content)
-                name = (const char*)attrs->children->content;
-              
-              if (!xmlStrcmp (attrs->name, (const xmlChar *)"value") && 
-                  attrs->children && attrs->children->content)
-                value = (const char*)attrs->children->content;
-            }
-          
-          if(name && value)
-            {
-              string* old;
-              string *v = new string((const char*)value);
-              string key((const char*)name);
-              old = hashedData.put(key, v);
-
-              if(old)
-                delete old;
-            }
-        }
-    }
+  XmlConf::build (lcur,
+                  &hashedDataTrees,
+                  &hashedData);
 }
 
 
@@ -1610,9 +1584,14 @@
 {
   try
   {
-    HashMap<string, string*>::Iterator it = hashedData.begin();
-    for (;it != hashedData.end(); it++)
-      delete *it;
+    list<NodeTree<string>*>::iterator it = hashedDataTrees.begin ();
+    while (it != hashedDataTrees.end ())
+      {
+        delete *it;
+        it++;
+      }
+
+    hashedDataTrees.clear ();
     hashedData.clear();
   }
   catch(...)
@@ -1627,8 +1606,9 @@
  */
 const char* Server::getHashedData(const char* name)
 {
-  string *s = hashedData.get(name);
-  return s ? s->c_str() : 0;
+  NodeTree<string> *s = hashedData.get(name);
+
+  return s ? s->getValue ()->c_str() : 0;
 }
 
 /*!





reply via email to

[Prev in Thread] Current Thread [Next in Thread]