summaryrefslogtreecommitdiff
path: root/tools/regression/src/detail/tiny_xml.cpp
blob: 682c04ff7b2e4d9bdfd6da406387e972a8f526fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//  tiny XML sub-set tools implementation  -----------------------------------//

//  (C) Copyright Beman Dawes 2002.  Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "tiny_xml.hpp"
#include <cassert>
#include <cstring>

namespace
{

  void eat_whitespace( char & c, std::istream & in )
  {
    while ( c == ' ' || c == '\r' || c == '\n' || c == '\t' )
      in.get( c );
  }

  std::string get_name( char & c, std::istream & in )
  {
    std::string result;
    eat_whitespace( c, in );
    while ( std::strchr(
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.", c )
      != 0 )
    {
      result += c;
      if(!in.get( c ))
        throw std::string("xml: unexpected eof");
    }
    return result;
  }

  void eat_delim( char & c, std::istream & in,
                  char delim, const std::string & msg )
  {
    eat_whitespace( c, in );
    if ( c != delim )
      throw std::string("xml syntax error, expected ") + delim
       + " (" + msg + ")";
    in.get( c );
  }

  std::string get_value( char & c, std::istream & in )
  {
    std::string result;
    while ( c != '\"' )
    {
      result += c;
      in.get( c );
    }
    in.get( c );
    return result;
  }

}

namespace boost
{
  namespace tiny_xml
  {

  //  parse  -----------------------------------------------------------------//

    element_ptr parse( std::istream & in, const std::string & msg )
    {
      char c = 0;  // current character
      element_ptr e( new element );

      if(!in.get( c ))
        throw std::string("xml: unexpected eof");
      if ( c == '<' )
        if(!in.get( c ))
          throw std::string("xml: unexpected eof");

      e->name = get_name( c, in );
      eat_whitespace( c, in );

      // attributes
      while ( c != '>' )
      {
        attribute a;
        a.name = get_name( c, in );

        eat_delim( c, in, '=', msg );
        eat_delim( c, in, '\"', msg );

        a.value = get_value( c, in );

        e->attributes.push_back( a );
        eat_whitespace( c, in );
      }
      if(!in.get( c )) // next after '>'
        throw std::string("xml: unexpected eof");

      eat_whitespace( c, in );

      // sub-elements
      while ( c == '<' )
      {
        if ( in.peek() == '/' ) break;
        e->elements.push_back( parse( in, msg ) );
        in.get( c ); // next after '>'
        eat_whitespace( c, in );
      }

      // content
      if ( c != '<' )
      {
        e->content += '\n';
        while ( c != '<' )
        {
          e->content += c;
          if(!in.get( c ))
            throw std::string("xml: unexpected eof");
        }
      }

      assert( c == '<' );
      if(!in.get( c )) // next after '<'
        throw std::string("xml: unexpected eof");

      eat_delim( c, in, '/', msg );
      std::string end_name( get_name( c, in ) );
      if ( e->name != end_name )
        throw std::string("xml syntax error: beginning name ")
          + e->name + " did not match end name " + end_name
          + " (" + msg + ")";

      eat_delim( c, in, '>', msg );
      return e;
    }

    //  write  ---------------------------------------------------------------//

    void write( const element & e, std::ostream & out )
    {
      out << "<" << e.name;
      if ( !e.attributes.empty() )
      {
        for( attribute_list::const_iterator itr = e.attributes.begin();
             itr != e.attributes.end(); ++itr )
        {
          out << " " << itr->name << "=\"" << itr->value << "\"";
        }
      }
      out << ">";
      if ( !e.elements.empty() )
      {
        out << "\n";
        for( element_list::const_iterator itr = e.elements.begin();
             itr != e.elements.end(); ++itr )
        {
          write( **itr, out );
        }
      }
      if ( !e.content.empty() )
      {
        out << e.content;
      }
      out << "</" << e.name << ">\n";
    }

  } // namespace tiny_xml
} // namespace boost