fixing 20 VS waring msg
[reactos.git] / reactos / tools / xml.h
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #ifndef XML_H
19 #define XML_H
20
21 #include <string>
22 #include <vector>
23 #include <stdarg.h>
24
25 class XMLElement;
26
27 extern std::string working_directory;
28
29 void
30 InitWorkingDirectory();
31
32 #ifdef _MSC_VER
33 unsigned __int64
34 #else
35 unsigned long long
36 #endif
37 filelen ( FILE* f );
38
39 class XMLException
40 {
41 public:
42 XMLException ( const std::string& location, const char* format, ... );
43 const std::string& operator *() { return _e; }
44
45 protected:
46 XMLException() {}
47 void SetExceptionV ( const std::string& location, const char* format, va_list args );
48 void SetException ( const std::string& location, const char* format, ... );
49
50 private:
51 std::string _e;
52 };
53
54 class XMLSyntaxErrorException : public XMLException
55 {
56 public:
57 XMLSyntaxErrorException (
58 const std::string& location,
59 const char* format, ... )
60 {
61 va_list args;
62 va_start ( args, format );
63 SetExceptionV ( location, format, args );
64 va_end ( args );
65 }
66 };
67
68 class XMLRequiredAttributeNotFoundException : public XMLException
69 {
70 public:
71 XMLRequiredAttributeNotFoundException (
72 const std::string& location,
73 const std::string& attributeName,
74 const std::string& elementName )
75 {
76 SetException ( location, "Required attribute '%s' not found in element '%s'",
77 attributeName.c_str(),
78 elementName.c_str() );
79 }
80 };
81
82 class XMLInvalidBuildFileException : public XMLException
83 {
84 public:
85 XMLInvalidBuildFileException (
86 const std::string& location,
87 const char* format,
88 ... )
89 {
90 va_list args;
91 va_start ( args, format );
92 SetExceptionV ( location, format, args );
93 va_end ( args );
94 }
95 };
96
97 class XMLFileNotFoundException : public XMLException
98 {
99 public:
100 XMLFileNotFoundException (
101 const std::string& location,
102 const std::string& filename )
103 {
104 SetException ( location, "Can't open file '%s'", filename.c_str() );
105 }
106 };
107
108 class Path
109 {
110 std::vector<std::string> path;
111 public:
112 Path(); // initializes path to getcwd();
113 Path ( const Path& cwd, const std::string& filename );
114 std::string Fixup ( const std::string& filename, bool include_filename ) const;
115
116 std::string RelativeFromWorkingDirectory ();
117 static std::string RelativeFromWorkingDirectory ( const std::string& path );
118 static std::string RelativeFromDirectory ( const std::string& path, const std::string& base_directory);
119
120 static void Split (
121 std::vector<std::string>& out,
122 const std::string& path,
123 bool include_last );
124 };
125
126 class XMLInclude
127 {
128 public:
129 XMLElement *e;
130 Path path;
131 std::string topIncludeFilename;
132 bool fileExists;
133
134 XMLInclude (
135 XMLElement* e_,
136 const Path& path_,
137 const std::string topIncludeFilename_ )
138 : e ( e_ ),
139 path ( path_ ),
140 topIncludeFilename ( topIncludeFilename_ )
141 {
142 }
143 };
144
145 class XMLIncludes : public std::vector<XMLInclude*>
146 {
147 public:
148 ~XMLIncludes();
149 };
150
151 class XMLFile
152 {
153 friend class XMLElement;
154 public:
155 XMLFile();
156 void close();
157 bool open(const std::string& filename);
158 void next_token();
159 bool next_is_text();
160 bool more_tokens();
161 bool get_token ( std::string& token );
162 bool get_token ( std::string& token, std::string& location );
163 const std::string& filename() { return _filename; }
164 std::string Location() const;
165
166 private:
167 std::string _buf, _filename;
168
169 const char *_p, *_end;
170 };
171
172
173 class XMLAttribute
174 {
175 public:
176 std::string name;
177 std::string value;
178
179 XMLAttribute();
180 XMLAttribute ( const std::string& name_, const std::string& value_ );
181 XMLAttribute ( const XMLAttribute& );
182 XMLAttribute& operator = ( const XMLAttribute& );
183 };
184
185
186 class XMLElement
187 {
188 public:
189 XMLFile* xmlFile;
190 std::string location;
191 std::string name;
192 std::vector<XMLAttribute*> attributes;
193 XMLElement* parentElement;
194 std::vector<XMLElement*> subElements;
195 std::string value;
196
197 XMLElement (
198 XMLFile* xmlFile,
199 const std::string& location );
200
201 ~XMLElement();
202
203 bool Parse (
204 const std::string& token,
205 bool& end_tag);
206
207 void AddSubElement ( XMLElement* e );
208
209 XMLAttribute* GetAttribute (
210 const std::string& attribute,
211 bool required);
212
213 const XMLAttribute* GetAttribute (
214 const std::string& attribute,
215 bool required ) const;
216
217 int FindElement (
218 const std::string& type,
219 int prev = -1 ) const;
220
221 int GetElements (
222 const std::string& type,
223 std::vector<XMLElement*>& v );
224
225 int GetElements (
226 const std::string& type,
227 std::vector<const XMLElement*>& v ) const;
228 };
229
230 XMLElement*
231 XMLLoadFile (
232 const std::string& filename,
233 const Path& path,
234 XMLIncludes& includes );
235
236 XMLElement*
237 XMLLoadFile ( const std::string& filename );
238
239 #endif // XML_H