# 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-2005 The OGRE Team
    00008 Also see acknowledgements in Readme.html
    00009
    00010 This program is free software; you can redistribute it and/or modify it under
    00011 the terms of the GNU Lesser General Public License as published by the Free Software
    00012 Foundation; either version 2 of the License, or (at your option) any later
    00013 version.
    00014
    00015 This program is distributed in the hope that it will be useful, but WITHOUT
    00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00018
    00019 You should have received a copy of the GNU Lesser General Public License along with
    00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00022 http://www.gnu.org/copyleft/lesser.txt.
    00023 -----------------------------------------------------------------------------
    00024 */
    00025 #ifndef __DataStream_H__
    00026 #define __DataStream_H__
    00027
    00028 #include "OgrePrerequisites.h"
    00029 #include "OgreString.h"
    00030 #include "OgreSharedPtr.h"
    00031 #include <istream>
    00032
    00033 namespace Ogre {
    00034
    00054     class _OgreExport DataStream
    00055     {
    00056     protected:
    00058         String mName;
    00060         size_t mSize;
    00061         #define OGRE_STREAM_TEMP_SIZE 128
    00062
    00063         char mTmpArea[OGRE_STREAM_TEMP_SIZE];
    00064     public:
    00066         DataStream() : mSize(0) {}
    00068         DataStream(const String& name) : mName(name), mSize(0) {}
    00070         const String& getName(void) { return mName; }
    00071         virtual ~DataStream() {}
    00072         // Streaming operators
    00073         template<typename T> DataStream& operator>>(T& val);
    00080         virtual size_t read(void* buf, size_t count) = 0;
    00092         virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n") = 0;
    00093
    00105         virtual String getLine( bool trimAfter = true );
    00106
    00112         virtual String getAsString(void);
    00113
    00118         virtual size_t skipLine(const String& delim = "\n") = 0;
    00119
    00122         virtual void skip(long count) = 0;
    00123
    00126         virtual void seek( size_t pos ) = 0;
    00127
    00129         virtual size_t tell(void) const = 0;
    00130
    00133         virtual bool eof(void) const = 0;
    00134
    00138         size_t size(void) const { return mSize; }
    00139
    00141         virtual void close(void) = 0;
    00142
    00143
    00144     };
    00145
    00149     typedef SharedPtr<DataStream> DataStreamPtr;
    00150
    00152     typedef std::list<DataStreamPtr> DataStreamList;
    00154     typedef SharedPtr<DataStreamList> DataStreamListPtr;
    00155
    00158     class _OgreExport MemoryDataStream : public DataStream
    00159     {
    00160     protected:
    00162         uchar* mData;
    00164         uchar* mPos;
    00166         uchar* mEnd;
    00168         bool mFreeOnClose;
    00169     public:
    00170
    00177         MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false);
    00178
    00186         MemoryDataStream(const String& name, void* pMem, size_t size,
    00187                 bool freeOnClose = false);
    00188
    00199         MemoryDataStream(DataStream& sourceStream,
    00200                 bool freeOnClose = true);
    00201
    00212         MemoryDataStream(DataStreamPtr& sourceStream,
    00213                 bool freeOnClose = true);
    00214
    00227         MemoryDataStream(const String& name, DataStream& sourceStream,
    00228                 bool freeOnClose = true);
    00229
    00242         MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
    00243             bool freeOnClose = true);
    00244
    00250         MemoryDataStream(size_t size, bool freeOnClose = true);
    00257         MemoryDataStream(const String& name, size_t size,
    00258                 bool freeOnClose = true);
    00259
    00260         ~MemoryDataStream();
    00261
    00263         uchar* getPtr(void) { return mData; }
    00264
    00266         uchar* getCurrentPtr(void) { return mPos; }
    00267
    00270         size_t read(void* buf, size_t count);
    00273         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00274
    00277         size_t skipLine(const String& delim = "\n");
    00278
    00281         void skip(long count);
    00282
    00285         void seek( size_t pos );
    00286
    00289         size_t tell(void) const;
    00290
    00293         bool eof(void) const;
    00294
    00297         void close(void);
    00298
    00300         void setFreeOnClose(bool free) { mFreeOnClose = free; }
    00301     };
    00302
    00306     typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
    00307
    00311     class _OgreExport FileStreamDataStream : public DataStream
    00312     {
    00313     protected:
    00315         std::ifstream* mpStream;
    00316         bool mFreeOnClose;
    00317     public:
    00323         FileStreamDataStream(std::ifstream* s,
    00324             bool freeOnClose = true);
    00331         FileStreamDataStream(const String& name,
    00332             std::ifstream* s,
    00333             bool freeOnClose = true);
    00334
    00347         FileStreamDataStream(const String& name,
    00348             std::ifstream* s,
    00349             size_t size,
    00350             bool freeOnClose = true);
    00351
    00352         ~FileStreamDataStream();
    00353
    00356         size_t read(void* buf, size_t count);
    00359         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00360
    00363         size_t skipLine(const String& delim = "\n");
    00364
    00367         void skip(long count);
    00368
    00371         void seek( size_t pos );
    00372
    00375         size_t tell(void) const;
    00376
    00379         bool eof(void) const;
    00380
    00383         void close(void);
    00384
    00385
    00386     };
    00387
    00397     class _OgreExport FileHandleDataStream : public DataStream
    00398     {
    00399     protected:
    00400         FILE* mFileHandle;
    00401     public:
    00403         FileHandleDataStream(FILE* handle);
    00405         FileHandleDataStream(const String& name, FILE* handle);
    00406         ~FileHandleDataStream();
    00407
    00410         size_t read(void* buf, size_t count);
    00413         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00414
    00417         size_t skipLine(const String& delim = "\n");
    00418
    00421         void skip(long count);
    00422
    00425         void seek( size_t pos );
    00426
    00429         size_t tell(void) const;
    00430
    00433         bool eof(void) const;
    00434
    00437         void close(void);
    00438
    00439     };
    00440 }
    00441 #endif
    00442

Copyright © 2000-2005 by The OGRE Team
Last modified Sun Nov 20 12:35:58 2005