summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvcore/StdStream.h
blob: f65d6dab5939af150870587236c4f6671ad7bf2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// This code is in the public domain -- Ignacio Castaño <castano@gmail.com>

//#pragma once
//#ifndef NV_CORE_STDSTREAM_H
//#define NV_CORE_STDSTREAM_H

#include "nvcore.h"
#include "Stream.h"
#include "Array.h"

#include <stdio.h> // fopen
#include <string.h> // memcpy

namespace nv
{

    // Portable version of fopen.
    inline FILE * fileOpen(const char * fileName, const char * mode)
    {
        nvCheck(fileName != NULL);
#if NV_CC_MSVC && _MSC_VER >= 1400
        FILE * fp;
        if (fopen_s(&fp, fileName, mode) == 0) {
            return fp;
        }
        return NULL;
#else
        return fopen(fileName, mode);
#endif
    }


    /// Base stdio stream.
    class NVCORE_CLASS StdStream : public Stream
    {
        NV_FORBID_COPY(StdStream);
    public:

        /// Ctor.
        StdStream( FILE * fp, bool autoclose ) : m_fp(fp), m_autoclose(autoclose) { }

        /// Dtor. 
        virtual ~StdStream()
        {
            if( m_fp != NULL && m_autoclose ) {
#if NV_OS_WIN32
                _fclose_nolock( m_fp );
#else
                fclose( m_fp );
#endif
            }
        }


        /** @name Stream implementation. */
        //@{
        virtual void seek( uint pos )
        {
            nvDebugCheck(m_fp != NULL);
            nvDebugCheck(pos <= size());
#if NV_OS_WIN32
            _fseek_nolock(m_fp, pos, SEEK_SET);
#else
            fseek(m_fp, pos, SEEK_SET);
#endif
        }

        virtual uint tell() const
        {
            nvDebugCheck(m_fp != NULL);
#if NV_OS_WIN32
            return _ftell_nolock(m_fp);
#else
            return (uint)ftell(m_fp);
#endif
        }

        virtual uint size() const
        {
            nvDebugCheck(m_fp != NULL);
#if NV_OS_WIN32
            uint pos = _ftell_nolock(m_fp);
            _fseek_nolock(m_fp, 0, SEEK_END);
            uint end = _ftell_nolock(m_fp);
            _fseek_nolock(m_fp, pos, SEEK_SET);
#else
            uint pos = (uint)ftell(m_fp);
            fseek(m_fp, 0, SEEK_END);
            uint end = (uint)ftell(m_fp);
            fseek(m_fp, pos, SEEK_SET);
#endif
            return end;
        }

        virtual bool isError() const
        {
            return m_fp == NULL || ferror( m_fp ) != 0;
        }

        virtual void clearError()
        {
            nvDebugCheck(m_fp != NULL);
            clearerr(m_fp);
        }

        // @@ The original implementation uses feof, which only returns true when we attempt to read *past* the end of the stream. 
        // That is, if we read the last byte of a file, then isAtEnd would still return false, even though the stream pointer is at the file end. This is not the intent and was inconsistent with the implementation of the MemoryStream, a better 
        // implementation uses use ftell and fseek to determine our location within the file.
        virtual bool isAtEnd() const
        {
            if (m_fp == NULL) return true;
            //nvDebugCheck(m_fp != NULL);
            //return feof( m_fp ) != 0;
#if NV_OS_WIN32
            uint pos = _ftell_nolock(m_fp);
            _fseek_nolock(m_fp, 0, SEEK_END);
            uint end = _ftell_nolock(m_fp);
            _fseek_nolock(m_fp, pos, SEEK_SET);
#else
            uint pos = (uint)ftell(m_fp);
            fseek(m_fp, 0, SEEK_END);
            uint end = (uint)ftell(m_fp);
            fseek(m_fp, pos, SEEK_SET);
#endif
            return pos == end;
        }

        /// Always true.
        virtual bool isSeekable() const { return true; }
        //@}

    protected:

        FILE * m_fp;
        bool m_autoclose;

    };


    /// Standard output stream.
    class NVCORE_CLASS StdOutputStream : public StdStream
    {
        NV_FORBID_COPY(StdOutputStream);
    public:

        /// Construct stream by file name.
        StdOutputStream( const char * name ) : StdStream(fileOpen(name, "wb"), /*autoclose=*/true) { }

        /// Construct stream by file handle.
        StdOutputStream( FILE * fp, bool autoclose ) : StdStream(fp, autoclose)
        {
        }

        /** @name Stream implementation. */
        //@{
        /// Write data.
        virtual uint serialize( void * data, uint len )
        {
            nvDebugCheck(data != NULL);
            nvDebugCheck(m_fp != NULL);
#if NV_OS_WIN32
            return (uint)_fwrite_nolock(data, 1, len, m_fp);
#elif NV_OS_LINUX
            return (uint)fwrite_unlocked(data, 1, len, m_fp);
#elif NV_OS_DARWIN
            // @@ No error checking, always returns len.
            for (uint i = 0; i < len; i++) {
                putc_unlocked(((char *)data)[i], m_fp);
            }
            return len;
#else
            return (uint)fwrite(data, 1, len, m_fp);
#endif
        }

        virtual bool isLoading() const
        {
            return false;
        }

        virtual bool isSaving() const
        {
            return true;
        }
        //@}

    };


    /// Standard input stream.
    class NVCORE_CLASS StdInputStream : public StdStream
    {
        NV_FORBID_COPY(StdInputStream);
    public:

        /// Construct stream by file name.
        StdInputStream( const char * name ) : StdStream(fileOpen(name, "rb"), /*autoclose=*/true) { }

        /// Construct stream by file handle.
        StdInputStream( FILE * fp, bool autoclose=true ) : StdStream(fp, autoclose)
        {
        }

        /** @name Stream implementation. */
        //@{
        /// Read data.
        virtual uint serialize( void * data, uint len )
        {
            nvDebugCheck(data != NULL);
            nvDebugCheck(m_fp != NULL);
#if NV_OS_WIN32
            return (uint)_fread_nolock(data, 1, len, m_fp);
#elif NV_OS_LINUX
            return (uint)fread_unlocked(data, 1, len, m_fp);
#elif NV_OS_DARWIN
            // This is rather lame. Not sure if it's faster than the locked version.
            for (uint i = 0; i < len; i++) {
                ((char *)data)[i] = getc_unlocked(m_fp);
                if (feof_unlocked(m_fp) != 0) {
                    return i;
                }
            }
            return len;
#else
            return (uint)fread(data, 1, len, m_fp);
#endif
            
        }

        virtual bool isLoading() const
        {
            return true;
        }

        virtual bool isSaving() const
        {
            return false;
        }
        //@}
    };



    /// Memory input stream.
    class NVCORE_CLASS MemoryInputStream : public Stream
    {
        NV_FORBID_COPY(MemoryInputStream);
    public:

        /// Ctor.
        MemoryInputStream( const uint8 * mem, uint size ) : m_mem(mem), m_ptr(mem), m_size(size) { }

        /** @name Stream implementation. */
        //@{
        /// Read data.
        virtual uint serialize( void * data, uint len )
        {
            nvDebugCheck(data != NULL);
            nvDebugCheck(!isError());

            uint left = m_size - tell();
            if (len > left) len = left;

            memcpy( data, m_ptr, len );
            m_ptr += len;

            return len;
        }

        virtual void seek( uint pos )
        {
            nvDebugCheck(!isError());
            m_ptr = m_mem + pos;
            nvDebugCheck(!isError());
        }

        virtual uint tell() const
        {
            nvDebugCheck(m_ptr >= m_mem);
            return uint(m_ptr - m_mem);
        }

        virtual uint size() const
        {
            return m_size;
        }

        virtual bool isError() const
        {
            return m_mem == NULL || m_ptr > m_mem + m_size || m_ptr < m_mem;
        }

        virtual void clearError()
        {
            // Nothing to do.
        }

        virtual bool isAtEnd() const
        {
            return m_ptr == m_mem + m_size;
        }

        /// Always true.
        virtual bool isSeekable() const
        {
            return true;
        }

        virtual bool isLoading() const
        {
            return true;
        }

        virtual bool isSaving() const
        {
            return false;
        }
        //@}

        const uint8 * ptr() const { return m_ptr; }


    private:

        const uint8 * m_mem;
        const uint8 * m_ptr;
        uint m_size;

    };


    /// Buffer output stream.
    class NVCORE_CLASS BufferOutputStream : public Stream
    {
        NV_FORBID_COPY(BufferOutputStream);
    public:

        BufferOutputStream(Array<uint8> & buffer) : m_buffer(buffer) { }

        virtual uint serialize( void * data, uint len )
        {
            nvDebugCheck(data != NULL);
            m_buffer.append((uint8 *)data, len);
            return len;
        }

        virtual void seek( uint /*pos*/ ) { /*Not implemented*/ }
        virtual uint tell() const { return m_buffer.size(); }
        virtual uint size() const { return m_buffer.size(); }

        virtual bool isError() const { return false; }
        virtual void clearError() {}

        virtual bool isAtEnd() const { return true; }
        virtual bool isSeekable() const { return false; }
        virtual bool isLoading() const { return false; }
        virtual bool isSaving() const { return true; }

    private:
        Array<uint8> & m_buffer;
    };


    /// Protected input stream.
    class NVCORE_CLASS ProtectedStream : public Stream
    {
        NV_FORBID_COPY(ProtectedStream);
    public:

        /// Ctor.
        ProtectedStream( Stream & s ) : m_s(&s), m_autodelete(false)
        { 
        }

        /// Ctor.
        ProtectedStream( Stream * s, bool autodelete = true ) : 
        m_s(s), m_autodelete(autodelete) 
        {
            nvDebugCheck(m_s != NULL);
        }

        /// Dtor.
        virtual ~ProtectedStream()
        {
            if( m_autodelete ) {
                delete m_s;
            }
        }

        /** @name Stream implementation. */
        //@{
        /// Read data.
        virtual uint serialize( void * data, uint len )
        {
            nvDebugCheck(data != NULL);
            len = m_s->serialize( data, len );

            if( m_s->isError() ) {
#if NV_OS_ORBIS
                //SBtodoORBIS disabled (no exceptions)
#else
                throw;
#endif
            }

            return len;
        }

        virtual void seek( uint pos )
        {
            m_s->seek( pos );

            if( m_s->isError() ) {
#if NV_OS_ORBIS
                //SBtodoORBIS disabled (no exceptions)
#else
                throw;
#endif
            }
        }

        virtual uint tell() const
        {
            return m_s->tell();
        }

        virtual uint size() const
        {
            return m_s->size();
        }

        virtual bool isError() const
        {
            return m_s->isError();
        }

        virtual void clearError()
        {
            m_s->clearError();
        }

        virtual bool isAtEnd() const
        {
            return m_s->isAtEnd();
        }

        virtual bool isSeekable() const
        {
            return m_s->isSeekable();
        }

        virtual bool isLoading() const
        {
            return m_s->isLoading();
        }

        virtual bool isSaving() const
        {
            return m_s->isSaving();
        }
        //@}


    private:

        Stream * const m_s;
        bool const m_autodelete;

    };

} // nv namespace


//#endif // NV_CORE_STDSTREAM_H