Debian Codegraph @

Utilities/scr2xml/resource_manager.cpp

raw
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2005 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** (if your name is missing here, please add it)
27 */
28
29 #include "resource_manager.h"
30 #include "resource_tokenizer.h"
31
32 #include "resource_tree.h"
33
34 #include <ClanLib/Core/System/clanstring.h>
35 #include <ClanLib/Core/System/error.h>
36 #include <ClanLib/Core/System/cl_assert.h>
37
38 #include <algorithm>
39 #include <ctype.h>
40 #include <iostream>
41 #include <map>
42
43 ResourceManager::ResourceManager(std::vector<char>& buffer, ResourceTree* t)
44 : config_file_data(buffer), tree(t)
45 {
46 parse();
47 }
48
49 ResourceManager::~ResourceManager()
50 {
51 }
52
53 void ResourceManager::parse()
54 {
55 try
56 {
57 ResourceTokenizer lexer(config_file_data);
58
59 while (true)
60 {
61 std::string token = lexer.get_next_token();
62 if (token == "") break;
63
64 if (token == "location") // obsolete. Will read it but issues a warning.
65 {
66 token = lexer.get_next_token();
67 if (token != "=")
68 throw CL_Error(lexer.write_error("Missing '=' following global 'location'-declaration"));
69
70 token = lexer.get_next_token();
71 if (token != "datafile")
72 throw CL_Error(lexer.write_error("Syntax error following global 'location'-declaration"));
73
74 token = lexer.get_next_token();
75 if (token != "(")
76 throw CL_Error(lexer.write_error("Missing '(' following 'datafile' keyword"));
77
78 token = lexer.get_next_token();
79
80 token = lexer.get_next_token();
81 if (token != ")")
82 throw CL_Error(lexer.write_error("Missing ')' following datafile filename"));
83
84 token = lexer.get_next_token();
85 if (token != ";")
86 throw CL_Error(lexer.write_error("Missing ';' following datafile filename"));
87 }
88 else // assume it is a section body (resource or section)
89 {
90 parse_section_body(token, lexer, "");
91 }
92 }
93 }
94 catch (...)
95 {
96 throw;
97 }
98 }
99
100 void ResourceManager::parse_section_body(
101 std::string token,
102 ResourceTokenizer &lexer,
103 std::string prefix)
104 {
105 if (token == "section")
106 {
107 std::string section_name = lexer.get_next_token();
108 std::string section_prefix = prefix + section_name;
109
110 token = lexer.get_next_token();
111 if (token != "{")
112 throw CL_Error(lexer.write_error("Missing '{'"));
113
114 tree->add_section(section_name);
115
116 while (true)
117 {
118 token = lexer.get_next_token();
119 if (token == "}") break;
120 parse_section_body(token, lexer, section_prefix);
121 }
122
123 tree->end_section();
124 }
125 else // must be a resource then
126 {
127 std::string name = token;
128
129 token = lexer.get_next_token();
130 if (token != "=")
131 {
132 std::string err;
133 err = err + "Missing '=' following declaration of resource '" + name + "'";
134 throw CL_Error(lexer.write_error(err.c_str()));
135 }
136
137 // read the location (if available):
138 std::string location;
139 token = lexer.get_next_token();
140 if(token == "(")
141 location = "";
142 else {
143 location = token;
144 token = lexer.get_next_token();
145 }
146
147 // read resource options:
148 if (token != "(")
149 {
150 std::string err;
151 err = err + "Missing '(' following declaration of resource '" + name + "'";
152 throw CL_Error(lexer.write_error(err.c_str()));
153 }
154
155 ResourceOptions options;
156 while (true)
157 {
158 token = lexer.get_next_token();
159 if (token == ")") break;
160
161 std::string option_name = token;
162
163 token = lexer.get_next_token();
164 if (token != "=")
165 {
166 std::string err;
167 err = err + "Syntax error in options following declaration of resource '" + name + "'";
168 throw CL_Error(lexer.write_error(err.c_str()));
169 }
170
171 token = lexer.get_next_token();
172 if (token == "(") // it is a value list
173 {
174 ResourceOption option(option_name);
175
176 while (true)
177 {
178 token = lexer.get_next_token();
179 if (token == ")") break;
180
181 option.add_value(token);
182
183 token = lexer.get_next_token();
184 if (token == ")") break;
185 if (token != ",")
186 {
187 std::string err;
188 err = err + "Missing ',' or ')' following declaration of multiple option values in option '" +
189 option_name + "' in resource '" + name + "' (found '" + token + "')";
190 throw CL_Error(lexer.write_error(err.c_str()));
191 }
192 }
193
194 options.add(option);
195 }
196 else
197 {
198 std::string option_value = token;
199
200 options.add(ResourceOption(option_name, option_value));
201 }
202
203 token = lexer.get_next_token();
204 if (token == ")") break;
205 if (token != ",")
206 {
207 std::string err;
208 err = err + "Syntax error in options following declaration of resource '" + name + "'";
209 throw CL_Error(lexer.write_error(err.c_str()));
210 }
211 }
212
213 token = lexer.get_next_token();
214 if (token != ";")
215 {
216 std::string err;
217 err = err + "Missing ';' following declaration of resource '" + name +"'";
218 throw CL_Error(lexer.write_error(err.c_str()));
219 }
220
221 if (options.exists("type") == false)
222 {
223 std::string err;
224 err = err + "No type declared for resource '" + name + "'";
225 throw CL_Error(lexer.write_error(err.c_str()));
226 }
227
228 tree->add_resource(name, location, options);
229 }
230 }
231
232 std::string ResourceManager::get_filename(const std::string &pathname)
233 {
234 std::string::size_type pos1 = pathname.find_last_of('/');
235 std::string::size_type pos2 = pathname.find_last_of('\\');
236
237 if (pos1 == std::string::npos && pos2 == std::string::npos)
238 {
239 return pathname;
240 }
241 else if (pos1 != std::string::npos && pos2 == std::string::npos)
242 {
243 return pathname.substr(pos1 + 1);
244 }
245 else if (pos1 == std::string::npos && pos2 != std::string::npos)
246 {
247 return pathname.substr(pos2 + 1);
248 }
249 else // (pos1 != std::string::npos && pos2 != std::string::npos)
250 {
251 return pathname.substr(((pos1>pos2) ? pos1 : pos2) + 1);
252 }
253 }
254
255 std::string ResourceManager::get_path(const std::string &pathname)
256 {
257 std::string::size_type length = pathname.length();
258 std::string::size_type pos1 = pathname.find_last_of('/');
259 std::string::size_type pos2 = pathname.find_last_of('\\');
260 if (pos1 == std::string::npos) pos1 = length;
261 if (pos2 == std::string::npos) pos2 = length;
262 if (pos1 < 0) pos1 = length;
263 if (pos2 < 0) pos2 = length;
264
265 if (pos1 == length && pos2 == length) return std::string();
266
267 int pos = (pos1 < pos2) ? pos1 : pos2;
268 return pathname.substr(0, pos);
269 }