Windows build fixes.
[libdcp.git] / src / xml.h
1 #ifndef LIBDCP_XML_H
2 #define LIBDCP_XML_H
3
4 #include <string>
5 #include <list>
6 #include <stdint.h>
7 #include <glibmm.h>
8 #include <boost/shared_ptr.hpp>
9 #include "types.h"
10 #include "exceptions.h"
11
12 namespace xmlpp {
13         class Node;
14         class DomParser;
15 }
16
17 namespace libdcp {
18
19 class XMLNode
20 {
21 public:
22         XMLNode ();
23         XMLNode (xmlpp::Node const * node);
24
25 protected:
26         std::string string_node (std::string);
27         std::string optional_string_node (std::string);
28         ContentKind kind_node (std::string);
29         Fraction fraction_node (std::string);
30         int64_t int64_node (std::string);
31         void ignore_node (std::string);
32         void done ();
33
34         template <class T>
35         boost::shared_ptr<T> sub_node (std::string name) {
36                 return boost::shared_ptr<T> (new T (xml_node (name)));
37         }
38
39         template <class T>
40         boost::shared_ptr<T> optional_sub_node (std::string name) {
41                 std::list<xmlpp::Node*> n = xml_nodes (name);
42                 if (n.size() > 1) {
43                         throw XMLError ("duplicate XML tag");
44                 } else if (n.empty ()) {
45                         return boost::shared_ptr<T> ();
46                 }
47                 
48                 return boost::shared_ptr<T> (new T (n.front ()));
49         }
50         
51         template <class T>
52         std::list<boost::shared_ptr<T> > sub_nodes (std::string name, std::string sub) {
53                 XMLNode p (xml_node (name));
54                 std::list<xmlpp::Node*> n = p.xml_nodes (sub);
55                 std::list<boost::shared_ptr<T> > r;
56                 for (typename std::list<xmlpp::Node*>::iterator i = n.begin(); i != n.end(); ++i) {
57                         r.push_back (boost::shared_ptr<T> (new T (*i)));
58                 }
59                 return r;
60         }
61
62         xmlpp::Node const * _node;
63
64 private:
65         xmlpp::Node* xml_node (std::string);
66         std::list<xmlpp::Node*> xml_nodes (std::string);
67         std::list<Glib::ustring> _taken;
68 };
69
70 class XMLFile : public XMLNode
71 {
72 public:
73         XMLFile (std::string file, std::string root_name);
74         virtual ~XMLFile ();
75
76 private:
77         xmlpp::DomParser* _parser;
78 };
79
80 }
81
82 #endif