tiny_xml.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // tiny XML sub-set tools --------------------------------------------------//
  2. // (C) Copyright Beman Dawes 2002. Permission to copy,
  3. // use, modify, sell and distribute this software is granted provided this
  4. // copyright notice appears in all copies. This software is provided "as is"
  5. // without express or implied warranty, and with no claim as to its
  6. // suitability for any purpose.
  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. #ifndef BOOST_TINY_XML_H
  14. #define BOOST_TINY_XML_H
  15. #include "boost/smart_ptr.hpp" // for shared_ptr
  16. #include "boost/utility.hpp" // for noncopyable
  17. #include <list>
  18. #include <iostream>
  19. #include <string>
  20. namespace boost
  21. {
  22. namespace tiny_xml
  23. {
  24. class element;
  25. struct attribute
  26. {
  27. std::string name;
  28. std::string value;
  29. attribute(){}
  30. attribute( const std::string & name, const std::string & value )
  31. : name(name), value(value) {}
  32. };
  33. typedef boost::shared_ptr< element > element_ptr;
  34. typedef std::list< element_ptr > element_list;
  35. typedef std::list< attribute > attribute_list;
  36. class element
  37. : boost::noncopyable // because deep copy sematics would be required
  38. {
  39. public:
  40. std::string name;
  41. attribute_list attributes;
  42. element_list elements;
  43. std::string content;
  44. element() {}
  45. explicit element( const std::string & name ) : name(name) {}
  46. };
  47. element_ptr parse( std::istream & in, const std::string & msg );
  48. // Precondition: stream positioned at either the initial "<"
  49. // or the first character after the initial "<".
  50. // Postcondition: stream positioned at the first character after final
  51. // ">" (or eof).
  52. // Returns: an element_ptr to an element representing the parsed stream.
  53. // Throws: std::string on syntax error. msg appended to what() string.
  54. void write( const element & e, std::ostream & out );
  55. }
  56. }
  57. #endif // BOOST_TINY_XML_H
粤ICP备19079148号