summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
commit14c7f45cdae826f88dc539c8c38dd95caf305731 (patch)
tree832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h
parent7c84ede3781c27db73403bd6302f320c76a58c8c (diff)
downloadrockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.tar.gz
rockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.zip
Add zook's ZenUtils to SVN
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18010 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h735
1 files changed, 735 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h
new file mode 100755
index 0000000000..a0bba4c9b3
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h
@@ -0,0 +1,735 @@
1/*
2* ResourceDirectory.cpp - Part of the PeLib library.
3*
4* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
5* All rights reserved.
6*
7* This software is licensed under the zlib/libpng License.
8* For more details see http://www.opensource.org/licenses/zlib-license.php
9* or the license information file (license.htm) in the root directory
10* of PeLib.
11*/
12
13#ifndef RESOURCEDIRECTORY_H
14#define RESOURCEDIRECTORY_H
15
16#include "PeLibInc.h"
17
18namespace PeLib
19{
20 class ResourceElement;
21
22 /// The class ResourceChild is used to store information about a resource node.
23 class ResourceChild
24 {
25 friend class ResourceElement;
26 friend class ResourceDirectory;
27 friend class ResourceNode;
28 friend class ResourceLeaf;
29
30 /// Stores name and offset of a resource node.
31 PELIB_IMG_RES_DIR_ENTRY entry;
32 /// A pointer to one of the node's child nodes.
33 ResourceElement* child;
34
35 public:
36 /// Function which compares a resource ID to the node's resource ID.
37 bool equalId(dword wId) const; // EXPORT
38 /// Function which compares a string to the node's resource name.
39 bool equalName(std::string strName) const; // EXPORT
40 /// Predicate that determines if a child is identified by name or by ID.
41 bool isNamedResource() const; // EXPORT
42 /// Used for sorting a node's children.
43 bool operator<(const ResourceChild& rc) const; // EXPORT
44 /// Returns the size of a resource child.
45// unsigned int size() const;
46
47 /// Standard constructor. Does absolutely nothing.
48 ResourceChild();
49 /// Makes a deep copy of a ResourceChild object.
50 ResourceChild(const ResourceChild& rhs);
51 /// Makes a deep copy of a ResourceChild object.
52 ResourceChild& operator=(const ResourceChild& rhs);
53 /// Deletes a ResourceChild object.
54 ~ResourceChild();
55 };
56
57 /// Base class for ResourceNode and ResourceLeaf, the elements of the resource tree.
58 /// \todo write
59 class ResourceElement
60 {
61 friend class ResourceChild;
62 friend class ResourceNode;
63 friend class ResourceLeaf;
64
65 protected:
66 /// Stores RVA of the resource element in the file.
67 unsigned int uiElementRva;
68
69 /// Reads the next resource element from the InputBuffer.
70 virtual int read(InputBuffer&, unsigned int, unsigned int/*, const std::string&*/) = 0;
71 /// Writes the next resource element into the OutputBuffer.
72 virtual void rebuild(OutputBuffer&, unsigned int&, unsigned int, const std::string&) const = 0;
73
74 public:
75 /// Returns the RVA of the element in the file.
76 unsigned int getElementRva() const; // EXPORT
77 /// Indicates if the resource element is a leaf or a node.
78 virtual bool isLeaf() const = 0; // EXPORT
79 /// Corrects erroneous valeus in the ResourceElement.
80 virtual void makeValid() = 0; // EXPORT
81 /// Returns the size of a resource element.
82// virtual unsigned int size() const = 0;
83 /// Necessary virtual destructor.
84 virtual ~ResourceElement() {}
85 };
86
87 /// ResourceLeafs represent the leafs of the resource tree: The actual resources.
88 class ResourceLeaf : public ResourceElement
89 {
90 friend class ResourceChild;
91 friend class ResourceDirectory;
92 template<typename T> friend struct fixNumberOfEntries;
93
94 private:
95 /// The resource data.
96 std::vector<byte> m_data;
97 /// PeLib equivalent of the Win32 structure IMAGE_RESOURCE_DATA_ENTRY
98 PELIB_IMAGE_RESOURCE_DATA_ENTRY entry;
99
100 protected:
101 int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/);
102 /// Writes the next resource leaf into the OutputBuffer.
103 void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const;
104
105 public:
106 /// Indicates if the resource element is a leaf or a node.
107 bool isLeaf() const; // EXPORT
108 /// Corrects erroneous valeus in the ResourceLeaf.
109 void makeValid(); // EXPORT
110 /// Reads the next resource leaf from the InputBuffer.
111 /// Returns the size of a resource lead.
112// unsigned int size() const;
113
114 /// Returns the resource data of this resource leaf.
115 std::vector<byte> getData() const; // EXPORT
116 /// Sets the resource data of this resource leaf.
117 void setData(const std::vector<byte>& vData); // EXPORT
118
119 /// Returns the OffsetToData value of this resource leaf.
120 dword getOffsetToData() const; // EXPORT
121 /// Returns the Size value of this resource leaf.
122 dword getSize() const; // EXPORT
123 /// Returns the CodePage value of this resource leaf.
124 dword getCodePage() const; // EXPORT
125 /// Returns the Reserved value of this resource leaf.
126 dword getReserved() const; // EXPORT
127
128 /// Sets the OffsetToData value of this resource leaf.
129 void setOffsetToData(dword dwValue); // EXPORT
130 /// Sets the Size value of this resource leaf.
131 void setSize(dword dwValue); // EXPORT
132 /// Sets the CodePage value of this resource leaf.
133 void setCodePage(dword dwValue); // EXPORT
134 /// Sets the Reserved value of this resource leaf.
135 void setReserved(dword dwValue); // EXPORT
136 };
137
138 /// ResourceNodes represent the nodes in the resource tree.
139 class ResourceNode : public ResourceElement
140 {
141 friend class ResourceChild;
142 friend class ResourceDirectory;
143 template<typename T> friend struct fixNumberOfEntries;
144
145 /// The node's children.
146 std::vector<ResourceChild> children;
147 /// The node's header. Equivalent to IMAGE_RESOURCE_DIRECTORY from the Win32 API.
148 PELIB_IMAGE_RESOURCE_DIRECTORY header;
149
150 protected:
151 /// Reads the next resource node.
152 int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/);
153 /// Writes the next resource node into the OutputBuffer.
154 void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const;
155
156 public:
157 /// Indicates if the resource element is a leaf or a node.
158 bool isLeaf() const; // EXPORT
159 /// Corrects erroneous valeus in the ResourceNode.
160 void makeValid(); // EXPORT
161
162 /// Returns the node's number of children.
163 unsigned int getNumberOfChildren() const; // EXPORT
164 /// Adds another child to node.
165 void addChild(); // EXPORT
166 /// Returns a node's child.
167 ResourceElement* getChild(unsigned int uiIndex); // EXPORT
168 /// Removes a node's child.
169 void removeChild(unsigned int uiIndex); // EXPORT
170
171 /// Returns the name of one of the node's children.
172 std::string getChildName(unsigned int uiIndex) const; // EXPORT
173 /// Returns the Name value of one of the node's children.
174 dword getOffsetToChildName(unsigned int uiIndex) const; // EXPORT
175 /// Returns the OffsetToData value of one of the node's children.
176 dword getOffsetToChildData(unsigned int uiIndex) const; // EXPORT
177
178 /// Sets the name of one of the node's children.
179 void setChildName(unsigned int uiIndex, const std::string& strNewName); // EXPORT
180 /// Sets the Name value of one of the node's children.
181 void setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset); // EXPORT
182 /// Sets the OffsetToData value of one of the node's children.
183 void setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset); // EXPORT
184
185 /// Returns the node's Characteristics value.
186 dword getCharacteristics() const; // EXPORT
187 /// Returns the node's TimeDateStamp value.
188 dword getTimeDateStamp() const; // EXPORT
189 /// Returns the node's MajorVersion value.
190 word getMajorVersion() const; // EXPORT
191 /// Returns the node's MinorVersion value.
192 word getMinorVersion() const; // EXPORT
193 /// Returns the node's NumberOfNamedEntries value.
194 word getNumberOfNamedEntries() const; // EXPORT
195 /// Returns the node's NumberOfIdEntries value.
196 word getNumberOfIdEntries() const; // EXPORT
197
198 /// Sets the node's Characteristics value.
199 void setCharacteristics(dword value); // EXPORT
200 /// Sets the node's TimeDateStamp value.
201 void setTimeDateStamp(dword value); // EXPORT
202 /// Sets the node's MajorVersion value.
203 void setMajorVersion(word value); // EXPORT
204 /// Sets the node's MinorVersion value.
205 void setMinorVersion(word value); // EXPORT
206 /// Sets the node's NumberOfNamedEntries value.
207 void setNumberOfNamedEntries(word value); // EXPORT
208 /// Sets the node's NumberOfIdEntries value.
209 void setNumberOfIdEntries(word value); // EXPORT
210
211 /// Returns the size of a resource node.
212// unsigned int size() const;
213 };
214
215 /// Auxiliary functor which is used to search through the resource tree.
216 /**
217 * Traits class for the template functions of ResourceDirectory.
218 * It's used to find out which function to use when searching for resource nodes or resource leafs
219 * in a node's children vector.
220 **/
221 template<typename T>
222 struct ResComparer
223 {
224 /// Pointer to a member function of ResourceChild
225 typedef bool(ResourceChild::*CompFunc)(T) const;
226
227 /// Return 0 for all unspecialized versions of ResComparer.
228 static CompFunc comp();
229 };
230
231 /// Auxiliary functor which is used to search through the resource tree.
232 /**
233 * ResComparer<dword> is used when a resource element is searched for by ID.
234 **/
235 template<>
236 struct ResComparer<dword>
237 {
238 /// Pointer to a member function of ResourceChild
239 typedef bool(ResourceChild::*CompFunc)(dword) const;
240
241 /// Return the address of the ResourceChild member function that compares the ids of resource elements.
242 static CompFunc comp()
243 {
244 return &ResourceChild::equalId;
245 }
246 };
247
248 /// Auxiliary functor which is used to search through the resource tree.
249 /**
250 * This specializd version of ResComparer is used when a resource element is searched for by name.
251 **/
252 template<>
253 struct ResComparer<std::string>
254 {
255 /// Pointer to a member function of ResourceChild
256 typedef bool(ResourceChild::*CompFunc)(std::string) const;
257
258 /// Return the address of the ResourceChild member function that compares the names of resource elements.
259 static CompFunc comp()
260 {
261 return &ResourceChild::equalName;
262 }
263 };
264
265 /// Unspecialized function that's used as base template for the specialized versions below.
266 template<typename T>
267 struct fixNumberOfEntries
268 {
269 /// Fixes a resource node's header.
270 static void fix(ResourceNode*);
271 };
272
273 /// Fixes NumberOfIdEntries value of a node.
274 template<>
275 struct fixNumberOfEntries<dword>
276 {
277 /// Fixes a resource node's NumberOfIdEntries value.
278 static void fix(ResourceNode* node)
279 {
280 node->header.NumberOfIdEntries = (unsigned int)node->children.size() - std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource));
281 }
282 };
283
284 /// Fixes NumberOfNamedEntries value of a node.
285 template<>
286 struct fixNumberOfEntries<std::string>
287 {
288 /// Fixes a resource node's NumberOfNamedEntries value.
289 static void fix(ResourceNode* node)
290 {
291 node->header.NumberOfNamedEntries = static_cast<PeLib::word>(std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)));
292 }
293 };
294
295 /// Class that represents the resource directory of a PE file.
296 /**
297 * The class ResourceDirectory represents the resource directory of a PE file. This class is fundamentally
298 * different from the other classes of the PeLib library due to the structure of the ResourceDirectory.
299 * For once, it's possible to manipulate the ResourceDirectory through a set of "high level" functions and
300 * and through a set of "low level" functions. The "high level" functions are the functions inside the
301 * ResourceDirectory class with the exception of getRoot.<br><br>
302 * getRoot on the other hand is the first "low level" function. Use it to retrieve the root node of the
303 * resource tree. Then you can traverse through the tree and manipulate individual nodes and leafs
304 * directly using the functions provided by the classes ResourceNode and ResourceLeaf.<br><br>
305 * There's another difference between the ResourceDirectory class and the other PeLib classes, which is
306 * once again caused by the special structure of the PE resource directory. The nodes of the resource
307 * tree must be in a certain order. Manipulating the resource tree does not directly sort the nodes
308 * correctly as this would cause more trouble than it fixes. That means it's your responsibility to
309 * fix the resource tree after manipulating it. PeLib makes the job easy for you, just call the
310 * ResourceDirectory::makeValid function.<br><br>
311 * You might also wonder why there's no size() function in this class. I did not forget it. It's just
312 * that it's impossible to calculate the size of the resource directory without rebuilding it. So why
313 * should PeLib do this if you can do it just as easily by calling rebuild() and then checking the length
314 * of the returned vector.<br><br>
315 * There are also different ways to serialize (rebuild) the resource tree as it's not a fixed structure
316 * that can easily be minimized like most other PE directories.<br><br>
317 * This means it's entirely possible that the resource tree you read from a file differs from the one
318 * PeLib creates. This might cause a minor issue. The original resource tree might be smaller (due to
319 * different padding) so it's crucial that you check if there's enough space in the original resource
320 * directory before you write the rebuilt resource directory back to the file.
321 **/
322 class ResourceDirectory
323 {
324 private:
325 /// The root node of the resource directory.
326 ResourceNode m_rnRoot;
327
328 // Prepare for some crazy syntax below to make Digital Mars happy.
329
330 /// Retrieves an iterator to a specified resource child.
331 template<typename S, typename T>
332 std::vector<ResourceChild>::const_iterator locateResourceT(S restypeid, T resid) const;
333
334 /// Retrieves an iterator to a specified resource child.
335 template<typename S, typename T>
336 std::vector<ResourceChild>::iterator locateResourceT(S restypeid, T resid);
337
338 /// Adds a new resource.
339 template<typename S, typename T>
340 int addResourceT(S restypeid, T resid, ResourceChild& rc);
341
342 /// Removes new resource.
343 template<typename S, typename T>
344 int removeResourceT(S restypeid, T resid);
345
346 /// Returns the data of a resource.
347 template<typename S, typename T>
348 int getResourceDataT(S restypeid, T resid, std::vector<byte>& data) const;
349
350 /// Sets the data of a resource.
351 template<typename S, typename T>
352 int setResourceDataT(S restypeid, T resid, std::vector<byte>& data);
353
354 /// Returns the ID of a resource.
355 template<typename S, typename T>
356 dword getResourceIdT(S restypeid, T resid) const;
357
358 /// Sets the ID of a resource.
359 template<typename S, typename T>
360 int setResourceIdT(S restypeid, T resid, dword dwNewResId);
361
362 /// Returns the name of a resource.
363 template<typename S, typename T>
364 std::string getResourceNameT(S restypeid, T resid) const;
365
366 /// Sets the name of a resource.
367 template<typename S, typename T>
368 int setResourceNameT(S restypeid, T resid, std::string strNewResName);
369
370 public:
371 ResourceNode* getRoot();
372 /// Corrects a erroneous resource directory.
373 void makeValid();
374 /// Reads the resource directory from a file.
375 int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva);
376 /// Rebuilds the resource directory.
377 void rebuild(std::vector<byte>& vBuffer, unsigned int uiRva) const;
378 /// Returns the size of the rebuilt resource directory.
379// unsigned int size() const;
380 /// Writes the resource directory to a file.
381 int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const;
382
383 /// Adds a new resource type.
384 int addResourceType(dword dwResTypeId);
385 /// Adds a new resource type.
386 int addResourceType(const std::string& strResTypeName);
387
388 /// Removes a resource type and all of it's resources.
389 int removeResourceType(dword dwResTypeId);
390 /// Removes a resource type and all of it's resources.
391 int removeResourceType(const std::string& strResTypeName);
392
393 /// Removes a resource type and all of it's resources.
394 int removeResourceTypeByIndex(unsigned int uiIndex);
395
396 /// Adds a new resource.
397 int addResource(dword dwResTypeId, dword dwResId);
398 /// Adds a new resource.
399 int addResource(dword dwResTypeId, const std::string& strResName);
400 /// Adds a new resource.
401 int addResource(const std::string& strResTypeName, dword dwResId);
402 /// Adds a new resource.
403 int addResource(const std::string& strResTypeName, const std::string& strResName);
404
405 /// Removes a resource.
406 int removeResource(dword dwResTypeId, dword dwResId);
407 /// Removes a resource.
408 int removeResource(dword dwResTypeId, const std::string& strResName);
409 /// Removes a resource.
410 int removeResource(const std::string& strResTypeName, dword dwResId);
411 /// Removes a resource.
412 int removeResource(const std::string& strResTypeName, const std::string& strResName);
413
414 /// Returns the number of resource types.
415 unsigned int getNumberOfResourceTypes() const;
416
417 /// Returns the ID of a resource type.
418 dword getResourceTypeIdByIndex(unsigned int uiIndex) const;
419 /// Returns the name of a resource type.
420 std::string getResourceTypeNameByIndex(unsigned int uiIndex) const;
421
422 /// Converts a resource type ID to an index.
423 int resourceTypeIdToIndex(dword dwResTypeId) const;
424 /// Converts a resource type name to an index.
425 int resourceTypeNameToIndex(const std::string& strResTypeName) const;
426
427 /// Returns the number of resources of a certain resource type.
428 unsigned int getNumberOfResources(dword dwId) const;
429 /// Returns the number of resources of a certain resource type.
430 unsigned int getNumberOfResources(const std::string& strResTypeName) const;
431
432 /// Returns the number of resources of a certain resource type.
433 unsigned int getNumberOfResourcesByIndex(unsigned int uiIndex) const;
434
435 /// Returns the data of a certain resource.
436 void getResourceData(dword dwResTypeId, dword dwResId, std::vector<byte>& data) const;
437 /// Returns the data of a certain resource.
438 void getResourceData(dword dwResTypeId, const std::string& strResName, std::vector<byte>& data) const;
439 /// Returns the data of a certain resource.
440 void getResourceData(const std::string& strResTypeName, dword dwResId, std::vector<byte>& data) const;
441 /// Returns the data of a certain resource.
442 void getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<byte>& data) const;
443
444 /// Returns the data of a certain resource.
445 void getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<byte>& data) const;
446
447 /// Sets the data of a certain resource.
448 void setResourceData(dword dwResTypeId, dword dwResId, std::vector<byte>& data);
449 /// Sets the data of a certain resource.
450 void setResourceData(dword dwResTypeId, const std::string& strResName, std::vector<byte>& data);
451 /// Sets the data of a certain resource.
452 void setResourceData(const std::string& strResTypeName, dword dwResId, std::vector<byte>& data);
453 /// Sets the data of a certain resource.
454 void setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<byte>& data);
455
456 /// Sets the data of a certain resource.
457 void setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<byte>& data);
458
459 /// Returns the ID of a certain resource.
460 dword getResourceId(dword dwResTypeId, const std::string& strResName) const;
461 /// Returns the ID of a certain resource.
462 dword getResourceId(const std::string& strResTypeName, const std::string& strResName) const;
463
464 /// Returns the ID of a certain resource.
465 dword getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const;
466
467 /// Sets the ID of a certain resource.
468 void setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId);
469 /// Sets the ID of a certain resource.
470 void setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId);
471 /// Sets the ID of a certain resource.
472 void setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId);
473 /// Sets the ID of a certain resource.
474 void setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId);
475
476 /// Sets the ID of a certain resource.
477 void setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId);
478
479 /// Returns the name of a certain resource.
480 std::string getResourceName(dword dwResTypeId, dword dwResId) const;
481 /// Returns the name of a certain resource.
482 std::string getResourceName(const std::string& strResTypeName, dword dwResId) const;
483
484 /// Returns the name of a certain resource.
485 std::string getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const;
486
487 /// Sets the name of a certain resource.
488 void setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName);
489 /// Sets the name of a certain resource.
490 void setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName);
491 /// Sets the name of a certain resource.
492 void setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName);
493 /// Sets the name of a certain resource.
494 void setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName);
495
496 /// Sets the name of a certain resource.
497 void setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName);
498 };
499
500 /**
501 * Looks through the entire resource tree and returns a const_iterator to the resource specified
502 * by the parameters.
503 * @param restypeid Identifier of the resource type (either ID or name).
504 * @param resid Identifier of the resource (either ID or name).
505 * @return A const_iterator to the specified resource.
506 **/
507 template<typename S, typename T>
508 std::vector<ResourceChild>::const_iterator ResourceDirectory::locateResourceT(S restypeid, T resid) const
509 {
510 typedef bool(ResourceChild::*CompFunc1)(S) const;
511 typedef bool(ResourceChild::*CompFunc2)(T) const;
512
513 CompFunc1 comp1 = ResComparer<S>::comp();
514 CompFunc2 comp2 = ResComparer<T>::comp();
515
516 std::vector<ResourceChild>::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid));
517 if (Iter == m_rnRoot.children.end())
518 {
519 return Iter;
520 }
521
522 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
523 std::vector<ResourceChild>::const_iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid));
524 if (ResIter == currNode->children.end())
525 {
526 return ResIter;
527 }
528
529 return ResIter;
530 }
531
532 /**
533 * Looks through the entire resource tree and returns an iterator to the resource specified
534 * by the parameters.
535 * @param restypeid Identifier of the resource type (either ID or name).
536 * @param resid Identifier of the resource (either ID or name).
537 * @return An iterator to the specified resource.
538 **/
539 template<typename S, typename T>
540 std::vector<ResourceChild>::iterator ResourceDirectory::locateResourceT(S restypeid, T resid)
541 {
542 typedef bool(ResourceChild::*CompFunc1)(S) const;
543 typedef bool(ResourceChild::*CompFunc2)(T) const;
544
545 CompFunc1 comp1 = ResComparer<S>::comp();
546 CompFunc2 comp2 = ResComparer<T>::comp();
547
548 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid));
549 if (Iter == m_rnRoot.children.end())
550 {
551 return Iter;
552 }
553
554 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
555 std::vector<ResourceChild>::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid));
556 if (ResIter == currNode->children.end())
557 {
558 return ResIter;
559 }
560
561 return ResIter;
562 }
563
564 /**
565 * Adds a new resource, resource type and ID are specified by the parameters.
566 * @param restypeid Identifier of the resource type (either ID or name).
567 * @param resid Identifier of the resource (either ID or name).
568 * @param rc ResourceChild that will be added.
569 **/
570 template<typename S, typename T>
571 int ResourceDirectory::addResourceT(S restypeid, T resid, ResourceChild& rc)
572 {
573 typedef bool(ResourceChild::*CompFunc1)(S) const;
574 typedef bool(ResourceChild::*CompFunc2)(T) const;
575
576 CompFunc1 comp1 = ResComparer<S>::comp();
577 CompFunc2 comp2 = ResComparer<T>::comp();
578
579 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid));
580 if (Iter == m_rnRoot.children.end())
581 {
582 return 1;
583 // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
584 }
585
586 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
587 std::vector<ResourceChild>::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid));
588 if (ResIter != currNode->children.end())
589 {
590 return 1;
591// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__);
592 }
593
594 rc.child = new ResourceNode;
595 ResourceChild rlnew;
596 rlnew.child = new ResourceLeaf;
597 ResourceNode* currNode2 = static_cast<ResourceNode*>(rc.child);
598 currNode2->children.push_back(rlnew);
599 currNode->children.push_back(rc);
600
601 fixNumberOfEntries<T>::fix(currNode);
602 fixNumberOfEntries<T>::fix(currNode2);
603
604 return 0;
605 }
606
607 /**
608 * Removes a resource, resource type and ID are specified by the parameters.
609 * @param restypeid Identifier of the resource type (either ID or name).
610 * @param resid Identifier of the resource (either ID or name).
611 **/
612 template<typename S, typename T>
613 int ResourceDirectory::removeResourceT(S restypeid, T resid)
614 {
615 typedef bool(ResourceChild::*CompFunc1)(S) const;
616 typedef bool(ResourceChild::*CompFunc2)(T) const;
617
618 CompFunc1 comp1 = ResComparer<S>::comp();
619 CompFunc2 comp2 = ResComparer<T>::comp();
620
621 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid));
622 if (Iter == m_rnRoot.children.end())
623 {
624 return 1;
625 //throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
626 }
627
628 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
629 std::vector<ResourceChild>::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid));
630 if (ResIter == currNode->children.end())
631 {
632 return 1;
633 // throw Exceptions::InvalidName(ResourceDirectoryId, __LINE__);
634 }
635
636 currNode->children.erase(ResIter);
637
638 fixNumberOfEntries<T>::fix(currNode);
639
640 return 0;
641 }
642
643 /**
644 * Returns the data of a resource, resource type and ID are specified by the parameters.
645 * @param restypeid Identifier of the resource type (either ID or name).
646 * @param resid Identifier of the resource (either ID or name).
647 * @param data The data of the resource will be written into this vector.
648 **/
649 template<typename S, typename T>
650 int ResourceDirectory::getResourceDataT(S restypeid, T resid, std::vector<byte>& data) const
651 {
652 std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
653 ResourceNode* currNode = static_cast<ResourceNode*>(ResIter->child);
654 ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
655 data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end());
656
657 return 0;
658 }
659
660 /**
661 * Sets the data of a resource, resource type and ID are specified by the parameters.
662 * @param restypeid Identifier of the resource type (either ID or name).
663 * @param resid Identifier of the resource (either ID or name).
664 * @param data The new data of the resource is taken from this vector.
665 **/
666 template<typename S, typename T>
667 int ResourceDirectory::setResourceDataT(S restypeid, T resid, std::vector<byte>& data)
668 {
669 std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
670 ResourceNode* currNode = static_cast<ResourceNode*>(ResIter->child);
671 ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
672 currLeaf->m_data.assign(data.begin(), data.end());
673
674 return 0;
675 }
676
677 /**
678 * Returns the id of a resource, resource type and ID are specified by the parameters.
679 * Note: Calling this function with resid == the ID of the resource makes no sense at all.
680 * @param restypeid Identifier of the resource type (either ID or name).
681 * @param resid Identifier of the resource (either ID or name).
682 * @return The ID of the specified resource.
683 **/
684 template<typename S, typename T>
685 dword ResourceDirectory::getResourceIdT(S restypeid, T resid) const
686 {
687 std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
688 return ResIter->entry.irde.Name;
689 }
690
691 /**
692 * Sets the id of a resource, resource type and ID are specified by the parameters.
693 * @param restypeid Identifier of the resource type (either ID or name).
694 * @param resid Identifier of the resource (either ID or name).
695 * @param dwNewResId New ID of the resource.
696 **/
697 template<typename S, typename T>
698 int ResourceDirectory::setResourceIdT(S restypeid, T resid, dword dwNewResId)
699 {
700 std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
701 ResIter->entry.irde.Name = dwNewResId;
702 return 0;
703 }
704
705 /**
706 * Returns the name of a resource, resource type and ID are specified by the parameters.
707 * Note: Calling this function with resid == the name of the resource makes no sense at all.
708 * @param restypeid Identifier of the resource type (either ID or name).
709 * @param resid Identifier of the resource (either ID or name).
710 * @return The name of the specified resource.
711 **/
712 template<typename S, typename T>
713 std::string ResourceDirectory::getResourceNameT(S restypeid, T resid) const
714 {
715 std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
716 return ResIter->entry.wstrName;
717 }
718
719 /**
720 * Sets the name of a resource, resource type and ID are specified by the parameters.
721 * @param restypeid Identifier of the resource type (either ID or name).
722 * @param resid Identifier of the resource (either ID or name).
723 * @param strNewResName The new name of the resource.
724 **/
725 template<typename S, typename T>
726 int ResourceDirectory::setResourceNameT(S restypeid, T resid, std::string strNewResName)
727 {
728 std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
729 ResIter->entry.wstrName = strNewResName;
730
731 return 0;
732 }
733}
734
735#endif