LTC Slave, add support for variable framerates
[ardour.git] / libs / pbd / pbd / xml++.h
1 /* xml++.h
2  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3  * are covered by the GNU Lesser General Public License, which should be
4  * included with libxml++ as the file COPYING.
5  * Modified for Ardour and released under the same terms.
6  */
7
8 #include <string>
9 #include <list>
10 #include <map>
11 #include <cstdio>
12 #include <cstdarg>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <boost/shared_ptr.hpp>
17
18 #ifndef __XML_H
19 #define __XML_H
20
21 class XMLTree;
22 class XMLNode;
23 class XMLProperty;
24
25 typedef std::list<XMLNode *>                   XMLNodeList;
26 typedef std::list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
27 typedef XMLNodeList::iterator                  XMLNodeIterator;
28 typedef XMLNodeList::const_iterator            XMLNodeConstIterator;
29 typedef std::list<XMLProperty*>                XMLPropertyList;
30 typedef XMLPropertyList::iterator              XMLPropertyIterator;
31 typedef XMLPropertyList::const_iterator        XMLPropertyConstIterator;
32 typedef std::map<std::string, XMLProperty*>    XMLPropertyMap;
33
34 class XMLTree {
35 public:
36         XMLTree();
37         XMLTree(const std::string& fn, bool validate = false);
38         XMLTree(const XMLTree*);
39         ~XMLTree();
40
41         XMLNode* root() const         { return _root; }
42         XMLNode* set_root(XMLNode* n) { return _root = n; }
43
44         const std::string& filename() const               { return _filename; }
45         const std::string& set_filename(const std::string& fn) { return _filename = fn; }
46
47         int compression() const { return _compression; }
48         int set_compression(int);
49
50         bool read() { return read_internal(false); }
51         bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
52         bool read_and_validate() { return read_internal(true); }
53         bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
54         bool read_buffer(const std::string&);
55
56         bool write() const;
57         bool write(const std::string& fn) { set_filename(fn); return write(); }
58
59         void debug (FILE*) const;
60
61         const std::string& write_buffer() const;
62
63         boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
64
65 private:
66         bool read_internal(bool validate);
67         
68         std::string _filename;
69         XMLNode*    _root;
70         xmlDocPtr   _doc;
71         int         _compression;
72 };
73
74 class XMLNode {
75 public:
76         XMLNode(const std::string& name);
77         XMLNode(const std::string& name, const std::string& content);
78         XMLNode(const XMLNode& other);
79         ~XMLNode();
80
81         XMLNode& operator= (const XMLNode& other);
82
83         const std::string& name() const { return _name; }
84
85         bool          is_content() const { return _is_content; }
86         const std::string& content()    const { return _content; }
87         const std::string& set_content(const std::string&);
88         XMLNode*      add_content(const std::string& s = std::string());
89
90         const XMLNodeList& children(const std::string& str = std::string()) const;
91         XMLNode* child(const char*) const;
92         XMLNode* add_child(const char *);
93         XMLNode* add_child_copy(const XMLNode&);
94         void     add_child_nocopy(XMLNode&);
95
96         std::string attribute_value();
97
98         const XMLPropertyList& properties() const { return _proplist; }
99         XMLProperty*       property(const char*);
100         XMLProperty*       property(const std::string&);
101         const XMLProperty* property(const char* n)   const { return const_cast<XMLNode*>(this)->property(n); }
102         const XMLProperty* property(const std::string& n) const { return const_cast<XMLNode*>(this)->property(n); }
103         
104         XMLProperty* add_property(const char* name, const std::string& value);
105         XMLProperty* add_property(const char* name, const char* value = "");
106         XMLProperty* add_property(const char* name, const long value);
107
108         void remove_property(const std::string&);
109         void remove_property_recursively(const std::string&);
110
111         /** Remove all nodes with the name passed to remove_nodes */
112         void remove_nodes(const std::string&);
113         /** Remove and delete all nodes with the name passed to remove_nodes */
114         void remove_nodes_and_delete(const std::string&);
115         /** Remove and delete all nodes with property prop matching val */
116         void remove_nodes_and_delete(const std::string& propname, const std::string& val);
117
118         void dump (std::ostream &, std::string p = "") const;
119
120 private:
121         std::string         _name;
122         bool                _is_content;
123         std::string         _content;
124         XMLNodeList         _children;
125         XMLPropertyList     _proplist;
126         XMLPropertyMap      _propmap;
127         mutable XMLNodeList _selected_children;
128
129         void clear_lists ();
130 };
131
132 class XMLProperty {
133 public:
134         XMLProperty(const std::string& n, const std::string& v = std::string());
135         ~XMLProperty();
136
137         const std::string& name() const { return _name; }
138         const std::string& value() const { return _value; }
139         const std::string& set_value(const std::string& v) { return _value = v; }
140
141 private:
142         std::string _name;
143         std::string _value;
144 };
145
146 class XMLException: public std::exception {
147 public:
148         explicit XMLException(const std::string msg) : _message(msg) {}
149         virtual ~XMLException() throw() {}
150
151         virtual const char* what() const throw() { return _message.c_str(); }
152
153 private:
154         std::string _message;
155 };
156
157 #endif /* __XML_H */
158