Debian Codegraph @

Utilities/scr2xml/resource_tree.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_tree.h"
30 #include "resource_manager.h"
31
32 #include <ClanLib/core.h>
33
34 #include <iostream>
35 #include <map>
36 #include <unistd.h>
37
38 struct Resource
39 {
40 Resource(std::string n, std::string l, const ResourceOptions &o): name(n), location(l), options(o)
41 {type=options.get_option("type").get_values()[0];options.remove("type");}
42 std::string name;
43 std::string type;
44 std::string location;
45 ResourceOptions options;
46 };
47
48 struct Section
49 {
50 Section(std::string n, Section *p=0):parent(p), name(n) {}
51 std::string name;
52 std::vector<Section*> sections;
53 std::vector<Resource*> resources;
54 Section* parent;
55
56 ~Section() {}
57 };
58
59 struct Tree
60 {
61 Tree() {current_section = root = new Section("");};
62 ~Tree() {delete root;}
63 Section *root;
64 Section *current_section;
65 };
66
67 ResourceTree::ResourceTree(std::vector<char> buffer):
68 tree(new Tree),
69 manager(new ResourceManager(buffer, this))
70 {
71 }
72
73 ResourceTree::~ResourceTree()
74 {
75 delete manager;
76 delete tree;
77 }
78
79 void ResourceTree::add_section(std::string name)
80 {
81 Section* sect=new Section(name,tree->current_section);
82 tree->current_section->sections.push_back(sect);
83 tree->current_section = sect;
84 }
85
86 void ResourceTree::end_section()
87 {
88 tree->current_section = tree->current_section->parent;
89 }
90
91 void ResourceTree::add_resource(std::string name, std::string location, const ResourceOptions &options)
92 {
93 tree->current_section->resources.push_back(new Resource(name, location, options));
94 }
95
96 std::map<std::string, void (*)(Resource*, CL_DomElement&, CL_DomDocument&)> resource_map;
97
98 std::string escape_option(const std::string& opt)
99 {
100 std::string str;
101 std::string::const_iterator it = opt.begin();
102 for(;it!=opt.end();++it)
103 {
104 if(*it=='"')
105 str += "&quot;";
106 else
107 str += *it;
108 }
109 return str;
110 }
111
112 std::string option_to_string(const std::vector<std::string> &value)
113 {
114 std::string str;
115 std::vector<std::string>::const_iterator it = value.begin();
116 str = escape_option(*it++);
117 for(;it != value.end();++it)
118 {
119 str += ",";
120 str += escape_option(*it);
121 }
122
123 return str;
124 }
125
126 void basic_resource_element(Resource* res, CL_DomElement& res_elem, CL_DomDocument& document)
127 {
128 if(res->location!="")
129 res_elem.set_attribute("value", res->location);
130 }
131
132 void file_resource_element(Resource* res, CL_DomElement& res_elem, CL_DomDocument& document)
133 {
134 res_elem.set_attribute("file", res->location);
135 }
136
137 void sprite_resource_element(Resource* res, CL_DomElement& res_elem, CL_DomDocument& document)
138 {
139 for(int i=1;;i++)
140 {
141 std::string str_image = "image";
142 str_image += CL_String::from_int(i);
143 if(!res->options.exists(str_image))
144 break;
145
146 CL_DomElement elem = document.create_element("image");
147 elem.set_attribute("name", str_image);
148 elem.set_attribute("file", option_to_string(res->options.get_option(str_image).get_values()));
149 res->options.remove(str_image);
150
151
152 // I'm not really sure what the format should look like
153
154 if(res->options.exists(str_image + "_method"))
155 {
156 std::string method = res->options.get_option(str_image + "_method").get_values()[0];
157 res->options.remove(str_image + "_method");
158 CL_DomElement method_elem = document.create_element(method);
159
160 std::string method_options[] = {"pos","size","array","array_skipframes","spacing","tcol","trans_limit"};
161
162 for(int j=0;j<sizeof(method_options)/sizeof(method_options[0]);j++)
163 {
164 std::string str_option = str_image + "_" + method_options[j];
165 if(res->options.exists(str_option))
166 {
167
168 method_elem.set_attribute(method_options[j], option_to_string(res->options.get_option(str_option).get_values()));
169 res->options.remove(str_option);
170 }
171 }
172
173 elem.append_child(method_elem);
174 }
175
176 res_elem.append_child(elem);
177 }
178 }
179
180 void init_resource_map()
181 {
182 resource_map["integer"]=basic_resource_element;
183 resource_map["string"]=basic_resource_element;
184 resource_map["font"]=basic_resource_element;
185
186 resource_map["surface"]=file_resource_element;
187 resource_map["sample"]=file_resource_element;
188
189 resource_map["sprite"]=sprite_resource_element;
190 }
191
192
193 void add_resource_element(Resource* res, CL_DomElement& sect_elem, CL_DomDocument& document)
194 {
195 CL_DomElement elem = document.create_element(res->type);
196 elem.set_attribute("name", res->name);
197
198 if(resource_map.find(res->type)!=resource_map.end())
199 resource_map[res->type](res, elem, document);
200 else
201 basic_resource_element(res, elem, document);
202
203 std::map<std::string, ResourceOption> options = res->options.get_options();
204
205 std::map<std::string, ResourceOption>::iterator it=options.begin();
206 for(;it!=options.end();++it)
207 elem.set_attribute(it->first, option_to_string(it->second.get_values()));
208 sect_elem.append_child(elem);
209 }
210
211 void add_section_element(Section* section, CL_DomElement& sect_elem, CL_DomDocument& document)
212 {
213 std::vector<Section*>::iterator sec_it = section->sections.begin();
214 for(;sec_it!=section->sections.end();++sec_it)
215 {
216 CL_DomElement elem = document.create_element("section");
217 elem.set_attribute("name", (*sec_it)->name);
218 add_section_element(*sec_it, elem, document);
219 sect_elem.append_child(elem);
220 }
221
222 std::vector<Resource*>::iterator res_it = section->resources.begin();
223 for(;res_it!=section->resources.end();++res_it)
224 {
225 add_resource_element(*res_it, sect_elem, document);
226 }
227 }
228
229
230
231 class OutputSource_Stdout : public CL_OutputSource
232 {
233 public:
234 int tell() const { return -1; }
235 int size() const { return -1; }
236 void open() { return; }
237 void close() { return; }
238 CL_OutputSource *clone() { return new OutputSource_Stdout; }
239 int write(const void *data, int size) { return ::write(1, data, size); }
240 };
241
242 void ResourceTree::write()
243 {
244 CL_DomDocument document;
245 CL_DomElement root_element = document.create_element("resources");
246
247 add_section_element(tree->root, root_element, document);
248
249 document.append_child(root_element);
250 document.save(new OutputSource_Stdout(), true);
251 }