Coverage Report

Created: 2025-07-11 06:36

/src/ogre/OgreMain/include/OgreDataStream.h
Line
Count
Source (jump to first uncovered line)
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-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#ifndef __DataStream_H__
29
#define __DataStream_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreSharedPtr.h"
33
#include <istream>
34
#include "OgreHeaderPrefix.h"
35
36
namespace Ogre {
37
    
38
    /** \addtogroup Core
39
    *  @{
40
    */
41
    /** \addtogroup Resources
42
    *  @{
43
    */
44
45
    /** General purpose class used for encapsulating the reading and writing of data.
46
47
        This class performs basically the same tasks as std::basic_istream, 
48
        except that it does not have any formatting capabilities, and is
49
        designed to be subclassed to receive data from multiple sources,
50
        including libraries which have no compatibility with the STL's
51
        stream interfaces. As such, this is an abstraction of a set of 
52
        wrapper classes which pretend to be standard stream classes but 
53
        can actually be implemented quite differently. 
54
    @par
55
        Generally, if a plugin or application provides an ArchiveFactory, 
56
        it should also provide a DataStream subclass which will be used
57
        to stream data out of that Archive implementation, unless it can 
58
        use one of the common implementations included.
59
    @note
60
        Ogre makes no guarantees about thread safety, for performance reasons.
61
        If you wish to access stream data asynchronously then you should
62
        organise your own mutexes to avoid race conditions. 
63
    */
64
    class _OgreExport DataStream : public StreamAlloc
65
    {
66
    public:
67
        enum AccessMode
68
        {
69
            READ = 1, 
70
            WRITE = 2
71
        };
72
    protected:
73
        /// The name (e.g. resource name) that can be used to identify the source for this data (optional)
74
        String mName;       
75
        /// Size of the data in the stream (may be 0 if size cannot be determined)
76
        size_t mSize;
77
        /// What type of access is allowed (AccessMode)
78
        uint16 mAccess;
79
80
        #define OGRE_STREAM_TEMP_SIZE 128
81
    public:
82
        /// Constructor for creating unnamed streams
83
        DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
84
        /// Constructor for creating named streams
85
        DataStream(const String& name, uint16 accessMode = READ) 
86
            : mName(name), mSize(0), mAccess(accessMode) {}
87
        /// Returns the name of the stream, if it has one.
88
0
        const String& getName(void) { return mName; }
89
        /// Gets the access mode of the stream
90
0
        uint16 getAccessMode() const { return mAccess; }
91
        /** Reports whether this stream is readable. */
92
0
        virtual bool isReadable() const { return (mAccess & READ) != 0; }
93
        /** Reports whether this stream is writeable. */
94
0
        virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
95
        virtual ~DataStream() {}
96
        // Streaming operators
97
        template<typename T> DataStream& operator>>(T& val);
98
        /** Read the requisite number of bytes from the stream, 
99
            stopping at the end of the file.
100
        @param buf Reference to a buffer pointer
101
        @param count Number of bytes to read
102
        @return The number of bytes read
103
        */
104
        virtual size_t read(void* buf, size_t count) = 0;
105
        /** Write the requisite number of bytes from the stream (only applicable to 
106
            streams that are not read-only)
107
        @param buf Pointer to a buffer containing the bytes to write
108
        @param count Number of bytes to write
109
        @return The number of bytes written
110
        */
111
        virtual size_t write(const void* buf, size_t count)
112
0
        {
113
0
                        (void)buf;
114
0
                        (void)count;
115
0
            // default to not supported
116
0
            return 0;
117
0
        }
118
119
        /** Get a single line from the stream.
120
121
            The delimiter character is not included in the data
122
            returned, and it is skipped over so the next read will occur
123
            after it. The buffer contents will include a
124
            terminating character.
125
        @note
126
            If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
127
            otherwise, it'll produce unexpected results.
128
        @param buf Reference to a buffer pointer
129
        @param maxCount The maximum length of data to be read, excluding the terminating character
130
        @param delim The delimiter to stop at
131
        @return The number of bytes read, excluding the terminating character
132
        */
133
        virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
134
        
135
        /** Returns a String containing the next line of data, optionally 
136
            trimmed for whitespace. 
137
138
            This is a convenience method for text streams only, allowing you to 
139
            retrieve a String object containing the next line of data. The data
140
            is read up to the next newline character and the result trimmed if
141
            required.
142
        @note
143
            If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
144
            otherwise, it'll produce unexpected results.
145
        @param 
146
            trimAfter If true, the line is trimmed for whitespace (as in 
147
            String.trim(true,true))
148
        */
149
        virtual String getLine( bool trimAfter = true );
150
151
        /** Returns a String containing the entire stream. 
152
153
            This is a convenience method for text streams only, allowing you to 
154
            retrieve a String object containing all the data in the stream.
155
        */
156
        virtual String getAsString(void);
157
158
        /** Skip a single line from the stream.
159
        @note
160
            If you used this function, you <b>must</b> open the stream in <b>binary mode</b>,
161
            otherwise, it'll produce unexpected results.
162
        @par
163
            delim The delimiter(s) to stop at
164
        @return The number of bytes skipped
165
        */
166
        virtual size_t skipLine(const String& delim = "\n");
167
168
        /** Skip a defined number of bytes. This can also be a negative value, in which case
169
        the file pointer rewinds a defined number of bytes. */
170
        virtual void skip(long count) = 0;
171
    
172
        /** Repositions the read point to a specified byte.
173
        */
174
        virtual void seek( size_t pos ) = 0;
175
        
176
        /** Returns the current byte offset from beginning */
177
        virtual size_t tell(void) const = 0;
178
179
        /** Returns true if the stream has reached the end.
180
        */
181
        virtual bool eof(void) const = 0;
182
183
        /** Returns the total size of the data to be read from the stream, 
184
            or 0 if this is indeterminate for this stream. 
185
        */
186
1.09k
        size_t size(void) const { return mSize; }
187
188
        /** Close the stream; this makes further operations invalid. */
189
        virtual void close(void) = 0;
190
        
191
192
    };
193
194
    /// List of DataStream items
195
    typedef std::list<DataStreamPtr> DataStreamList;
196
197
    /** Common subclass of DataStream for handling data from chunks of memory.
198
    */
199
    class _OgreExport MemoryDataStream : public DataStream
200
    {
201
    private:
202
        /// Pointer to the start of the data area
203
        uchar* mData;
204
        /// Pointer to the current position in the memory
205
        uchar* mPos;
206
        /// Pointer to the end of the memory
207
        uchar* mEnd;
208
        /// Do we delete the memory on close
209
        bool mFreeOnClose;          
210
    public:
211
        
212
        /** Wrap an existing memory chunk in a stream.
213
        @param pMem Pointer to the existing memory
214
        @param size The size of the memory chunk in bytes
215
        @param freeOnClose If true, the memory associated will be destroyed
216
            when the stream is closed. Note: it's important that if you set
217
            this option to true, that you allocated the memory using OGRE_ALLOC_T
218
            with a category of MEMCATEGORY_GENERAL to ensure the freeing of memory 
219
            matches up.
220
        @param readOnly Whether to make the stream on this memory read-only once created
221
        */
222
        MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
223
        
224
        /** Wrap an existing memory chunk in a named stream.
225
        @param name The name to give the stream
226
        @param pMem Pointer to the existing memory
227
        @param size The size of the memory chunk in bytes
228
        @param freeOnClose If true, the memory associated will be destroyed
229
            when the stream is destroyed. Note: it's important that if you set
230
            this option to true, that you allocated the memory using OGRE_ALLOC_T
231
            with a category of MEMCATEGORY_GENERAL ensure the freeing of memory 
232
            matches up.
233
        @param readOnly Whether to make the stream on this memory read-only once created
234
        */
235
        MemoryDataStream(const String& name, void* pMem, size_t size, 
236
                bool freeOnClose = false, bool readOnly = false);
237
238
        /** Create a stream which pre-buffers the contents of another stream.
239
240
            This constructor can be used to intentionally read in the entire
241
            contents of another stream, copying them to the internal buffer
242
            and thus making them available in memory as a single unit.
243
        @param sourceStream Another DataStream which will provide the source
244
            of data
245
        @param freeOnClose If true, the memory associated will be destroyed
246
            when the stream is destroyed.
247
        @param readOnly Whether to make the stream on this memory read-only once created
248
        */
249
        MemoryDataStream(DataStream& sourceStream, 
250
                bool freeOnClose = true, bool readOnly = false);
251
        
252
        /** Create a stream which pre-buffers the contents of another stream.
253
254
            This constructor can be used to intentionally read in the entire
255
            contents of another stream, copying them to the internal buffer
256
            and thus making them available in memory as a single unit.
257
        @param sourceStream Another DataStream which will provide the source
258
            of data
259
        @param freeOnClose If true, the memory associated will be destroyed
260
            when the stream is destroyed.
261
        @param readOnly Whether to make the stream on this memory read-only once created
262
        */
263
        MemoryDataStream(const DataStreamPtr& sourceStream,
264
                bool freeOnClose = true, bool readOnly = false);
265
266
        /** Create a named stream which pre-buffers the contents of 
267
            another stream.
268
269
            This constructor can be used to intentionally read in the entire
270
            contents of another stream, copying them to the internal buffer
271
            and thus making them available in memory as a single unit.
272
        @param name The name to give the stream
273
        @param sourceStream Another DataStream which will provide the source
274
            of data
275
        @param freeOnClose If true, the memory associated will be destroyed
276
            when the stream is destroyed.
277
        @param readOnly Whether to make the stream on this memory read-only once created
278
        */
279
        MemoryDataStream(const String& name, DataStream& sourceStream, 
280
                bool freeOnClose = true, bool readOnly = false);
281
282
        /** Create a named stream which pre-buffers the contents of 
283
        another stream.
284
285
        This constructor can be used to intentionally read in the entire
286
        contents of another stream, copying them to the internal buffer
287
        and thus making them available in memory as a single unit.
288
        @param name The name to give the stream
289
        @param sourceStream Another DataStream which will provide the source
290
        of data
291
        @param freeOnClose If true, the memory associated will be destroyed
292
        when the stream is destroyed.
293
        @param readOnly Whether to make the stream on this memory read-only once created
294
        */
295
        MemoryDataStream(const String& name, const DataStreamPtr& sourceStream, 
296
            bool freeOnClose = true, bool readOnly = false);
297
298
        /** Create a stream with a brand new empty memory chunk.
299
        @param size The size of the memory chunk to create in bytes
300
        @param freeOnClose If true, the memory associated will be destroyed
301
            when the stream is destroyed.
302
        @param readOnly Whether to make the stream on this memory read-only once created
303
        */
304
        MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
305
        /** Create a named stream with a brand new empty memory chunk.
306
        @param name The name to give the stream
307
        @param size The size of the memory chunk to create in bytes
308
        @param freeOnClose If true, the memory associated will be destroyed
309
            when the stream is destroyed.
310
        @param readOnly Whether to make the stream on this memory read-only once created
311
        */
312
        MemoryDataStream(const String& name, size_t size, 
313
                bool freeOnClose = true, bool readOnly = false);
314
315
        ~MemoryDataStream();
316
317
        /** Get a pointer to the start of the memory block this stream holds. */
318
1.09k
        uchar* getPtr(void) { return mData; }
319
        
320
        /** Get a pointer to the current position in the memory block this stream holds. */
321
0
        uchar* getCurrentPtr(void) { return mPos; }
322
        
323
        /** @copydoc DataStream::read
324
        */
325
        size_t read(void* buf, size_t count) override;
326
327
        /** @copydoc DataStream::write
328
        */
329
        size_t write(const void* buf, size_t count) override;
330
331
        /** @copydoc DataStream::readLine
332
        */
333
        size_t readLine(char* buf, size_t maxCount, const String& delim = "\n") override;
334
        
335
        /** @copydoc DataStream::skipLine
336
        */
337
        size_t skipLine(const String& delim = "\n") override;
338
339
        /** @copydoc DataStream::skip
340
        */
341
        void skip(long count) override;
342
    
343
        /** @copydoc DataStream::seek
344
        */
345
        void seek( size_t pos ) override;
346
        
347
        /** @copydoc DataStream::tell
348
        */
349
        size_t tell(void) const override;
350
351
        /** @copydoc DataStream::eof
352
        */
353
        bool eof(void) const override;
354
355
        /** @copydoc DataStream::close
356
        */
357
        void close(void) override;
358
359
        /** Sets whether or not to free the encapsulated memory on close. */
360
0
        void setFreeOnClose(bool free) { mFreeOnClose = free; }
361
    };
362
363
    /** Common subclass of DataStream for handling data from 
364
        std::basic_istream.
365
    */
366
    class _OgreExport FileStreamDataStream : public DataStream
367
    {
368
    private:
369
        /// Reference to source stream (read)
370
        std::istream* mInStream;
371
        /// Reference to source file stream (read-only)
372
        std::ifstream* mFStreamRO;
373
        /// Reference to source file stream (read-write)
374
        std::fstream* mFStream;
375
        bool mFreeOnClose;  
376
377
        void determineAccess();
378
    public:
379
        /** Construct a read-only stream from an STL stream
380
        @param s Pointer to source stream
381
        @param freeOnClose Whether to delete the underlying stream on 
382
            destruction of this class
383
        */
384
        FileStreamDataStream(std::ifstream* s, 
385
            bool freeOnClose = true);
386
        /** Construct a read-write stream from an STL stream
387
        @param s Pointer to source stream
388
        @param freeOnClose Whether to delete the underlying stream on 
389
        destruction of this class
390
        */
391
        FileStreamDataStream(std::fstream* s, 
392
            bool freeOnClose = true);
393
394
        /** Construct named read-only stream from an STL stream
395
        @param name The name to give this stream
396
        @param s Pointer to source stream
397
        @param freeOnClose Whether to delete the underlying stream on 
398
            destruction of this class
399
        */
400
        FileStreamDataStream(const String& name, 
401
            std::ifstream* s, 
402
            bool freeOnClose = true);
403
404
        /** Construct named read-write stream from an STL stream
405
        @param name The name to give this stream
406
        @param s Pointer to source stream
407
        @param freeOnClose Whether to delete the underlying stream on 
408
        destruction of this class
409
        */
410
        FileStreamDataStream(const String& name, 
411
            std::fstream* s, 
412
            bool freeOnClose = true);
413
414
        /** Construct named read-only stream from an STL stream, and tell it the size
415
416
            This variant tells the class the size of the stream too, which 
417
            means this class does not need to seek to the end of the stream 
418
            to determine the size up-front. This can be beneficial if you have
419
            metadata about the contents of the stream already.
420
        @param name The name to give this stream
421
        @param s Pointer to source stream
422
        @param size Size of the stream contents in bytes
423
        @param freeOnClose Whether to delete the underlying stream on 
424
            destruction of this class. If you specify 'true' for this you
425
            must ensure that the stream was allocated using OGRE_NEW_T with 
426
            MEMCATEGRORY_GENERAL.
427
        */
428
        FileStreamDataStream(const String& name, 
429
            std::ifstream* s, 
430
            size_t size, 
431
            bool freeOnClose = true);
432
433
        /** Construct named read-write stream from an STL stream, and tell it the size
434
435
        This variant tells the class the size of the stream too, which 
436
        means this class does not need to seek to the end of the stream 
437
        to determine the size up-front. This can be beneficial if you have
438
        metadata about the contents of the stream already.
439
        @param name The name to give this stream
440
        @param s Pointer to source stream
441
        @param size Size of the stream contents in bytes
442
        @param freeOnClose Whether to delete the underlying stream on 
443
        destruction of this class. If you specify 'true' for this you
444
        must ensure that the stream was allocated using OGRE_NEW_T with 
445
        MEMCATEGRORY_GENERAL.
446
        */
447
        FileStreamDataStream(const String& name, 
448
            std::fstream* s, 
449
            size_t size, 
450
            bool freeOnClose = true);
451
452
        ~FileStreamDataStream();
453
454
        /** @copydoc DataStream::read
455
        */
456
        size_t read(void* buf, size_t count) override;
457
458
        /** @copydoc DataStream::write
459
        */
460
        size_t write(const void* buf, size_t count) override;
461
462
        /** @copydoc DataStream::readLine
463
        */
464
        size_t readLine(char* buf, size_t maxCount, const String& delim = "\n") override;
465
        
466
        /** @copydoc DataStream::skip
467
        */
468
        void skip(long count) override;
469
    
470
        /** @copydoc DataStream::seek
471
        */
472
        void seek( size_t pos ) override;
473
474
        /** @copydoc DataStream::tell
475
        */
476
        size_t tell(void) const override;
477
478
        /** @copydoc DataStream::eof
479
        */
480
        bool eof(void) const override;
481
482
        /** @copydoc DataStream::close
483
        */
484
        void close(void) override;
485
        
486
        
487
    };
488
489
    /** Common subclass of DataStream for handling data from C-style file 
490
        handles.
491
492
        Use of this class is generally discouraged; if you want to wrap file
493
        access in a DataStream, you should definitely be using the C++ friendly
494
        FileStreamDataStream. However, since there are quite a few applications
495
        and libraries still wedded to the old FILE handle access, this stream
496
        wrapper provides some backwards compatibility.
497
    */
498
    class _OgreExport FileHandleDataStream : public DataStream
499
    {
500
    private:
501
        FILE* mFileHandle;
502
    public:
503
        /// Create stream from a C file handle
504
        FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
505
        /// Create named stream from a C file handle
506
        FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
507
        ~FileHandleDataStream();
508
509
        /** @copydoc DataStream::read
510
        */
511
        size_t read(void* buf, size_t count) override;
512
513
        /** @copydoc DataStream::write
514
        */
515
        size_t write(const void* buf, size_t count) override;
516
517
        /** @copydoc DataStream::skip
518
        */
519
        void skip(long count) override;
520
    
521
        /** @copydoc DataStream::seek
522
        */
523
        void seek( size_t pos ) override;
524
525
        /** @copydoc DataStream::tell
526
        */
527
        size_t tell(void) const override;
528
529
        /** @copydoc DataStream::eof
530
        */
531
        bool eof(void) const override;
532
533
        /** @copydoc DataStream::close
534
        */
535
        void close(void) override;
536
537
    };
538
    /** @} */
539
    /** @} */
540
}
541
542
#include "OgreHeaderSuffix.h"
543
544
#endif
545