# 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-2006 Torus Knot Software Ltd
    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 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef __DataStream_H__
    00030 #define __DataStream_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreString.h"
    00034 #include "OgreSharedPtr.h"
    00035 #include <istream>
    00036
    00037 namespace Ogre {
    00038
    00058     class _OgreExport DataStream : public StreamAlloc
    00059     {
    00060     protected:
    00062         String mName;
    00064         size_t mSize;
    00065         #define OGRE_STREAM_TEMP_SIZE 128
    00066     public:
    00068         DataStream() : mSize(0) {}
    00070         DataStream(const String& name) : mName(name), mSize(0) {}
    00072         const String& getName(void) { return mName; }
    00073         virtual ~DataStream() {}
    00074         // Streaming operators
    00075         template<typename T> DataStream& operator>>(T& val);
    00082         virtual size_t read(void* buf, size_t count) = 0;
    00097         virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00098
    00113         virtual String getLine( bool trimAfter = true );
    00114
    00120         virtual String getAsString(void);
    00121
    00129         virtual size_t skipLine(const String& delim = "\n");
    00130
    00133         virtual void skip(long count) = 0;
    00134
    00137         virtual void seek( size_t pos ) = 0;
    00138
    00140         virtual size_t tell(void) const = 0;
    00141
    00144         virtual bool eof(void) const = 0;
    00145
    00149         size_t size(void) const { return mSize; }
    00150
    00152         virtual void close(void) = 0;
    00153
    00154
    00155     };
    00156
    00160     typedef SharedPtr<DataStream> DataStreamPtr;
    00161
    00163     typedef std::list<DataStreamPtr> DataStreamList;
    00165     typedef SharedPtr<DataStreamList> DataStreamListPtr;
    00166
    00169     class _OgreExport MemoryDataStream : public DataStream
    00170     {
    00171     protected:
    00173         uchar* mData;
    00175         uchar* mPos;
    00177         uchar* mEnd;
    00179         bool mFreeOnClose;
    00180     public:
    00181
    00191         MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false);
    00192
    00203         MemoryDataStream(const String& name, void* pMem, size_t size,
    00204                 bool freeOnClose = false);
    00205
    00216         MemoryDataStream(DataStream& sourceStream,
    00217                 bool freeOnClose = true);
    00218
    00229         MemoryDataStream(DataStreamPtr& sourceStream,
    00230                 bool freeOnClose = true);
    00231
    00244         MemoryDataStream(const String& name, DataStream& sourceStream,
    00245                 bool freeOnClose = true);
    00246
    00259         MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
    00260             bool freeOnClose = true);
    00261
    00267         MemoryDataStream(size_t size, bool freeOnClose = true);
    00274         MemoryDataStream(const String& name, size_t size,
    00275                 bool freeOnClose = true);
    00276
    00277         ~MemoryDataStream();
    00278
    00280         uchar* getPtr(void) { return mData; }
    00281
    00283         uchar* getCurrentPtr(void) { return mPos; }
    00284
    00287         size_t read(void* buf, size_t count);
    00290         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00291
    00294         size_t skipLine(const String& delim = "\n");
    00295
    00298         void skip(long count);
    00299
    00302         void seek( size_t pos );
    00303
    00306         size_t tell(void) const;
    00307
    00310         bool eof(void) const;
    00311
    00314         void close(void);
    00315
    00317         void setFreeOnClose(bool free) { mFreeOnClose = free; }
    00318     };
    00319
    00323     typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
    00324
    00328     class _OgreExport FileStreamDataStream : public DataStream
    00329     {
    00330     protected:
    00332         std::ifstream* mpStream;
    00333         bool mFreeOnClose;
    00334     public:
    00340         FileStreamDataStream(std::ifstream* s,
    00341             bool freeOnClose = true);
    00348         FileStreamDataStream(const String& name,
    00349             std::ifstream* s,
    00350             bool freeOnClose = true);
    00351
    00366         FileStreamDataStream(const String& name,
    00367             std::ifstream* s,
    00368             size_t size,
    00369             bool freeOnClose = true);
    00370
    00371         ~FileStreamDataStream();
    00372
    00375         size_t read(void* buf, size_t count);
    00378         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00379
    00382         void skip(long count);
    00383
    00386         void seek( size_t pos );
    00387
    00390         size_t tell(void) const;
    00391
    00394         bool eof(void) const;
    00395
    00398         void close(void);
    00399
    00400
    00401     };
    00402
    00412     class _OgreExport FileHandleDataStream : public DataStream
    00413     {
    00414     protected:
    00415         FILE* mFileHandle;
    00416     public:
    00418         FileHandleDataStream(FILE* handle);
    00420         FileHandleDataStream(const String& name, FILE* handle);
    00421         ~FileHandleDataStream();
    00422
    00425         size_t read(void* buf, size_t count);
    00426
    00429         void skip(long count);
    00430
    00433         void seek( size_t pos );
    00434
    00437         size_t tell(void) const;
    00438
    00441         bool eof(void) const;
    00442
    00445         void close(void);
    00446
    00447     };
    00448 }
    00449 #endif
    00450

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Dec 27 17:31:05 2009