Better error.
[libcxml.git] / src / cxml.h
index 9581adce01e8ea6b0f922b4df00d0656217997bc..75af36b401a719e34a5fd4c3c80cb77b4902c15e 100644 (file)
@@ -1,3 +1,22 @@
+/*
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+
+    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 LIBCXML_CXML_H
 #define LIBCXML_CXML_H
 
@@ -6,8 +25,14 @@
 #include <stdint.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/optional.hpp>
-#include <boost/lexical_cast.hpp>
+#include <boost/filesystem.hpp>
 #include <boost/algorithm/string/erase.hpp>
+
+/* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
+#ifdef check
+#undef check
+#endif
+
 #include <glibmm.h>
 
 namespace xmlpp {
@@ -51,7 +76,7 @@ public:
         *  not destroy the xmlpp::Node.
         *  @param node xmlpp::Node.
         */
-       Node (xmlpp::Node const * node);
+       Node (xmlpp::Node* node);
 
        std::string name () const;
 
@@ -88,7 +113,12 @@ public:
        {
                std::string s = string_child (c);
                boost::erase_all (s, " ");
-               return boost::lexical_cast<T> (s);
+               std::stringstream t;
+               t.imbue (std::locale::classic ());
+               t << s;
+               T n;
+               t >> n;
+               return n;
        }
 
        template <class T>
@@ -101,7 +131,12 @@ public:
 
                std::string t = s.get ();
                boost::erase_all (t, " ");
-               return boost::optional<T> (boost::lexical_cast<T> (t));
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << t;
+               T n;
+               u >> n;
+               return n;
        }
                
        /** This will mark a child as to be ignored when calling done() */
@@ -127,7 +162,12 @@ public:
        {
                std::string s = string_attribute (c);
                boost::erase_all (s, " ");
-               return boost::lexical_cast<T> (s);
+               std::stringstream t;
+               t.imbue (std::locale::classic ());
+               t << s;
+               T n;
+               t >> n;
+               return n;
        }
 
        template <class T>
@@ -140,32 +180,64 @@ public:
 
                std::string t = s.get ();
                boost::erase_all (t, " ");
-               return boost::optional<T> (boost::lexical_cast<T> (t));
+               std::stringstream u;
+               u.imbue (std::locale::classic ());
+               u << t;
+               T n;
+               u >> n;
+               return n;
        }
 
        /** @return The content of this node */
        std::string content () const;
 
+       /** @return namespace URI of this node */
+       std::string namespace_uri () const;
+
+       /** @return namespace prefix of this node */
+       std::string namespace_prefix () const;
+
        boost::shared_ptr<Node> node_child (std::string) const;
        boost::shared_ptr<Node> optional_node_child (std::string) const;
 
        std::list<boost::shared_ptr<Node> > node_children (std::string) const;
+
+       xmlpp::Node* node () const {
+               return _node;
+       }
        
 protected:
-       xmlpp::Node const * _node;
+       xmlpp::Node* _node;
        
 private:
        mutable std::list<Glib::ustring> _taken;
 };
 
-class File : public Node
+typedef boost::shared_ptr<cxml::Node> NodePtr;
+typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
+
+class Document : public Node
 {
 public:
-       File (std::string file, std::string root_name);
-       virtual ~File ();
+       Document ();
+       Document (std::string root_name);
+       Document (std::string root_name, boost::filesystem::path);
 
+       virtual ~Document ();
+
+       void read_file (boost::filesystem::path);
+       void read_stream (std::istream &);
+       void read_string (std::string);
+       
+       std::string root_name () const {
+               return _root_name;
+       }
+              
 private:
+       void take_root_node ();
+       
        xmlpp::DomParser* _parser;
+       std::string _root_name;
 };
 
 }