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-2012 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 {
    00037
    00040     template <size_t cacheSize>
    00041     class StaticCache
    00042     {
    00043     protected:
    00045         char mBuffer[cacheSize];
    00046
    00048         size_t mValidBytes;
    00050         size_t mPos;
    00051
    00052     public:
    00054         StaticCache()
    00055         {
    00056             mValidBytes = 0;
    00057             mPos = 0;
    00058         }
    00059
    00062         size_t cacheData(const void* buf, size_t count)
    00063         {
    00064             assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
    00065
    00066             if (count < cacheSize)
    00067             {
    00068                 // number of bytes written is less than total size of cache
    00069                 if (count + mValidBytes <= cacheSize)
    00070                 {
    00071                     // just append
    00072                     memcpy(mBuffer + mValidBytes, buf, count);
    00073                     mValidBytes += count;
    00074                 }
    00075                 else
    00076                 {
    00077                     size_t begOff = count - (cacheSize - mValidBytes);
    00078                     // override old cache content in the beginning
    00079                     memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
    00080                     // append new data
    00081                     memcpy(mBuffer + cacheSize - count, buf, count);
    00082                     mValidBytes = cacheSize;
    00083                 }
    00084                 mPos = mValidBytes;
    00085                 return count;
    00086             }
    00087             else
    00088             {
    00089                 // discard all
    00090                 memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
    00091                 mValidBytes = mPos = cacheSize;
    00092                 return cacheSize;
    00093             }
    00094         }
    00096         size_t read(void* buf, size_t count)
    00097         {
    00098             size_t rb = avail();
    00099             rb = (rb < count) ? rb : count;
    00100             memcpy(buf, mBuffer + mPos, rb);
    00101             mPos += rb;
    00102             return rb;
    00103         }
    00104
    00106         bool rewind(size_t count)
    00107         {
    00108             if (mPos < count)
    00109             {
    00110                 clear();
    00111                 return false;
    00112             }
    00113             else
    00114             {
    00115                 mPos -= count;
    00116                 return true;
    00117             }
    00118         }
    00120         bool ff(size_t count)
    00121         {
    00122             if (avail() < count)
    00123             {
    00124                 clear();
    00125                 return false;
    00126             }
    00127             else
    00128             {
    00129                 mPos += count;
    00130                 return true;
    00131             }
    00132         }
    00133
    00135         size_t avail() const
    00136         {
    00137             return mValidBytes - mPos;
    00138         }
    00139
    00141         void clear()
    00142         {
    00143             mValidBytes = 0;
    00144             mPos = 0;
    00145         }
    00146     };
    00147
    00148
    00175     class _OgreExport DataStream : public StreamAlloc
    00176     {
    00177     public:
    00178         enum AccessMode
    00179         {
    00180             READ = 1,
    00181             WRITE = 2
    00182         };
    00183     protected:
    00185         String mName;
    00187         size_t mSize;
    00189         uint16 mAccess;
    00190
    00191         #define OGRE_STREAM_TEMP_SIZE 128
    00192     public:
    00194         DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
    00196         DataStream(const String& name, uint16 accessMode = READ)
    00197             : mName(name), mSize(0), mAccess(accessMode) {}
    00199         const String& getName(void) { return mName; }
    00201         uint16 getAccessMode() const { return mAccess; }
    00203         virtual bool isReadable() const { return (mAccess & READ) != 0; }
    00205         virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
    00206         virtual ~DataStream() {}
    00207         // Streaming operators
    00208         template<typename T> DataStream& operator>>(T& val);
    00215         virtual size_t read(void* buf, size_t count) = 0;
    00222         virtual size_t write(const void* buf, size_t count)
    00223         {
    00224                         (void)buf;
    00225                         (void)count;
    00226             // default to not supported
    00227             return 0;
    00228         }
    00229
    00244         virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00245
    00260         virtual String getLine( bool trimAfter = true );
    00261
    00267         virtual String getAsString(void);
    00268
    00276         virtual size_t skipLine(const String& delim = "\n");
    00277
    00280         virtual void skip(long count) = 0;
    00281
    00284         virtual void seek( size_t pos ) = 0;
    00285
    00287         virtual size_t tell(void) const = 0;
    00288
    00291         virtual bool eof(void) const = 0;
    00292
    00296         size_t size(void) const { return mSize; }
    00297
    00299         virtual void close(void) = 0;
    00300
    00301
    00302     };
    00303
    00307     typedef SharedPtr<DataStream> DataStreamPtr;
    00308
    00310     typedef list<DataStreamPtr>::type DataStreamList;
    00312     typedef SharedPtr<DataStreamList> DataStreamListPtr;
    00313
    00316     class _OgreExport MemoryDataStream : public DataStream
    00317     {
    00318     protected:
    00320         uchar* mData;
    00322         uchar* mPos;
    00324         uchar* mEnd;
    00326         bool mFreeOnClose;
    00327     public:
    00328
    00339         MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
    00340
    00352         MemoryDataStream(const String& name, void* pMem, size_t size,
    00353                 bool freeOnClose = false, bool readOnly = false);
    00354
    00366         MemoryDataStream(DataStream& sourceStream,
    00367                 bool freeOnClose = true, bool readOnly = false);
    00368
    00380         MemoryDataStream(DataStreamPtr& sourceStream,
    00381                 bool freeOnClose = true, bool readOnly = false);
    00382
    00396         MemoryDataStream(const String& name, DataStream& sourceStream,
    00397                 bool freeOnClose = true, bool readOnly = false);
    00398
    00412         MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
    00413             bool freeOnClose = true, bool readOnly = false);
    00414
    00421         MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
    00429         MemoryDataStream(const String& name, size_t size,
    00430                 bool freeOnClose = true, bool readOnly = false);
    00431
    00432         ~MemoryDataStream();
    00433
    00435         uchar* getPtr(void) { return mData; }
    00436
    00438         uchar* getCurrentPtr(void) { return mPos; }
    00439
    00442         size_t read(void* buf, size_t count);
    00443
    00446         size_t write(const void* buf, size_t count);
    00447
    00450         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00451
    00454         size_t skipLine(const String& delim = "\n");
    00455
    00458         void skip(long count);
    00459
    00462         void seek( size_t pos );
    00463
    00466         size_t tell(void) const;
    00467
    00470         bool eof(void) const;
    00471
    00474         void close(void);
    00475
    00477         void setFreeOnClose(bool free) { mFreeOnClose = free; }
    00478     };
    00479
    00483     typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
    00484
    00488     class _OgreExport FileStreamDataStream : public DataStream
    00489     {
    00490     protected:
    00492         std::istream* mInStream;
    00494         std::ifstream* mFStreamRO;
    00496         std::fstream* mFStream;
    00497         bool mFreeOnClose;
    00498
    00499         void determineAccess();
    00500     public:
    00506         FileStreamDataStream(std::ifstream* s,
    00507             bool freeOnClose = true);
    00513         FileStreamDataStream(std::fstream* s,
    00514             bool freeOnClose = true);
    00515
    00522         FileStreamDataStream(const String& name,
    00523             std::ifstream* s,
    00524             bool freeOnClose = true);
    00525
    00532         FileStreamDataStream(const String& name,
    00533             std::fstream* s,
    00534             bool freeOnClose = true);
    00535
    00550         FileStreamDataStream(const String& name,
    00551             std::ifstream* s,
    00552             size_t size,
    00553             bool freeOnClose = true);
    00554
    00569         FileStreamDataStream(const String& name,
    00570             std::fstream* s,
    00571             size_t size,
    00572             bool freeOnClose = true);
    00573
    00574         ~FileStreamDataStream();
    00575
    00578         size_t read(void* buf, size_t count);
    00579
    00582         size_t write(const void* buf, size_t count);
    00583
    00586         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
    00587
    00590         void skip(long count);
    00591
    00594         void seek( size_t pos );
    00595
    00598         size_t tell(void) const;
    00599
    00602         bool eof(void) const;
    00603
    00606         void close(void);
    00607
    00608
    00609     };
    00610
    00620     class _OgreExport FileHandleDataStream : public DataStream
    00621     {
    00622     protected:
    00623         FILE* mFileHandle;
    00624     public:
    00626         FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
    00628         FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
    00629         ~FileHandleDataStream();
    00630
    00633         size_t read(void* buf, size_t count);
    00634
    00637         size_t write(const void* buf, size_t count);
    00638
    00641         void skip(long count);
    00642
    00645         void seek( size_t pos );
    00646
    00649         size_t tell(void) const;
    00650
    00653         bool eof(void) const;
    00654
    00657         void close(void);
    00658
    00659     };
    00662 }
    00663 #endif
    00664

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:21