OGRE: OgreDataStream.h Source File - OGRE Documentation

# OgreDataStream.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef __DataStream_H__
    00029 #define __DataStream_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032 #include "OgreString.h"
    00033 #include "OgreSharedPtr.h"
    00034 #include <istream>
    00035
    00036 namespace Ogre {
    00063     class _OgreExport DataStream : public StreamAlloc
    00064     {
    00065     public:
    00066         enum AccessMode
    00067         {
    00068             READ = 1,
    00069             WRITE = 2
    00070         };
    00071     protected:
    00073         String mName;
    00075         size_t mSize;
    00077         uint16 mAccess;
    00078
    00079         #define OGRE_STREAM_TEMP_SIZE 128
    00080     public:
    00082         DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
    00084         DataStream(const String& name, uint16 accessMode = READ)
    00085             : mName(name), mSize(0), mAccess(accessMode) {}
    00087         const String& getName(void) { return mName; }
    00089         uint16 getAccessMode() const { return mAccess; }
    00091         virtual bool isReadable() const { return (mAccess & READ) != 0; }
    00093         virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
    00094         virtual ~DataStream() {}
    00095         // Streaming operators
    00096         template<typename T> DataStream& operator>>(T& val);
    00103         virtual size_t read(void* buf, size_t count) = 0;
    00110         virtual size_t write(const void* buf, size_t count)
    00111         {
    00112                         (void)buf;
    00113                         (void)count;
    00114             // default to not supported
    00115             return 0;
    00116         }
    00117
    00132         virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00133
    00148         virtual String getLine( bool trimAfter = true );
    00149
    00155         virtual String getAsString(void);
    00156
    00164         virtual size_t skipLine(const String& delim = "\n");
    00165
    00168         virtual void skip(long count) = 0;
    00169
    00172         virtual void seek( size_t pos ) = 0;
    00173
    00175         virtual size_t tell(void) const = 0;
    00176
    00179         virtual bool eof(void) const = 0;
    00180
    00184         size_t size(void) const { return mSize; }
    00185
    00187         virtual void close(void) = 0;
    00188
    00189
    00190     };
    00191
    00195     typedef SharedPtr<DataStream> DataStreamPtr;
    00196
    00198     typedef list<DataStreamPtr>::type DataStreamList;
    00200     typedef SharedPtr<DataStreamList> DataStreamListPtr;
    00201
    00204     class _OgreExport MemoryDataStream : public DataStream
    00205     {
    00206     protected:
    00208         uchar* mData;
    00210         uchar* mPos;
    00212         uchar* mEnd;
    00214         bool mFreeOnClose;
    00215     public:
    00216
    00227         MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
    00228
    00240         MemoryDataStream(const String& name, void* pMem, size_t size,
    00241                 bool freeOnClose = false, bool readOnly = false);
    00242
    00254         MemoryDataStream(DataStream& sourceStream,
    00255                 bool freeOnClose = true, bool readOnly = false);
    00256
    00268         MemoryDataStream(DataStreamPtr& sourceStream,
    00269                 bool freeOnClose = true, bool readOnly = false);
    00270
    00284         MemoryDataStream(const String& name, DataStream& sourceStream,
    00285                 bool freeOnClose = true, bool readOnly = false);
    00286
    00300         MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
    00301             bool freeOnClose = true, bool readOnly = false);
    00302
    00309         MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
    00317         MemoryDataStream(const String& name, size_t size,
    00318                 bool freeOnClose = true, bool readOnly = false);
    00319
    00320         ~MemoryDataStream();
    00321
    00323         uchar* getPtr(void) { return mData; }
    00324
    00326         uchar* getCurrentPtr(void) { return mPos; }
    00327
    00330         size_t read(void* buf, size_t count);
    00331
    00334         size_t write(const void* buf, size_t count);
    00335
    00338         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00339
    00342         size_t skipLine(const String& delim = "\n");
    00343
    00346         void skip(long count);
    00347
    00350         void seek( size_t pos );
    00351
    00354         size_t tell(void) const;
    00355
    00358         bool eof(void) const;
    00359
    00362         void close(void);
    00363
    00365         void setFreeOnClose(bool free) { mFreeOnClose = free; }
    00366     };
    00367
    00371     typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
    00372
    00376     class _OgreExport FileStreamDataStream : public DataStream
    00377     {
    00378     protected:
    00380         std::istream* mpInStream;
    00382         std::ifstream* mpFStreamRO;
    00384         std::fstream* mpFStream;
    00385         bool mFreeOnClose;
    00386
    00387         void determineAccess();
    00388     public:
    00394         FileStreamDataStream(std::ifstream* s,
    00395             bool freeOnClose = true);
    00401         FileStreamDataStream(std::fstream* s,
    00402             bool freeOnClose = true);
    00403
    00410         FileStreamDataStream(const String& name,
    00411             std::ifstream* s,
    00412             bool freeOnClose = true);
    00413
    00420         FileStreamDataStream(const String& name,
    00421             std::fstream* s,
    00422             bool freeOnClose = true);
    00423
    00438         FileStreamDataStream(const String& name,
    00439             std::ifstream* s,
    00440             size_t size,
    00441             bool freeOnClose = true);
    00442
    00457         FileStreamDataStream(const String& name,
    00458             std::fstream* s,
    00459             size_t size,
    00460             bool freeOnClose = true);
    00461
    00462         ~FileStreamDataStream();
    00463
    00466         size_t read(void* buf, size_t count);
    00467
    00470         size_t write(const void* buf, size_t count);
    00471
    00474         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00475
    00478         void skip(long count);
    00479
    00482         void seek( size_t pos );
    00483
    00486         size_t tell(void) const;
    00487
    00490         bool eof(void) const;
    00491
    00494         void close(void);
    00495
    00496
    00497     };
    00498
    00508     class _OgreExport FileHandleDataStream : public DataStream
    00509     {
    00510     protected:
    00511         FILE* mFileHandle;
    00512     public:
    00514         FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
    00516         FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
    00517         ~FileHandleDataStream();
    00518
    00521         size_t read(void* buf, size_t count);
    00522
    00525         size_t write(const void* buf, size_t count);
    00526
    00529         void skip(long count);
    00530
    00533         void seek( size_t pos );
    00534
    00537         size_t tell(void) const;
    00538
    00541         bool eof(void) const;
    00542
    00545         void close(void);
    00546
    00547     };
    00550 }
    00551 #endif
    00552

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:51