OgreDataStream.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __DataStream_H__
#define __DataStream_H__
#include "OgrePrerequisites.h"
#include "OgreString.h"
#include "OgreSharedPtr.h"
#include <istream>
#include "OgreHeaderPrefix.h"
namespace Ogre {
template <size_t cacheSize>
class StaticCache
{
protected:
char mBuffer[cacheSize];
size_t mValidBytes;
size_t mPos;
public:
StaticCache()
{
mValidBytes = 0;
mPos = 0;
}
size_t cacheData(const void* buf, size_t count)
{
assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
if (count < cacheSize)
{
// number of bytes written is less than total size of cache
if (count + mValidBytes <= cacheSize)
{
// just append
memcpy(mBuffer + mValidBytes, buf, count);
mValidBytes += count;
}
else
{
size_t begOff = count - (cacheSize - mValidBytes);
// override old cache content in the beginning
memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
// append new data
memcpy(mBuffer + cacheSize - count, buf, count);
mValidBytes = cacheSize;
}
mPos = mValidBytes;
return count;
}
else
{
// discard all
memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
mValidBytes = mPos = cacheSize;
return cacheSize;
}
}
size_t read(void* buf, size_t count)
{
size_t rb = avail();
rb = (rb < count) ? rb : count;
memcpy(buf, mBuffer + mPos, rb);
mPos += rb;
return rb;
}
bool rewind(size_t count)
{
if (mPos < count)
{
clear();
return false;
}
else
{
mPos -= count;
return true;
}
}
bool ff(size_t count)
{
if (avail() < count)
{
clear();
return false;
}
else
{
mPos += count;
return true;
}
}
size_t avail() const
{
return mValidBytes - mPos;
}
void clear()
{
mValidBytes = 0;
mPos = 0;
}
};
class _OgreExport DataStream : public StreamAlloc
{
public:
enum AccessMode
{
READ = 1,
WRITE = 2
};
protected:
String mName;
size_t mSize;
uint16 mAccess;
#define OGRE_STREAM_TEMP_SIZE 128
public:
DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
DataStream(const String& name, uint16 accessMode = READ)
: mName(name), mSize(0), mAccess(accessMode) {}
const String& getName(void) { return mName; }
uint16 getAccessMode() const { return mAccess; }
virtual bool isReadable() const { return (mAccess & READ) != 0; }
virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
virtual ~DataStream() {}
// Streaming operators
template<typename T> DataStream& operator>>(T& val);
virtual size_t read(void* buf, size_t count) = 0;
virtual size_t write(const void* buf, size_t count)
{
(void)buf;
(void)count;
// default to not supported
return 0;
}
virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
virtual String getLine( bool trimAfter = true );
virtual String getAsString(void);
virtual size_t skipLine(const String& delim = "\n");
virtual void skip(long count) = 0;
virtual void seek( size_t pos ) = 0;
virtual size_t tell(void) const = 0;
virtual bool eof(void) const = 0;
size_t size(void) const { return mSize; }
virtual void close(void) = 0;
};
typedef SharedPtr<DataStream> DataStreamPtr;
typedef list<DataStreamPtr>::type DataStreamList;
typedef SharedPtr<DataStreamList> DataStreamListPtr;
class _OgreExport MemoryDataStream : public DataStream
{
protected:
uchar* mData;
uchar* mPos;
uchar* mEnd;
bool mFreeOnClose;
public:
MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
MemoryDataStream(const String& name, void* pMem, size_t size,
bool freeOnClose = false, bool readOnly = false);
MemoryDataStream(DataStream& sourceStream,
bool freeOnClose = true, bool readOnly = false);
MemoryDataStream(DataStreamPtr& sourceStream,
bool freeOnClose = true, bool readOnly = false);
MemoryDataStream(const String& name, DataStream& sourceStream,
bool freeOnClose = true, bool readOnly = false);
MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
bool freeOnClose = true, bool readOnly = false);
MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
MemoryDataStream(const String& name, size_t size,
bool freeOnClose = true, bool readOnly = false);
~MemoryDataStream();
uchar* getPtr(void) { return mData; }
uchar* getCurrentPtr(void) { return mPos; }
size_t read(void* buf, size_t count);
size_t write(const void* buf, size_t count);
size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
size_t skipLine(const String& delim = "\n");
void skip(long count);
void seek( size_t pos );
size_t tell(void) const;
bool eof(void) const;
void close(void);
void setFreeOnClose(bool free) { mFreeOnClose = free; }
};
typedef SharedPtr<MemoryDataStream> MemoryDataStreamPtr;
class _OgreExport FileStreamDataStream : public DataStream
{
protected:
std::istream* mInStream;
std::ifstream* mFStreamRO;
std::fstream* mFStream;
bool mFreeOnClose;
void determineAccess();
public:
FileStreamDataStream(std::ifstream* s,
bool freeOnClose = true);
FileStreamDataStream(std::fstream* s,
bool freeOnClose = true);
FileStreamDataStream(const String& name,
std::ifstream* s,
bool freeOnClose = true);
FileStreamDataStream(const String& name,
std::fstream* s,
bool freeOnClose = true);
FileStreamDataStream(const String& name,
std::ifstream* s,
size_t size,
bool freeOnClose = true);
FileStreamDataStream(const String& name,
std::fstream* s,
size_t size,
bool freeOnClose = true);
~FileStreamDataStream();
size_t read(void* buf, size_t count);
size_t write(const void* buf, size_t count);
size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
void skip(long count);
void seek( size_t pos );
size_t tell(void) const;
bool eof(void) const;
void close(void);
};
class _OgreExport FileHandleDataStream : public DataStream
{
protected:
FILE* mFileHandle;
public:
FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
~FileHandleDataStream();
size_t read(void* buf, size_t count);
size_t write(const void* buf, size_t count);
void skip(long count);
void seek( size_t pos );
size_t tell(void) const;
bool eof(void) const;
void close(void);
};
}
#include "OgreHeaderSuffix.h"
#endif
Ogre::MemoryDataStream::setFreeOnClose
void setFreeOnClose(bool free)
Sets whether or not to free the encapsulated memory on close.
Definition: OgreDataStream.h:478
Ogre::DataStreamListPtr
SharedPtr< DataStreamList > DataStreamListPtr
Shared pointer to list of DataStream items.
Definition: OgreDataStream.h:313
Ogre::MemoryDataStream::mData
uchar * mData
Pointer to the start of the data area.
Definition: OgreDataStream.h:321
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::StaticCache::avail
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
Definition: OgreDataStream.h:136
OgreSharedPtr.h
Ogre::DataStream::mName
String mName
The name (e.g. resource name) that can be used to identify the source for this data (optional) ...
Definition: OgreDataStream.h:186
Ogre::FileStreamDataStream::mFStreamRO
std::ifstream * mFStreamRO
Reference to source file stream (read-only)
Definition: OgreDataStream.h:495
Ogre::MemoryDataStream::getPtr
uchar * getPtr(void)
Get a pointer to the start of the memory block this stream holds.
Definition: OgreDataStream.h:436
Ogre::FileStreamDataStream
Common subclass of DataStream for handling data from std::basic_istream.
Definition: OgreDataStream.h:489
Ogre::FileStreamDataStream::mFreeOnClose
bool mFreeOnClose
Definition: OgreDataStream.h:498
OgreString.h
Ogre::DataStream::isWriteable
virtual bool isWriteable() const
Reports whether this stream is writeable.
Definition: OgreDataStream.h:206
Ogre::StaticCache::ff
bool ff(size_t count)
Step forward in cached stream by &#39;count&#39; bytes.
Definition: OgreDataStream.h:121
Ogre::uchar
unsigned char uchar
In order to avoid finger-aches :)
Definition: OgrePrerequisites.h:112
Ogre::StaticCache::mPos
size_t mPos
Current read position.
Definition: OgreDataStream.h:51
Ogre::DataStream::mSize
size_t mSize
Size of the data in the stream (may be 0 if size cannot be determined)
Definition: OgreDataStream.h:188
Ogre::MemoryDataStream::mFreeOnClose
bool mFreeOnClose
Do we delete the memory on close.
Definition: OgreDataStream.h:327
Ogre::DataStream::~DataStream
virtual ~DataStream()
Definition: OgreDataStream.h:207
Ogre::FileStreamDataStream::mInStream
std::istream * mInStream
Reference to source stream (read)
Definition: OgreDataStream.h:493
OgrePrerequisites.h
Ogre::DataStream::isReadable
virtual bool isReadable() const
Reports whether this stream is readable.
Definition: OgreDataStream.h:204
Ogre::StaticCache::clear
void clear()
Clear the cache.
Definition: OgreDataStream.h:142
Ogre::StaticCache::cacheData
size_t cacheData(const void *buf, size_t count)
Cache data pointed by &#39;buf&#39;.
Definition: OgreDataStream.h:63
Ogre::FileStreamDataStream::mFStream
std::fstream * mFStream
Reference to source file stream (read-write)
Definition: OgreDataStream.h:497
Ogre::DataStream::getAccessMode
uint16 getAccessMode() const
Gets the access mode of the stream.
Definition: OgreDataStream.h:202
Ogre::MemoryDataStream
Common subclass of DataStream for handling data from chunks of memory.
Definition: OgreDataStream.h:317
Ogre::MemoryDataStream::mPos
uchar * mPos
Pointer to the current position in the memory.
Definition: OgreDataStream.h:323
Ogre::DataStream
General purpose class used for encapsulating the reading and writing of data.
Definition: OgreDataStream.h:176
Ogre::DataStream::write
virtual size_t write(const void *buf, size_t count)
Write the requisite number of bytes from the stream (only applicable to streams that are not read-onl...
Definition: OgreDataStream.h:223
Ogre::DataStreamList
list< DataStreamPtr >::type DataStreamList
List of DataStream items.
Definition: OgreDataStream.h:311
Ogre::DataStream::mAccess
uint16 mAccess
What type of access is allowed (AccessMode)
Definition: OgreDataStream.h:190
Ogre::StaticCache
Template version of cache based on static array.
Definition: OgreDataStream.h:42
Ogre::DataStream::getName
const String & getName(void)
Returns the name of the stream, if it has one.
Definition: OgreDataStream.h:200
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::MemoryDataStream::getCurrentPtr
uchar * getCurrentPtr(void)
Get a pointer to the current position in the memory block this stream holds.
Definition: OgreDataStream.h:439
Ogre::DataStream::DataStream
DataStream(uint16 accessMode=READ)
Constructor for creating unnamed streams.
Definition: OgreDataStream.h:195
Ogre::DataStreamPtr
SharedPtr< DataStream > DataStreamPtr
Shared pointer to allow data streams to be passed around without worrying about deallocation.
Definition: OgreDataStream.h:308
Ogre::DataStream::AccessMode
AccessMode
Definition: OgreDataStream.h:179
OgreHeaderSuffix.h
Ogre::StaticCache::mBuffer
char mBuffer[cacheSize]
Static buffer.
Definition: OgreDataStream.h:46
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::FileHandleDataStream
Common subclass of DataStream for handling data from C-style file handles.
Definition: OgreDataStream.h:621
Ogre::SharedPtr< DataStream >
OgreHeaderPrefix.h
Ogre::DataStream::DataStream
DataStream(const String &name, uint16 accessMode=READ)
Constructor for creating named streams.
Definition: OgreDataStream.h:197
Ogre::StaticCache::StaticCache
StaticCache()
Constructor.
Definition: OgreDataStream.h:55
Ogre::MemoryDataStream::mEnd
uchar * mEnd
Pointer to the end of the memory.
Definition: OgreDataStream.h:325
Ogre::DataStream::size
size_t size(void) const
Returns the total size of the data to be read from the stream, or 0 if this is indeterminate for this...
Definition: OgreDataStream.h:297
Ogre::list::type
std::list< T, A > type
Definition: OgrePrerequisites.h:508
Ogre::StaticCache::rewind
bool rewind(size_t count)
Step back in cached stream by &#39;count&#39; bytes.
Definition: OgreDataStream.h:107
Ogre::StaticCache::mValidBytes
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
Definition: OgreDataStream.h:49
Ogre::FileHandleDataStream::mFileHandle
FILE * mFileHandle
Definition: OgreDataStream.h:624
Ogre::uint16
unsigned short uint16
Definition: OgrePlatform.h:360
Ogre::MemoryDataStreamPtr
SharedPtr< MemoryDataStream > MemoryDataStreamPtr
Shared pointer to allow memory data streams to be passed around without worrying about deallocation...
Definition: OgreDataStream.h:484
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
Ogre::StaticCache::read
size_t read(void *buf, size_t count)
Read data from cache to &#39;buf&#39; (maximum &#39;count&#39; bytes).
Definition: OgreDataStream.h:97

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.