tiny_xml.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // tiny XML sub-set tools implementation -----------------------------------//
  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. #include "tiny_xml.hpp"
  8. #include <cassert>
  9. namespace
  10. {
  11. void eat_whitespace( char & c, std::istream & in )
  12. {
  13. while ( c == ' ' || c == '\r' || c == '\n' || c == '\t' )
  14. in.get( c );
  15. }
  16. std::string get_name( char & c, std::istream & in )
  17. {
  18. std::string result;
  19. eat_whitespace( c, in );
  20. while ( std::strchr(
  21. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.", c )
  22. != 0 )
  23. {
  24. result += c;
  25. in.get( c );
  26. }
  27. return result;
  28. }
  29. void eat_delim( char & c, std::istream & in,
  30. char delim, const std::string & msg )
  31. {
  32. eat_whitespace( c, in );
  33. if ( c != delim )
  34. throw std::string("xml syntax error, expected ") + delim
  35. + " (" + msg + ")";
  36. in.get( c );
  37. }
  38. std::string get_value( char & c, std::istream & in )
  39. {
  40. std::string result;
  41. while ( c != '\"' )
  42. {
  43. result += c;
  44. in.get( c );
  45. }
  46. in.get( c );
  47. return result;
  48. }
  49. }
  50. namespace boost
  51. {
  52. namespace tiny_xml
  53. {
  54. // parse -----------------------------------------------------------------//
  55. element_ptr parse( std::istream & in, const std::string & msg )
  56. {
  57. char c = 0; // current character
  58. element_ptr e( new element );
  59. in.get( c );
  60. if ( c == '<' ) in.get( c );
  61. e->name = get_name( c, in );
  62. eat_whitespace( c, in );
  63. // attributes
  64. while ( c != '>' )
  65. {
  66. attribute a;
  67. a.name = get_name( c, in );
  68. eat_delim( c, in, '=', msg );
  69. eat_delim( c, in, '\"', msg );
  70. a.value = get_value( c, in );
  71. e->attributes.push_back( a );
  72. eat_whitespace( c, in );
  73. }
  74. in.get( c ); // next after '>'
  75. eat_whitespace( c, in );
  76. // sub-elements
  77. while ( c == '<' )
  78. {
  79. if ( in.peek() == '/' ) break;
  80. e->elements.push_back( parse( in, msg ) );
  81. in.get( c ); // next after '>'
  82. eat_whitespace( c, in );
  83. }
  84. // content
  85. if ( c != '<' )
  86. {
  87. e->content += '\n';
  88. while ( c != '<' )
  89. {
  90. e->content += c;
  91. in.get( c );
  92. }
  93. }
  94. assert( c == '<' );
  95. in.get( c ); // next after '<'
  96. eat_delim( c, in, '/', msg );
  97. std::string end_name( get_name( c, in ) );
  98. if ( e->name != end_name )
  99. throw std::string("xml syntax error: beginning name ")
  100. + e->name + " did not match end name " + end_name
  101. + " (" + msg + ")";
  102. eat_delim( c, in, '>', msg );
  103. return e;
  104. }
  105. // write ---------------------------------------------------------------//
  106. void write( const element & e, std::ostream & out )
  107. {
  108. out << "<" << e.name;
  109. if ( !e.attributes.empty() )
  110. {
  111. for( attribute_list::const_iterator itr = e.attributes.begin();
  112. itr != e.attributes.end(); ++itr )
  113. {
  114. out << " " << itr->name << "=\"" << itr->value << "\"";
  115. }
  116. }
  117. out << ">";
  118. if ( !e.elements.empty() )
  119. {
  120. out << "\n";
  121. for( element_list::const_iterator itr = e.elements.begin();
  122. itr != e.elements.end(); ++itr )
  123. {
  124. write( **itr, out );
  125. }
  126. }
  127. if ( !e.content.empty() )
  128. {
  129. out << e.content;
  130. }
  131. out << "</" << e.name << ">\n";
  132. }
  133. } // namespace tiny_xml
  134. } // namespace boost
粤ICP备19079148号