Go to the documentation of this file.
28 #ifndef __DataStream_H__
29 #define __DataStream_H__
41 template <
size_t cacheSize>
65 assert(
avail() == 0 &&
"It is assumed that you cache data only after you have read everything.");
67 if (count < cacheSize)
82 memcpy(
mBuffer + cacheSize - count, buf, count);
91 memcpy(
mBuffer, (
const char*)buf + count - cacheSize, cacheSize);
97 size_t read(
void* buf,
size_t count)
100 rb = (rb < count) ? rb : count;
121 bool ff(
size_t count)
192 #define OGRE_STREAM_TEMP_SIZE 128
198 : mName(name), mSize(0), mAccess(accessMode) {}
204 virtual bool isReadable()
const {
return (mAccess & READ) != 0; }
206 virtual bool isWriteable()
const {
return (mAccess & WRITE) != 0; }
216 virtual size_t read(
void* buf,
size_t count) = 0;
223 virtual size_t write(
const void* buf,
size_t count)
281 virtual void skip(
long count) = 0;
285 virtual void seek(
size_t pos ) = 0;
288 virtual size_t tell(
void)
const = 0;
292 virtual bool eof(
void)
const = 0;
297 size_t size(
void)
const {
return mSize; }
340 MemoryDataStream(
void* pMem,
size_t size,
bool freeOnClose =
false,
bool readOnly =
false);
354 bool freeOnClose =
false,
bool readOnly =
false);
368 bool freeOnClose =
true,
bool readOnly =
false);
382 bool freeOnClose =
true,
bool readOnly =
false);
398 bool freeOnClose =
true,
bool readOnly =
false);
414 bool freeOnClose =
true,
bool readOnly =
false);
431 bool freeOnClose =
true,
bool readOnly =
false);
443 size_t read(
void* buf,
size_t count);
447 size_t write(
const void* buf,
size_t count);
508 bool freeOnClose =
true);
515 bool freeOnClose =
true);
525 bool freeOnClose =
true);
535 bool freeOnClose =
true);
554 bool freeOnClose =
true);
573 bool freeOnClose =
true);
579 size_t read(
void* buf,
size_t count);
583 size_t write(
const void* buf,
size_t count);
634 size_t read(
void* buf,
size_t count);
638 size_t write(
const void* buf,
size_t count);
String mName
The name (e.g. resource name) that can be used to identify the source for this data (optional)
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...
uchar * getPtr(void)
Get a pointer to the start of the memory block this stream holds.
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
std::istream * mInStream
Reference to source stream (read)
void close(void)
Close the stream; this makes further operations invalid.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
uchar * getCurrentPtr(void)
Get a pointer to the current position in the memory block this stream holds.
FileHandleDataStream(FILE *handle, uint16 accessMode=READ)
Create stream from a C file handle.
uchar * mPos
Pointer to the current position in the memory.
void skip(long count)
Skip a defined number of bytes.
virtual size_t tell(void) const =0
Returns the current byte offset from beginning.
FileStreamDataStream(const String &name, std::ifstream *s, size_t size, bool freeOnClose=true)
Construct named read-only stream from an STL stream, and tell it the size.
void skip(long count)
Skip a defined number of bytes.
size_t readLine(char *buf, size_t maxCount, const String &delim="\n")
Get a single line from the stream.
size_t read(void *buf, size_t count)
Read the requisite number of bytes from the stream, stopping at the end of the file.
virtual bool eof(void) const =0
Returns true if the stream has reached the end.
FileStreamDataStream(std::ifstream *s, bool freeOnClose=true)
Construct a read-only stream from an STL stream.
size_t tell(void) const
Returns the current byte offset from beginning.
list< DataStreamPtr >::type DataStreamList
List of DataStream items.
size_t cacheData(const void *buf, size_t count)
Cache data pointed by 'buf'.
FileStreamDataStream(const String &name, std::fstream *s, bool freeOnClose=true)
Construct named read-write stream from an STL stream.
SharedPtr< DataStreamList > DataStreamListPtr
Shared pointer to list of DataStream items.
bool eof(void) const
Returns true if the stream has reached the end.
General purpose class used for encapsulating the reading and writing of data.
MemoryDataStream(const String &name, size_t size, bool freeOnClose=true, bool readOnly=false)
Create a named stream with a brand new empty memory chunk.
Common subclass of DataStream for handling data from chunks of memory.
uint16 mAccess
What type of access is allowed (AccessMode)
MemoryDataStream(DataStreamPtr &sourceStream, bool freeOnClose=true, bool readOnly=false)
Create a stream which pre-buffers the contents of another stream.
size_t mSize
Size of the data in the stream (may be 0 if size cannot be determined)
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...
Common subclass of DataStream for handling data from std::basic_istream.
uchar * mEnd
Pointer to the end of the memory.
MemoryDataStream(const String &name, DataStream &sourceStream, bool freeOnClose=true, bool readOnly=false)
Create a named stream which pre-buffers the contents of another stream.
virtual void skip(long count)=0
Skip a defined number of bytes.
void seek(size_t pos)
Repositions the read point to a specified byte.
MemoryDataStream(const String &name, void *pMem, size_t size, bool freeOnClose=false, bool readOnly=false)
Wrap an existing memory chunk in a named stream.
void seek(size_t pos)
Repositions the read point to a specified byte.
MemoryDataStream(size_t size, bool freeOnClose=true, bool readOnly=false)
Create a stream with a brand new empty memory chunk.
void clear()
Clear the cache.
virtual String getAsString(void)
Returns a String containing the entire stream.
size_t mPos
Current read position.
FileStreamDataStream(const String &name, std::ifstream *s, bool freeOnClose=true)
Construct named read-only stream from an STL stream.
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
virtual size_t readLine(char *buf, size_t maxCount, const String &delim="\n")
Get a single line from the stream.
size_t read(void *buf, size_t count)
Read the requisite number of bytes from the stream, stopping at the end of the file.
size_t skipLine(const String &delim="\n")
Skip a single line from the stream.
unsigned char uchar
In order to avoid finger-aches :)
DataStream(uint16 accessMode=READ)
Constructor for creating unnamed streams.
virtual bool isWriteable() const
Reports whether this stream is writeable.
virtual void seek(size_t pos)=0
Repositions the read point to a specified byte.
virtual bool isReadable() const
Reports whether this stream is readable.
virtual void close(void)=0
Close the stream; this makes further operations invalid.
void close(void)
Close the stream; this makes further operations invalid.
void close(void)
Close the stream; this makes further operations invalid.
size_t readLine(char *buf, size_t maxCount, const String &delim="\n")
Get a single line from the stream.
bool eof(void) const
Returns true if the stream has reached the end.
Common subclass of DataStream for handling data from C-style file handles.
size_t tell(void) const
Returns the current byte offset from beginning.
uint16 getAccessMode() const
Gets the access mode of the stream.
SharedPtr< MemoryDataStream > MemoryDataStreamPtr
Shared pointer to allow memory data streams to be passed around without worrying about deallocation.
const String & getName(void)
Returns the name of the stream, if it has one.
void seek(size_t pos)
Repositions the read point to a specified byte.
DataStream(const String &name, uint16 accessMode=READ)
Constructor for creating named streams.
size_t read(void *buf, size_t count)
Read the requisite number of bytes from the stream, stopping at the end of the file.
MemoryDataStream(const String &name, const DataStreamPtr &sourceStream, bool freeOnClose=true, bool readOnly=false)
Create a named stream which pre-buffers the contents of another stream.
virtual size_t skipLine(const String &delim="\n")
Skip a single line from the stream.
size_t read(void *buf, size_t count)
Read data from cache to 'buf' (maximum 'count' bytes).
FileStreamDataStream(const String &name, std::fstream *s, size_t size, bool freeOnClose=true)
Construct named read-write stream from an STL stream, and tell it the size.
std::ifstream * mFStreamRO
Reference to source file stream (read-only)
SharedPtr< DataStream > DataStreamPtr
Shared pointer to allow data streams to be passed around without worrying about deallocation.
MemoryDataStream(void *pMem, size_t size, bool freeOnClose=false, bool readOnly=false)
Wrap an existing memory chunk in a stream.
virtual String getLine(bool trimAfter=true)
Returns a String containing the next line of data, optionally trimmed for whitespace.
char mBuffer[cacheSize]
Static buffer.
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...
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...
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...
void setFreeOnClose(bool free)
Sets whether or not to free the encapsulated memory on close.
std::fstream * mFStream
Reference to source file stream (read-write)
bool rewind(size_t count)
Step back in cached stream by 'count' bytes.
Template version of cache based on static array.
DataStream & operator>>(T &val)
FileHandleDataStream(const String &name, FILE *handle, uint16 accessMode=READ)
Create named stream from a C file handle.
bool mFreeOnClose
Do we delete the memory on close.
uchar * mData
Pointer to the start of the data area.
MemoryDataStream(DataStream &sourceStream, bool freeOnClose=true, bool readOnly=false)
Create a stream which pre-buffers the contents of another stream.
void skip(long count)
Skip a defined number of bytes.
bool ff(size_t count)
Step forward in cached stream by 'count' bytes.
bool eof(void) const
Returns true if the stream has reached the end.
StaticCache()
Constructor.
virtual size_t read(void *buf, size_t count)=0
Read the requisite number of bytes from the stream, stopping at the end of the file.
FileStreamDataStream(std::fstream *s, bool freeOnClose=true)
Construct a read-write stream from an STL stream.
size_t tell(void) const
Returns the current byte offset from beginning.
Copyright © 2012 Torus Knot Software Ltd

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.