• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  tiny XML sub-set tools  --------------------------------------------------//
2 
3 //  (C) Copyright Beman Dawes 2002.  Distributed under the Boost
4 //  Software License, Version 1.0. (See accompanying file
5 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 //  Provides self-contained tools for this XML sub-set:
8 //
9 //    element ::= { "<" name { name "=" "\"" value "\"" } ">"
10 //                  {element} [contents] "</" name ">" }
11 //
12 //  The point of "self-contained" is to minimize tool-chain dependencies.
13 
14 #ifndef BOOST_TINY_XML_H
15 #define BOOST_TINY_XML_H
16 
17 #include "boost/smart_ptr.hpp" // for shared_ptr
18 #include "boost/utility.hpp"   // for noncopyable
19 #include <list>
20 #include <iostream>
21 #include <string>
22 
23 namespace boost
24 {
25    namespace tiny_xml
26    {
27       class element;
28       struct attribute
29       {
30          std::string name;
31          std::string value;
32 
attributeboost::tiny_xml::attribute33          attribute(){}
attributeboost::tiny_xml::attribute34          attribute( const std::string & name, const std::string & value )
35             : name(name), value(value) {}
36       };
37       typedef boost::shared_ptr< element >  element_ptr;
38       typedef boost::weak_ptr< element >    weak_element_ptr;
39       typedef std::list< element_ptr  >     element_list;
40       typedef std::list< attribute >        attribute_list;
41 
42       class element
43          : private boost::noncopyable  // because deep copy semantics would be required
44       {
45       public:
46          // The name of the XML element, or "" if this is inline content,
47          // or begins with '?' if this is a processing instruction.
48          std::string       name;
49          // List of attributes applied to this element
50          attribute_list    attributes;
51          // List of sub-elements, this will be empty if the name is empty
52          // or starts with a '?'.  Plain text content will be inside
53          // anonymous elements in this list - this preserves the order of
54          // plain text mixed with true XML <elements>.
55          element_list      elements;
56          // The plain text content of this element, only present if the name is ""
57          // or if this is a processing instruction in which case it is the content
58          // after the name of the instruction.
59          std::string       content;
60          // Pointer to our parent
61          weak_element_ptr  parent;
62 
element()63          element() {}
element(const std::string & name)64          explicit element( const std::string & name ) : name(name) {}
65       };
66 
67       // Precondition: stream positioned at either the initial "<"
68       // or the first character after the initial "<".
69       // Postcondition: stream positioned at the first character after final
70       //  ">" (or eof).
71       // Returns: an element_ptr to an element representing the parsed stream.
72       // Throws: std::string on syntax error. msg appended to what() string.
73       element_ptr parse( std::istream & in, const std::string & msg );
74 
75       void write( const element & e, std::ostream & out );
76 
77    }
78 }
79 
80 #endif  // BOOST_TINY_XML_H
81 
82 
83 
84