ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
resource.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program 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 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef PRINCE_RESOURCE_H
23 #define PRINCE_RESOURCE_H
24 
25 #include "common/stream.h"
26 #include "common/memstream.h"
27 #include "common/archive.h"
28 #include "common/debug-channels.h"
29 #include "common/ptr.h"
30 
31 #include "prince/decompress.h"
32 
33 namespace Prince {
34 
35 namespace Resource {
36 
37 Common::SeekableReadStream *getDecompressedStream(Common::SeekableReadStream *stream);
38 
39 template <typename T>
40 bool loadFromStream(T &resource, Common::SeekableReadStream &stream) {
41  return resource.loadStream(stream);
42 }
43 
44 template<typename T>
45 bool loadResource(T *resource, const char *resourceName, bool required) {
46  Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
47  if (!stream_) {
48  if (required)
49  error("Can't load %s", resourceName);
50  return false;
51  }
52 
53  Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
54 
55  return loadFromStream(*resource, *stream);
56 }
57 
58 template <typename T>
59 bool loadResource(Common::Array<T> &array, Common::SeekableReadStream &stream, bool required = true) {
60  T t;
61  while (t.loadFromStream(stream))
62  array.push_back(t);
63 
64  return true;
65 }
66 
67 
68 template <typename T>
69 bool loadResource(Common::Array<T> &array, const char *resourceName, bool required = true) {
70  Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
71  if (!stream_) {
72  if (required)
73  error("Can't load %s", resourceName);
74  return false;
75  }
76 
77  Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
78 
79  return loadResource(array, *stream, required);
80 }
81 
82 template <typename T>
83 bool loadResource(Common::Array<T *> &array, const char *resourceName, bool required = true) {
84 
85  Common::SeekableReadStream *stream_(SearchMan.createReadStreamForMember(resourceName));
86  if (!stream_) {
87  if (required)
88  error("Can't load %s", resourceName);
89  return false;
90  }
91 
92  Common::ScopedPtr<Common::SeekableReadStream> stream(getDecompressedStream(stream_));
93 
94  // FIXME: This is stupid. Maybe loadFromStream should be helper method that returns initialized object
95  while (true) {
96  T* t = new T();
97  if (!t->loadFromStream(*stream)) {
98  delete t;
99  break;
100  }
101  array.push_back(t);
102  }
103  return true;
104 }
105 
106 }
107 
108 } // End of namespace Prince
109 
110 #endif
Definition: array.h:52
Definition: stream.h:745
void push_back(const T &element)
Definition: array.h:180
Definition: animation.h:30
#define SearchMan
Definition: archive.h:499
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1