]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/xml/xmlwriter.h
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / libs / xml / xmlwriter.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_XML_XMLWRITER_H)
23 #define INCLUDED_XML_XMLWRITER_H
24
25 #include "convert.h"
26 #include <vector>
27 #include "xml/ixml.h"
28
29 class XMLEntityOutputStream
30 {
31   SingleCharacterOutputStream m_ostream;
32 public:
33   XMLEntityOutputStream(TextOutputStream& ostream)
34     : m_ostream(ostream)
35   {
36   }
37   void write(const char c)
38   {
39     m_ostream.write(c);
40   }
41   void writeEscaped(const char c)
42   {
43     switch(c)
44     {
45     case '<':
46       write('&');
47       write('l');
48       write('t');
49       write(';');
50       break;
51     case '>':
52       write('&');
53       write('g');
54       write('t');
55       write(';');
56       break;
57     case '"':
58       write('&');
59       write('q');
60       write('u');
61       write('o');
62       write('t');
63       write(';');
64       break;
65     case '&':
66       write('&');
67       write('a');
68       write('m');
69       write('p');
70       write(';');
71       break;
72     default:
73       write(c);
74       break;
75     }
76   }
77   std::size_t write(const char* buffer, std::size_t length)
78   {
79     const char*const end = buffer + length;
80     for(const char* p = buffer; p != end; ++p)
81     {
82       writeEscaped(*p);
83     }
84     return length;
85   }
86 };
87
88 template<typename T>
89 inline XMLEntityOutputStream& operator<<(XMLEntityOutputStream& ostream, const T& t)
90 {
91   return ostream_write(ostream, t);
92 }
93
94
95 class XMLStreamWriter : public XMLImporter, public XMLAttrVisitor
96 {
97   class state_t
98   {
99   public:
100     enum EState
101     {
102       eStartElement,
103       eContent,
104     };
105     state_t()
106       : m_state(eStartElement)
107     {}
108     EState m_state;
109   };
110
111   XMLEntityOutputStream m_ostream;
112   std::vector<state_t> m_elements;
113
114   void write_cdata(const char* buffer, std::size_t length)
115   {
116     m_ostream << StringRange(buffer, buffer + length);
117   }
118   void write_string(const char* string)
119   {
120     m_ostream << string;
121   }
122   void write_quoted_string(const char* string)
123   {
124     m_ostream.write('"');
125     m_ostream << string;
126     m_ostream.write('"');
127   }
128 public:
129   XMLStreamWriter(TextOutputStream& ostream)
130     : m_ostream(ostream)
131   {
132     m_elements.push_back(state_t());
133     m_elements.back().m_state = state_t::eContent;
134     m_ostream.write('<');
135     m_ostream.write('?');
136     write_string("xml");
137     visit("version", "1.0");
138     m_ostream.write('?');
139     m_ostream.write('>');
140   }
141
142   void pushElement(const XMLElement& element)
143   {
144     if(m_elements.back().m_state == state_t::eStartElement)
145     {
146       m_elements.back().m_state = state_t::eContent;
147       m_ostream.write('>');
148     }
149
150     m_elements.push_back(state_t());
151
152     m_ostream.write('<');
153     write_string(element.name());
154     element.forEachAttribute(*this);
155   }
156   void popElement(const char* name)
157   {
158     if(m_elements.back().m_state == state_t::eStartElement)
159     {
160       m_ostream.write('/');
161       m_ostream.write('>');
162       m_elements.pop_back();
163     }
164     else
165     {
166       m_ostream.write('<');
167       m_ostream.write('/');
168       write_string(name);
169       m_ostream.write('>');
170       m_elements.pop_back();
171     }
172   }
173   std::size_t write(const char* data, std::size_t length)
174   {
175     if(m_elements.back().m_state == state_t::eStartElement)
176     {
177       m_elements.back().m_state = state_t::eContent;
178       m_ostream.write('>');
179     }
180     write_cdata(data, length);
181     return length;
182   }
183
184   void visit(const char* name, const char* value)
185   {
186     m_ostream.write(' ');
187     write_string(name);
188     m_ostream.write('=');
189     write_quoted_string(value);
190   }
191 };
192
193
194 #endif