tiny_xml.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // tiny XML sub-set tools --------------------------------------------------//
  2. // (C) Copyright Beman Dawes 2002. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // Provides self-contained tools for this XML sub-set:
  6. //
  7. // element ::= { "<" name { name "=" "\"" value "\"" } ">"
  8. // {element} [contents] "</" name ">" }
  9. //
  10. // The point of "self-contained" is to minimize tool-chain dependencies.
  11. #ifndef BOOST_TINY_XML_H
  12. #define BOOST_TINY_XML_H
  13. #include "boost/smart_ptr.hpp" // for shared_ptr
  14. #include "boost/utility.hpp" // for noncopyable
  15. #include <list>
  16. #include <iostream>
  17. #include <string>
  18. namespace boost
  19. {
  20. namespace tiny_xml
  21. {
  22. class element;
  23. struct attribute
  24. {
  25. std::string name;
  26. std::string value;
  27. attribute(){}
  28. attribute( const std::string & name, const std::string & value )
  29. : name(name), value(value) {}
  30. };
  31. typedef boost::shared_ptr< element > element_ptr;
  32. typedef std::list< element_ptr > element_list;
  33. typedef std::list< attribute > attribute_list;
  34. class element
  35. : private boost::noncopyable // because deep copy sematics would be required
  36. {
  37. public:
  38. std::string name;
  39. attribute_list attributes;
  40. element_list elements;
  41. std::string content;
  42. element() {}
  43. explicit element( const std::string & name ) : name(name) {}
  44. };
  45. element_ptr parse( std::istream & in, const std::string & msg );
  46. // Precondition: stream positioned at either the initial "<"
  47. // or the first character after the initial "<".
  48. // Postcondition: stream positioned at the first character after final
  49. // ">" (or eof).
  50. // Returns: an element_ptr to an element representing the parsed stream.
  51. // Throws: std::string on syntax error. msg appended to what() string.
  52. void write( const element & e, std::ostream & out );
  53. }
  54. }
  55. #endif // BOOST_TINY_XML_H
粤ICP备19079148号