NO-OP: whitespace
[ardour.git] / libs / pbd / pbd / xml++.h
index 65ab0f2c03ac4d2eb105f893482678df9d70c8a8..4b012c910e6d5b8fdc064cc5b899f46aee0531b4 100644 (file)
@@ -1,3 +1,25 @@
+/*
+    Copyright (C) 2012 Paul Davis
+
+    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 2 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, write to the Free Software
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#ifndef __XML_H
+#define __XML_H
+
 /* xml++.h
  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
  * are covered by the GNU Lesser General Public License, which should be
@@ -6,8 +28,7 @@
  */
 
 #include <string>
-#include <list>
-#include <map>
+#include <vector>
 #include <cstdio>
 #include <cstdarg>
 
 #include <libxml/tree.h>
 #include <boost/shared_ptr.hpp>
 
-#ifndef __XML_H
-#define __XML_H
+#include <glibmm/ustring.h>
+
+#include "pbd/string_convert.h"
+#include "pbd/libpbd_visibility.h"
 
 class XMLTree;
 class XMLNode;
-class XMLProperty;
-
-typedef std::list<XMLNode *>                   XMLNodeList;
-typedef std::list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
-typedef XMLNodeList::iterator                  XMLNodeIterator;
-typedef XMLNodeList::const_iterator            XMLNodeConstIterator;
-typedef std::list<XMLProperty*>                XMLPropertyList;
-typedef XMLPropertyList::iterator              XMLPropertyIterator;
-typedef XMLPropertyList::const_iterator        XMLPropertyConstIterator;
-typedef std::map<std::string, XMLProperty*>    XMLPropertyMap;
-
-class XMLTree {
+
+class LIBPBD_API XMLProperty {
+public:
+       XMLProperty(const std::string& n, const std::string& v = std::string());
+       ~XMLProperty();
+
+       const std::string& name() const { return _name; }
+       const std::string& value() const { return _value; }
+       const std::string& set_value(const std::string& v) { return _value = v; }
+
+private:
+       std::string _name;
+       std::string _value;
+};
+
+typedef std::vector<XMLNode *>                   XMLNodeList;
+typedef std::vector<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
+typedef XMLNodeList::iterator                    XMLNodeIterator;
+typedef XMLNodeList::const_iterator              XMLNodeConstIterator;
+typedef std::vector<XMLProperty*>                XMLPropertyList;
+typedef XMLPropertyList::iterator                XMLPropertyIterator;
+typedef XMLPropertyList::const_iterator          XMLPropertyConstIterator;
+
+class LIBPBD_API XMLTree {
 public:
        XMLTree();
        XMLTree(const std::string& fn, bool validate = false);
@@ -51,7 +86,7 @@ public:
        bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
        bool read_and_validate() { return read_internal(true); }
        bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
-       bool read_buffer(const std::string&);
+       bool read_buffer(const std::string&, bool to_tree_doc = false);
 
        bool write() const;
        bool write(const std::string& fn) { set_filename(fn); return write(); }
@@ -60,21 +95,29 @@ public:
 
        const std::string& write_buffer() const;
 
+       boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
+
 private:
        bool read_internal(bool validate);
-       
+
        std::string _filename;
        XMLNode*    _root;
+       xmlDocPtr   _doc;
        int         _compression;
 };
 
-class XMLNode {
+class LIBPBD_API XMLNode {
 public:
        XMLNode(const std::string& name);
        XMLNode(const std::string& name, const std::string& content);
        XMLNode(const XMLNode& other);
        ~XMLNode();
 
+       XMLNode& operator= (const XMLNode& other);
+
+       bool operator== (const XMLNode& other) const;
+       bool operator!= (const XMLNode& other) const;
+
        const std::string& name() const { return _name; }
 
        bool          is_content() const { return _is_content; }
@@ -88,27 +131,63 @@ public:
        XMLNode* add_child_copy(const XMLNode&);
        void     add_child_nocopy(XMLNode&);
 
-       boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
        std::string attribute_value();
 
        const XMLPropertyList& properties() const { return _proplist; }
-       XMLProperty*       property(const char*);
-       XMLProperty*       property(const std::string&);
-       const XMLProperty* property(const char* n)   const { return ((XMLNode*)this)->property(n); }
-       const XMLProperty* property(const std::string& n) const { return ((XMLNode*)this)->property(n); }
-       
-       XMLProperty* add_property(const char* name, const std::string& value);
-       XMLProperty* add_property(const char* name, const char* value = "");
-       XMLProperty* add_property(const char* name, const long value);
+       XMLProperty const *    property(const char*) const;
+       XMLProperty const *    property(const std::string&) const;
+       XMLProperty *    property(const char*);
+       XMLProperty *    property(const std::string&);
+
+       bool has_property_with_value (const std::string&, const std::string&) const;
+
+       bool set_property (const char* name, const std::string& value);
+
+       bool set_property (const char* name, const char* cstr) {
+               return set_property (name, std::string(cstr));
+       }
+
+       bool set_property (const char* name, const Glib::ustring& ustr)
+       {
+               return set_property (name, ustr.raw ());
+       }
+
+       template<class T>
+       bool set_property (const char* name, const T& value)
+       {
+               std::string str;
+               if (!PBD::to_string<T> (value, str)) {
+                       return false;
+               }
+               return set_property(name, str);
+       }
+
+       bool get_property (const char* name, std::string& value) const;
+
+       template <class T>
+       bool get_property (const char* name, T& value) const
+       {
+               XMLProperty const* const prop = property (name);
+               if (!prop) {
+                       return false;
+               }
+
+               return PBD::string_to<T> (prop->value (), value);
+       }
 
        void remove_property(const std::string&);
+       void remove_property_recursively(const std::string&);
 
        /** Remove all nodes with the name passed to remove_nodes */
-       void remove_nodes(const std::string&);
+       void remove_nodes (const std::string&);
        /** Remove and delete all nodes with the name passed to remove_nodes */
-       void remove_nodes_and_delete(const std::string&);
+       void remove_nodes_and_delete (const std::string&);
        /** Remove and delete all nodes with property prop matching val */
-       void remove_nodes_and_delete(const std::string& propname, const std::string& val);
+       void remove_nodes_and_delete (const std::string& propname, const std::string& val);
+       /** Remove and delete first node with given name and prop matching val */
+       void remove_node_and_delete (const std::string& n, const std::string& propname, const std::string& val);
+
+       void dump (std::ostream &, std::string p = "") const;
 
 private:
        std::string         _name;
@@ -116,25 +195,12 @@ private:
        std::string         _content;
        XMLNodeList         _children;
        XMLPropertyList     _proplist;
-       XMLPropertyMap      _propmap;
        mutable XMLNodeList _selected_children;
-};
 
-class XMLProperty {
-public:
-       XMLProperty(const std::string& n, const std::string& v = std::string());
-       ~XMLProperty();
-
-       const std::string& name() const { return _name; }
-       const std::string& value() const { return _value; }
-       const std::string& set_value(const std::string& v) { return _value = v; }
-
-private:
-       std::string _name;
-       std::string _value;
+       void clear_lists ();
 };
 
-class XMLException: public std::exception {
+class LIBPBD_API XMLException: public std::exception {
 public:
        explicit XMLException(const std::string msg) : _message(msg) {}
        virtual ~XMLException() throw() {}