make BasicUI (for control surfaces) loop toggle work like the GUI
[ardour.git] / libs / pbd / xml++.cc
index 7c6fffe7f80adda07cd478cf11bfb3baa14c4326..6d260f252503e4098e85a5604df9dbe44ffc4e3b 100644 (file)
@@ -5,12 +5,13 @@
  * Modified for Ardour and released under the same terms.
  */
 
+#include <iostream>
 #include "pbd/xml++.h"
 #include <libxml/debugXML.h>
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
 
-#define XML_VERSION "1.0"
+xmlChar* xml_version = xmlCharStrdup("1.0");
 
 using namespace std;
 
@@ -41,7 +42,7 @@ XMLTree::XMLTree(const XMLTree* from)
        , _doc (xmlCopyDoc (from->_doc, 1))
        , _compression(from->compression())
 {
-       
+
 }
 
 XMLTree::~XMLTree()
@@ -81,26 +82,23 @@ XMLTree::read_internal(bool validate)
                _doc = 0;
        }
 
-       xmlParserCtxtPtr ctxt = NULL; /* the parser context */
+       /* create a parser context */
+       xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
+       if (ctxt == NULL) {
+               return false;
+       }
 
        xmlKeepBlanksDefault(0);
        /* parse the file, activating the DTD validation option */
        if (validate) {
-               /* create a parser context */
-               ctxt = xmlNewParserCtxt();
-               if (ctxt == NULL) {
-                       return false;
-               }
                _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_DTDVALID);
        } else {
-               _doc = xmlParseFile(_filename.c_str());
+               _doc = xmlCtxtReadFile(ctxt, _filename.c_str(), NULL, XML_PARSE_HUGE);
        }
-       
+
        /* check if parsing suceeded */
        if (_doc == NULL) {
-               if (validate) {
-                       xmlFreeParserCtxt(ctxt);
-               }
+               xmlFreeParserCtxt(ctxt);
                return false;
        } else {
                /* check if validation suceeded */
@@ -113,10 +111,8 @@ XMLTree::read_internal(bool validate)
        _root = readnode(xmlDocGetRootElement(_doc));
 
        /* free up the parser context */
-       if (validate) {
-               xmlFreeParserCtxt(ctxt);
-       }
-       
+       xmlFreeParserCtxt(ctxt);
+
        return true;
 }
 
@@ -130,7 +126,7 @@ XMLTree::read_buffer(const string& buffer)
        delete _root;
        _root = 0;
 
-       doc = xmlParseMemory((char*)buffer.c_str(), buffer.length());
+       doc = xmlParseMemory(const_cast<char*>(buffer.c_str()), buffer.length());
        if (!doc) {
                return false;
        }
@@ -150,10 +146,24 @@ XMLTree::write() const
        int result;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        result = xmlSaveFormatFileEnc(_filename.c_str(), doc, "UTF-8", 1);
+#ifndef NDEBUG
+       if (result == -1) {
+               xmlErrorPtr xerr = xmlGetLastError ();
+               if (!xerr) {
+                       std::cerr << "unknown XML error during xmlSaveFormatFileEnc()." << std::endl;
+               } else {
+                       std::cerr << "xmlSaveFormatFileEnc: error"
+                               << " domain: " << xerr->domain
+                               << " code: " << xerr->code
+                               << " msg: " << xerr->message
+                               << std::endl;
+               }
+       }
+#endif
        xmlFreeDoc(doc);
 
        if (result == -1) {
@@ -166,15 +176,17 @@ XMLTree::write() const
 void
 XMLTree::debug(FILE* out) const
 {
+#ifdef LIBXML_DEBUG_ENABLED
        xmlDocPtr doc;
        XMLNodeList children;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        xmlDebugDumpDocument (out, doc);
        xmlFreeDoc(doc);
+#endif
 }
 
 const string&
@@ -187,7 +199,7 @@ XMLTree::write_buffer() const
        XMLNodeList children;
 
        xmlKeepBlanksDefault(0);
-       doc = xmlNewDoc((xmlChar*) XML_VERSION);
+       doc = xmlNewDoc(xml_version);
        xmlSetDocCompressMode(doc, _compression);
        writenode(doc, _root, doc->children, 1);
        xmlDocDumpMemory(doc, (xmlChar **) & ptr, &len);
@@ -245,7 +257,7 @@ XMLNode::clear_lists ()
        _proplist.clear ();
 }
 
-XMLNode& 
+XMLNode&
 XMLNode::operator= (const XMLNode& from)
 {
        if (&from != this) {
@@ -254,17 +266,17 @@ XMLNode::operator= (const XMLNode& from)
                XMLPropertyIterator curprop;
                XMLNodeList nodes;
                XMLNodeIterator curnode;
-               
+
                clear_lists ();
 
                _name = from.name();
                set_content(from.content());
-               
+
                props = from.properties();
                for (curprop = props.begin(); curprop != props.end(); ++curprop) {
                        add_property((*curprop)->name().c_str(), (*curprop)->value());
                }
-               
+
                nodes = from.children();
                for (curnode = nodes.begin(); curnode != nodes.end(); ++curnode) {
                        add_child_copy(**curnode);
@@ -357,16 +369,16 @@ XMLTree::find(const string xpath, XMLNode* node) const
        xmlDocPtr doc = 0;
 
        if (node) {
-               doc = xmlNewDoc((xmlChar*) XML_VERSION);
+               doc = xmlNewDoc(xml_version);
                writenode(doc, node, doc->children, 1);
                ctxt = xmlXPathNewContext(doc);
        } else {
                ctxt = xmlXPathNewContext(_doc);
        }
-       
+
        boost::shared_ptr<XMLSharedNodeList> result =
                boost::shared_ptr<XMLSharedNodeList>(find_impl(ctxt, xpath));
-       
+
        xmlXPathFreeContext(ctxt);
        if (doc) {
                xmlFreeDoc (doc);
@@ -422,7 +434,7 @@ XMLNode::add_property(const char* n, const string& v)
 {
        string ns(n);
         map<string,XMLProperty*>::iterator iter;
-       
+
         if ((iter = _propmap.find(ns)) != _propmap.end()) {
                 iter->second->set_value (v);
                 return iter->second;
@@ -460,7 +472,10 @@ XMLNode::remove_property(const string& n)
 {
        if (_propmap.find(n) != _propmap.end()) {
                XMLProperty* p = _propmap[n];
-               _proplist.remove (p);
+               XMLPropertyIterator i = std::find(_proplist.begin(), _proplist.end(), p);
+               if (i != _proplist.end ()) {
+                       _proplist.erase (i);
+               }
                delete p;
                _propmap.erase(n);
        }
@@ -480,15 +495,12 @@ void
 XMLNode::remove_nodes(const string& n)
 {
        XMLNodeIterator i = _children.begin();
-       XMLNodeIterator tmp;
-
        while (i != _children.end()) {
-               tmp = i;
-               ++tmp;
                if ((*i)->name() == n) {
-                       _children.erase (i);
+                       i = _children.erase (i);
+               } else {
+                       ++i;
                }
-               i = tmp;
        }
 }
 
@@ -496,16 +508,14 @@ void
 XMLNode::remove_nodes_and_delete(const string& n)
 {
        XMLNodeIterator i = _children.begin();
-       XMLNodeIterator tmp;
 
        while (i != _children.end()) {
-               tmp = i;
-               ++tmp;
                if ((*i)->name() == n) {
                        delete *i;
-                       _children.erase (i);
+                       i = _children.erase (i);
+               } else {
+                       ++i;
                }
-               i = tmp;
        }
 }
 
@@ -513,20 +523,16 @@ void
 XMLNode::remove_nodes_and_delete(const string& propname, const string& val)
 {
        XMLNodeIterator i = _children.begin();
-       XMLNodeIterator tmp;
        XMLProperty* prop;
 
        while (i != _children.end()) {
-               tmp = i;
-               ++tmp;
-
                prop = (*i)->property(propname);
                if (prop && prop->value() == val) {
                        delete *i;
-                       _children.erase(i);
+                       i = _children.erase(i);
+               } else {
+                       ++i;
                }
-
-               i = tmp;
        }
 }
 
@@ -555,7 +561,7 @@ readnode(xmlNodePtr node)
        xmlAttrPtr attr;
 
        if (node->name) {
-               name = (char*)node->name;
+               name = (const char*)node->name;
        }
 
        tmp = new XMLNode(name);
@@ -565,7 +571,7 @@ readnode(xmlNodePtr node)
                if (attr->children) {
                        content = (char*)attr->children->content;
                }
-               tmp->add_property((char*)attr->name, content);
+               tmp->add_property((const char*)attr->name, content);
        }
 
        if (node->content) {
@@ -591,9 +597,9 @@ writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
        xmlNodePtr node;
 
        if (root) {
-               node = doc->children = xmlNewDocNode(doc, 0, (xmlChar*) n->name().c_str(), 0);
+               node = doc->children = xmlNewDocNode(doc, 0, (const xmlChar*) n->name().c_str(), 0);
        } else {
-               node = xmlNewChild(p, 0, (xmlChar*) n->name().c_str(), 0);
+               node = xmlNewChild(p, 0, (const xmlChar*) n->name().c_str(), 0);
        }
 
        if (n->is_content()) {
@@ -603,7 +609,7 @@ writenode(xmlDocPtr doc, XMLNode* n, xmlNodePtr p, int root = 0)
 
        props = n->properties();
        for (curprop = props.begin(); curprop != props.end(); ++curprop) {
-               xmlSetProp(node, (xmlChar*) (*curprop)->name().c_str(), (xmlChar*) (*curprop)->value().c_str());
+               xmlSetProp(node, (const xmlChar*) (*curprop)->name().c_str(), (const xmlChar*) (*curprop)->value().c_str());
        }
 
        children = n->children();
@@ -659,11 +665,11 @@ XMLNode::dump (ostream& s, string p) const
                        s << " " << (*i)->name() << "=\"" << (*i)->value() << "\"";
                }
                s << ">\n";
-               
+
                for (XMLNodeList::const_iterator i = _children.begin(); i != _children.end(); ++i) {
                        (*i)->dump (s, p + "  ");
                }
-               
+
                s << p << "</" << _name << ">\n";
        }
 }