1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #ifndef __DataStream_H__ |
---|
30 | #define __DataStream_H__ |
---|
31 | |
---|
32 | #include "OgrePrerequisites.h" |
---|
33 | #include "OgreString.h" |
---|
34 | #include "OgreSharedPtr.h" |
---|
35 | #include <istream> |
---|
36 | |
---|
37 | namespace Ogre { |
---|
38 | |
---|
39 | /** General purpose class used for encapsulating the reading of data. |
---|
40 | @remarks |
---|
41 | This class performs basically the same tasks as std::basic_istream, |
---|
42 | except that it does not have any formatting capabilities, and is |
---|
43 | designed to be subclassed to receive data from multiple sources, |
---|
44 | including libraries which have no compatiblity with the STL's |
---|
45 | stream interfaces. As such, this is an abstraction of a set of |
---|
46 | wrapper classes which pretend to be standard stream classes but |
---|
47 | can actually be implemented quite differently. |
---|
48 | @par |
---|
49 | Generally, if a plugin or application provides an ArchiveFactory, |
---|
50 | it should also provide a DataStream subclass which will be used |
---|
51 | to stream data out of that Archive implementation, unless it can |
---|
52 | use one of the common implementations included. |
---|
53 | @note |
---|
54 | Ogre makes no guarantees about thread safety, for performance reasons. |
---|
55 | If you wish to access stream data asynchronously then you should |
---|
56 | organise your own mutexes to avoid race conditions. |
---|
57 | */ |
---|
58 | class _OgreExport DataStream |
---|
59 | { |
---|
60 | protected: |
---|
61 | /// The name (e.g. resource name) that can be used to identify the source fot his data (optional) |
---|
62 | String mName; |
---|
63 | /// Size of the data in the stream (may be 0 if size cannot be determined) |
---|
64 | size_t mSize; |
---|
65 | #define OGRE_STREAM_TEMP_SIZE 128 |
---|
66 | public: |
---|
67 | /// Constructor for creating unnamed streams |
---|
68 | DataStream() : mSize(0) {} |
---|
69 | /// Constructor for creating named streams |
---|
70 | DataStream(const String& name) : mName(name), mSize(0) {} |
---|
71 | /// Returns the name of the stream, if it has one. |
---|
72 | const String& getName(void) { return mName; } |
---|
73 | virtual ~DataStream() {} |
---|
74 | // Streaming operators |
---|
75 | template<typename T> DataStream& operator>>(T& val); |
---|
76 | /** Read the requisite number of bytes from the stream, |
---|
77 | stopping at the end of the file. |
---|
78 | @param buf Reference to a buffer pointer |
---|
79 | @param count Number of bytes to read |
---|
80 | @returns The number of bytes read |
---|
81 | */ |
---|
82 | virtual size_t read(void* buf, size_t count) = 0; |
---|
83 | /** Get a single line from the stream. |
---|
84 | @remarks |
---|
85 | The delimiter character is not included in the data |
---|
86 | returned, and it is skipped over so the next read will occur |
---|
87 | after it. The buffer contents will include a |
---|
88 | terminating character. |
---|
89 | @note |
---|
90 | If you used this function, you <b>must</b> open the stream in <b>binary mode</b>, |
---|
91 | otherwise, it'll produce unexpected results. |
---|
92 | @param buf Reference to a buffer pointer |
---|
93 | @param maxCount The maximum length of data to be read, excluding the terminating character |
---|
94 | @param delim The delimiter to stop at |
---|
95 | @returns The number of bytes read, excluding the terminating character |
---|
96 | */ |
---|
97 | virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n"); |
---|
98 | |
---|
99 | /** Returns a String containing the next line of data, optionally |
---|
100 | trimmed for whitespace. |
---|
101 | @remarks |
---|
102 | This is a convenience method for text streams only, allowing you to |
---|
103 | retrieve a String object containing the next line of data. The data |
---|
104 | is read up to the next newline character and the result trimmed if |
---|
105 | required. |
---|
106 | @note |
---|
107 | If you used this function, you <b>must</b> open the stream in <b>binary mode</b>, |
---|
108 | otherwise, it'll produce unexpected results. |
---|
109 | @param |
---|
110 | trimAfter If true, the line is trimmed for whitespace (as in |
---|
111 | String.trim(true,true)) |
---|
112 | */ |
---|
113 | virtual String getLine( bool trimAfter = true ); |
---|
114 | |
---|
115 | /** Returns a String containing the entire stream. |
---|
116 | @remarks |
---|
117 | This is a convenience method for text streams only, allowing you to |
---|
118 | retrieve a String object containing all the data in the stream. |
---|
119 | */ |
---|
120 | virtual String getAsString(void); |
---|
121 | |
---|
122 | /** Skip a single line from the stream. |
---|
123 | @note |
---|
124 | If you used this function, you <b>must</b> open the stream in <b>binary mode</b>, |
---|
125 | otherwise, it'll produce unexpected results. |
---|
126 | @param delim The delimiter(s) to stop at |
---|
127 | @returns The number of bytes skipped |
---|
128 | */ |
---|
129 | virtual size_t skipLine(const String& delim = "\n"); |
---|
130 | |
---|
131 | /** Skip a defined number of bytes. This can also be a negative value, in which case |
---|
132 | the file pointer rewinds a defined number of bytes. */ |
---|
133 | virtual void skip(long count) = 0; |
---|
134 | |
---|
135 | /** Repositions the read point to a specified byte. |
---|
136 | */ |
---|
137 | virtual void seek( size_t pos ) = 0; |
---|
138 | |
---|
139 | /** Returns the current byte offset from beginning */ |
---|
140 | virtual size_t tell(void) const = 0; |
---|
141 | |
---|
142 | /** Returns true if the stream has reached the end. |
---|
143 | */ |
---|
144 | virtual bool eof(void) const = 0; |
---|
145 | |
---|
146 | /** Returns the total size of the data to be read from the stream, |
---|
147 | or 0 if this is indeterminate for this stream. |
---|
148 | */ |
---|
149 | size_t size(void) const { return mSize; } |
---|
150 | |
---|
151 | /** Close the stream; this makes further operations invalid. */ |
---|
152 | virtual void close(void) = 0; |
---|
153 | |
---|
154 | |
---|
155 | }; |
---|
156 | |
---|
157 | /** Shared pointer to allow data streams to be passed around without |
---|
158 | worrying about deallocation |
---|
159 | */ |
---|
160 | typedef SharedPtr<DataStream> DataStreamPtr; |
---|
161 | |
---|
162 | /// List of DataStream items |
---|
163 | typedef std::list<DataStreamPtr> DataStreamList; |
---|
164 | /// Shared pointer to list of DataStream items |
---|
165 | typedef SharedPtr<DataStreamList> DataStreamListPtr; |
---|
166 | |
---|
167 | /** Common subclass of DataStream for handling data from chunks of memory. |
---|
168 | */ |
---|
169 | class _OgreExport MemoryDataStream : public DataStream |
---|
170 | { |
---|
171 | protected: |
---|
172 | /// Pointer to the start of the data area |
---|
173 | uchar* mData; |
---|
174 | /// Pointer to the current position in the memory |
---|
175 | uchar* mPos; |
---|
176 | /// Pointer to the end of the memory |
---|
177 | uchar* mEnd; |
---|
178 | /// Do we delete the memory on close |
---|
179 | bool mFreeOnClose; |
---|
180 | public: |
---|
181 | |
---|
182 | /** Wrap an existing memory chunk in a stream. |
---|
183 | @param pMem Pointer to the exising memory |
---|
184 | @param size The size of the memory chunk in bytes |
---|
185 | @param freeOnClose If true, the memory associated will be destroyed |
---|
186 | when the stream is destroyed. |
---|
187 | */ |
---|
188 | MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false); |
---|
189 | |
---|
190 | /** Wrap an existing memory chunk in a named stream. |
---|
191 | @param name The name to give the stream |
---|
192 | @param pMem Pointer to the exising memory |
---|
193 | @param size The size of the memory chunk in bytes |
---|
194 | @param freeOnClose If true, the memory associated will be destroyed |
---|
195 | when the stream is destroyed. |
---|
196 | */ |
---|
197 | MemoryDataStream(const String& name, void* pMem, size_t size, |
---|
198 | bool freeOnClose = false); |
---|
199 | |
---|
200 | /** Create a stream which pre-buffers the contents of another stream. |
---|
201 | @remarks |
---|
202 | This constructor can be used to intentionally read in the entire |
---|
203 | contents of another stream, copying them to the internal buffer |
---|
204 | and thus making them available in memory as a single unit. |
---|
205 | @param sourceStream Another DataStream which will provide the source |
---|
206 | of data |
---|
207 | @param freeOnClose If true, the memory associated will be destroyed |
---|
208 | when the stream is destroyed. |
---|
209 | */ |
---|
210 | MemoryDataStream(DataStream& sourceStream, |
---|
211 | bool freeOnClose = true); |
---|
212 | |
---|
213 | /** Create a stream which pre-buffers the contents of another stream. |
---|
214 | @remarks |
---|
215 | This constructor can be used to intentionally read in the entire |
---|
216 | contents of another stream, copying them to the internal buffer |
---|
217 | and thus making them available in memory as a single unit. |
---|
218 | @param sourceStream Weak reference to another DataStream which will provide the source |
---|
219 | of data |
---|
220 | @param freeOnClose If true, the memory associated will be destroyed |
---|
221 | when the stream is destroyed. |
---|
222 | */ |
---|
223 | MemoryDataStream(DataStreamPtr& sourceStream, |
---|
224 | bool freeOnClose = true); |
---|
225 | |
---|
226 | /** Create a named stream which pre-buffers the contents of |
---|
227 | another stream. |
---|
228 | @remarks |
---|
229 | This constructor can be used to intentionally read in the entire |
---|
230 | contents of another stream, copying them to the internal buffer |
---|
231 | and thus making them available in memory as a single unit. |
---|
232 | @param name The name to give the stream |
---|
233 | @param sourceStream Another DataStream which will provide the source |
---|
234 | of data |
---|
235 | @param freeOnClose If true, the memory associated will be destroyed |
---|
236 | when the stream is destroyed. |
---|
237 | */ |
---|
238 | MemoryDataStream(const String& name, DataStream& sourceStream, |
---|
239 | bool freeOnClose = true); |
---|
240 | |
---|
241 | /** Create a named stream which pre-buffers the contents of |
---|
242 | another stream. |
---|
243 | @remarks |
---|
244 | This constructor can be used to intentionally read in the entire |
---|
245 | contents of another stream, copying them to the internal buffer |
---|
246 | and thus making them available in memory as a single unit. |
---|
247 | @param name The name to give the stream |
---|
248 | @param sourceStream Another DataStream which will provide the source |
---|
249 | of data |
---|
250 | @param freeOnClose If true, the memory associated will be destroyed |
---|
251 | when the stream is destroyed. |
---|
252 | */ |
---|
253 | MemoryDataStream(const String& name, const DataStreamPtr& sourceStream, |
---|
254 | bool freeOnClose = true); |
---|
255 | |
---|
256 | /** Create a stream with a brand new empty memory chunk. |
---|
257 | @param size The size of the memory chunk to create in bytes |
---|
258 | @param freeOnClose If true, the memory associated will be destroyed |
---|
259 | when the stream is destroyed. |
---|
260 | */ |
---|
261 | MemoryDataStream(size_t size, bool freeOnClose = true); |
---|
262 | /** Create a named stream with a brand new empty memory chunk. |
---|
263 | @param name The name to give the stream |
---|
264 | @param size The size of the memory chunk to create in bytes |
---|
265 | @param freeOnClose If true, the memory associated will be destroyed |
---|
266 | when the stream is destroyed. |
---|
267 | */ |
---|
268 | MemoryDataStream(const String& name, size_t size, |
---|
269 | bool freeOnClose = true); |
---|
270 | |
---|
271 | ~MemoryDataStream(); |
---|
272 | |
---|
273 | /** Get a pointer to the start of the memory block this stream holds. */ |
---|
274 | uchar* getPtr(void) { return mData; } |
---|
275 | |
---|
276 | /** Get a pointer to the current position in the memory block this stream holds. */ |
---|
277 | uchar* getCurrentPtr(void) { return mPos; } |
---|
278 | |
---|
279 | /** @copydoc DataStream::read |
---|
280 | */ |
---|
281 | size_t read(void* buf, size_t count); |
---|
282 | /** @copydoc DataStream::readLine |
---|
283 | */ |
---|
284 | size_t readLine(char* buf, size_t maxCount, const String& delim = "\n"); |
---|
285 | |
---|
286 | /** @copydoc DataStream::skipLine |
---|
287 | */ |
---|
288 | size_t skipLine(const String& delim = "\n"); |
---|
289 | |
---|
290 | /** @copydoc DataStream::skip |
---|
291 | */ |
---|
292 | void skip(long count); |
---|
293 | |
---|
294 | /** @copydoc DataStream::seek |
---|
295 | */ |
---|
296 | void seek( size_t pos ); |
---|
297 | |
---|
298 | /** @copydoc DataStream::tell |
---|
299 | */ |
---|
300 | size_t tell(void) const; |
---|
301 | |
---|
302 | /** @copydoc DataStream::eof |
---|
303 | */ |
---|
304 | bool eof(void) const; |
---|
305 | |
---|
306 | /** @copydoc DataStream::close |
---|
307 | */ |
---|
308 | void close(void); |
---|
309 | |
---|
310 | /** Sets whether or not to free the encapsulated memory on close. */ |
---|
311 | void setFreeOnClose(bool free) { mFreeOnClose = free; } |
---|
312 | }; |
---|
313 | |
---|
314 | /** Shared pointer to allow memory data streams to be passed around without |
---|
315 | worrying about deallocation |
---|
316 | */ |
---|
317 | typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr; |
---|
318 | |
---|
319 | /** Common subclass of DataStream for handling data from |
---|
320 | std::basic_istream. |
---|
321 | */ |
---|
322 | class _OgreExport FileStreamDataStream : public DataStream |
---|
323 | { |
---|
324 | protected: |
---|
325 | /// Reference to source stream |
---|
326 | std::ifstream* mpStream; |
---|
327 | bool mFreeOnClose; |
---|
328 | public: |
---|
329 | /** Construct stream from an STL stream |
---|
330 | @param s Pointer to source stream |
---|
331 | @param freeOnClose Whether to delete the underlying stream on |
---|
332 | destruction of this class |
---|
333 | */ |
---|
334 | FileStreamDataStream(std::ifstream* s, |
---|
335 | bool freeOnClose = true); |
---|
336 | /** Construct named stream from an STL stream |
---|
337 | @param name The name to give this stream |
---|
338 | @param s Pointer to source stream |
---|
339 | @param freeOnClose Whether to delete the underlying stream on |
---|
340 | destruction of this class |
---|
341 | */ |
---|
342 | FileStreamDataStream(const String& name, |
---|
343 | std::ifstream* s, |
---|
344 | bool freeOnClose = true); |
---|
345 | |
---|
346 | /** Construct named stream from an STL stream, and tell it the size |
---|
347 | @remarks |
---|
348 | This variant tells the class the size of the stream too, which |
---|
349 | means this class does not need to seek to the end of the stream |
---|
350 | to determine the size up-front. This can be beneficial if you have |
---|
351 | metadata about the contents of the stream already. |
---|
352 | @param name The name to give this stream |
---|
353 | @param s Pointer to source stream |
---|
354 | @param size Size of the stream contents in bytes |
---|
355 | @param freeOnClose Whether to delete the underlying stream on |
---|
356 | destruction of this class |
---|
357 | */ |
---|
358 | FileStreamDataStream(const String& name, |
---|
359 | std::ifstream* s, |
---|
360 | size_t size, |
---|
361 | bool freeOnClose = true); |
---|
362 | |
---|
363 | ~FileStreamDataStream(); |
---|
364 | |
---|
365 | /** @copydoc DataStream::read |
---|
366 | */ |
---|
367 | size_t read(void* buf, size_t count); |
---|
368 | /** @copydoc DataStream::readLine |
---|
369 | */ |
---|
370 | size_t readLine(char* buf, size_t maxCount, const String& delim = "\n"); |
---|
371 | |
---|
372 | /** @copydoc DataStream::skip |
---|
373 | */ |
---|
374 | void skip(long count); |
---|
375 | |
---|
376 | /** @copydoc DataStream::seek |
---|
377 | */ |
---|
378 | void seek( size_t pos ); |
---|
379 | |
---|
380 | /** @copydoc DataStream::tell |
---|
381 | */ |
---|
382 | size_t tell(void) const; |
---|
383 | |
---|
384 | /** @copydoc DataStream::eof |
---|
385 | */ |
---|
386 | bool eof(void) const; |
---|
387 | |
---|
388 | /** @copydoc DataStream::close |
---|
389 | */ |
---|
390 | void close(void); |
---|
391 | |
---|
392 | |
---|
393 | }; |
---|
394 | |
---|
395 | /** Common subclass of DataStream for handling data from C-style file |
---|
396 | handles. |
---|
397 | @remarks |
---|
398 | Use of this class is generally discouraged; if you want to wrap file |
---|
399 | access in a DataStream, you should definitely be using the C++ friendly |
---|
400 | FileStreamDataStream. However, since there are quite a few applications |
---|
401 | and libraries still wedded to the old FILE handle access, this stream |
---|
402 | wrapper provides some backwards compatibility. |
---|
403 | */ |
---|
404 | class _OgreExport FileHandleDataStream : public DataStream |
---|
405 | { |
---|
406 | protected: |
---|
407 | FILE* mFileHandle; |
---|
408 | public: |
---|
409 | /// Create stream from a C file handle |
---|
410 | FileHandleDataStream(FILE* handle); |
---|
411 | /// Create named stream from a C file handle |
---|
412 | FileHandleDataStream(const String& name, FILE* handle); |
---|
413 | ~FileHandleDataStream(); |
---|
414 | |
---|
415 | /** @copydoc DataStream::read |
---|
416 | */ |
---|
417 | size_t read(void* buf, size_t count); |
---|
418 | |
---|
419 | /** @copydoc DataStream::skip |
---|
420 | */ |
---|
421 | void skip(long count); |
---|
422 | |
---|
423 | /** @copydoc DataStream::seek |
---|
424 | */ |
---|
425 | void seek( size_t pos ); |
---|
426 | |
---|
427 | /** @copydoc DataStream::tell |
---|
428 | */ |
---|
429 | size_t tell(void) const; |
---|
430 | |
---|
431 | /** @copydoc DataStream::eof |
---|
432 | */ |
---|
433 | bool eof(void) const; |
---|
434 | |
---|
435 | /** @copydoc DataStream::close |
---|
436 | */ |
---|
437 | void close(void); |
---|
438 | |
---|
439 | }; |
---|
440 | } |
---|
441 | #endif |
---|
442 | |
---|