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-2013 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 #include "OgreHeaderPrefix.h"
00036 
00037 namespace Ogre {
00038     
00041     template <size_t cacheSize>
00042     class StaticCache
00043     {
00044     protected:
00046         char mBuffer[cacheSize];
00047         
00049         size_t mValidBytes;
00051         size_t mPos;
00052         
00053     public:
00055         StaticCache()
00056         {
00057             mValidBytes = 0;
00058             mPos = 0;
00059         }
00060         
00063         size_t cacheData(const void* buf, size_t count)
00064         {
00065             assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
00066             
00067             if (count < cacheSize)
00068             {
00069                 // number of bytes written is less than total size of cache
00070                 if (count + mValidBytes <= cacheSize)
00071                 {
00072                     // just append
00073                     memcpy(mBuffer + mValidBytes, buf, count);
00074                     mValidBytes += count;
00075                 }
00076                 else
00077                 {
00078                     size_t begOff = count - (cacheSize - mValidBytes);
00079                     // override old cache content in the beginning
00080                     memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
00081                     // append new data
00082                     memcpy(mBuffer + cacheSize - count, buf, count);
00083                     mValidBytes = cacheSize;
00084                 }
00085                 mPos = mValidBytes;
00086                 return count;
00087             }
00088             else
00089             {
00090                 // discard all
00091                 memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
00092                 mValidBytes = mPos = cacheSize;
00093                 return cacheSize;
00094             }
00095         }
00097         size_t read(void* buf, size_t count)
00098         {
00099             size_t rb = avail();
00100             rb = (rb < count) ? rb : count;
00101             memcpy(buf, mBuffer + mPos, rb);
00102             mPos += rb;
00103             return rb;
00104         }
00105         
00107         bool rewind(size_t count)
00108         {
00109             if (mPos < count)
00110             {
00111                 clear();
00112                 return false;
00113             }
00114             else
00115             {
00116                 mPos -= count;
00117                 return true;
00118             }
00119         }
00121         bool ff(size_t count)
00122         {
00123             if (avail() < count)
00124             {
00125                 clear();
00126                 return false;
00127             }
00128             else
00129             {
00130                 mPos += count;
00131                 return true;
00132             }
00133         }
00134         
00136         size_t avail() const
00137         {
00138             return mValidBytes - mPos;
00139         }
00140         
00142         void clear()
00143         {
00144             mValidBytes = 0;
00145             mPos = 0;
00146         }
00147     };
00148     
00149     
00176     class _OgreExport DataStream : public StreamAlloc
00177     {
00178     public:
00179         enum AccessMode
00180         {
00181             READ = 1, 
00182             WRITE = 2
00183         };
00184     protected:
00186         String mName;       
00188         size_t mSize;
00190         uint16 mAccess;
00191 
00192         #define OGRE_STREAM_TEMP_SIZE 128
00193     public:
00195         DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
00197         DataStream(const String& name, uint16 accessMode = READ) 
00198             : mName(name), mSize(0), mAccess(accessMode) {}
00200         const String& getName(void) { return mName; }
00202         uint16 getAccessMode() const { return mAccess; }
00204         virtual bool isReadable() const { return (mAccess & READ) != 0; }
00206         virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
00207         virtual ~DataStream() {}
00208         // Streaming operators
00209         template<typename T> DataStream& operator>>(T& val);
00216         virtual size_t read(void* buf, size_t count) = 0;
00223         virtual size_t write(const void* buf, size_t count)
00224         {
00225                         (void)buf;
00226                         (void)count;
00227             // default to not supported
00228             return 0;
00229         }
00230 
00245         virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
00246         
00261         virtual String getLine( bool trimAfter = true );
00262 
00268         virtual String getAsString(void);
00269 
00277         virtual size_t skipLine(const String& delim = "\n");
00278 
00281         virtual void skip(long count) = 0;
00282     
00285         virtual void seek( size_t pos ) = 0;
00286         
00288         virtual size_t tell(void) const = 0;
00289 
00292         virtual bool eof(void) const = 0;
00293 
00297         size_t size(void) const { return mSize; }
00298 
00300         virtual void close(void) = 0;
00301         
00302 
00303     };
00304 
00308     typedef SharedPtr<DataStream> DataStreamPtr;
00309 
00311     typedef list<DataStreamPtr>::type DataStreamList;
00313     typedef SharedPtr<DataStreamList> DataStreamListPtr;
00314 
00317     class _OgreExport MemoryDataStream : public DataStream
00318     {
00319     protected:
00321         uchar* mData;
00323         uchar* mPos;
00325         uchar* mEnd;
00327         bool mFreeOnClose;          
00328     public:
00329         
00340         MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
00341         
00353         MemoryDataStream(const String& name, void* pMem, size_t size, 
00354                 bool freeOnClose = false, bool readOnly = false);
00355 
00367         MemoryDataStream(DataStream& sourceStream, 
00368                 bool freeOnClose = true, bool readOnly = false);
00369         
00381         MemoryDataStream(DataStreamPtr& sourceStream, 
00382                 bool freeOnClose = true, bool readOnly = false);
00383 
00397         MemoryDataStream(const String& name, DataStream& sourceStream, 
00398                 bool freeOnClose = true, bool readOnly = false);
00399 
00413         MemoryDataStream(const String& name, const DataStreamPtr& sourceStream, 
00414             bool freeOnClose = true, bool readOnly = false);
00415 
00422         MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
00430         MemoryDataStream(const String& name, size_t size, 
00431                 bool freeOnClose = true, bool readOnly = false);
00432 
00433         ~MemoryDataStream();
00434 
00436         uchar* getPtr(void) { return mData; }
00437         
00439         uchar* getCurrentPtr(void) { return mPos; }
00440         
00443         size_t read(void* buf, size_t count);
00444 
00447         size_t write(const void* buf, size_t count);
00448 
00451         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
00452         
00455         size_t skipLine(const String& delim = "\n");
00456 
00459         void skip(long count);
00460     
00463         void seek( size_t pos );
00464         
00467         size_t tell(void) const;
00468 
00471         bool eof(void) const;
00472 
00475         void close(void);
00476 
00478         void setFreeOnClose(bool free) { mFreeOnClose = free; }
00479     };
00480 
00484     typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
00485 
00489     class _OgreExport FileStreamDataStream : public DataStream
00490     {
00491     protected:
00493         std::istream* mInStream;
00495         std::ifstream* mFStreamRO;
00497         std::fstream* mFStream;
00498         bool mFreeOnClose;  
00499 
00500         void determineAccess();
00501     public:
00507         FileStreamDataStream(std::ifstream* s, 
00508             bool freeOnClose = true);
00514         FileStreamDataStream(std::fstream* s, 
00515             bool freeOnClose = true);
00516 
00523         FileStreamDataStream(const String& name, 
00524             std::ifstream* s, 
00525             bool freeOnClose = true);
00526 
00533         FileStreamDataStream(const String& name, 
00534             std::fstream* s, 
00535             bool freeOnClose = true);
00536 
00551         FileStreamDataStream(const String& name, 
00552             std::ifstream* s, 
00553             size_t size, 
00554             bool freeOnClose = true);
00555 
00570         FileStreamDataStream(const String& name, 
00571             std::fstream* s, 
00572             size_t size, 
00573             bool freeOnClose = true);
00574 
00575         ~FileStreamDataStream();
00576 
00579         size_t read(void* buf, size_t count);
00580 
00583         size_t write(const void* buf, size_t count);
00584 
00587         size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
00588         
00591         void skip(long count);
00592     
00595         void seek( size_t pos );
00596 
00599         size_t tell(void) const;
00600 
00603         bool eof(void) const;
00604 
00607         void close(void);
00608         
00609         
00610     };
00611 
00621     class _OgreExport FileHandleDataStream : public DataStream
00622     {
00623     protected:
00624         FILE* mFileHandle;
00625     public:
00627         FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
00629         FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
00630         ~FileHandleDataStream();
00631 
00634         size_t read(void* buf, size_t count);
00635 
00638         size_t write(const void* buf, size_t count);
00639 
00642         void skip(long count);
00643     
00646         void seek( size_t pos );
00647 
00650         size_t tell(void) const;
00651 
00654         bool eof(void) const;
00655 
00658         void close(void);
00659 
00660     };
00663 }
00664 
00665 #include "OgreHeaderSuffix.h"
00666 
00667 #endif
00668 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:42