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)
70 if (count + mValidBytes <= cacheSize)
73 memcpy(mBuffer + mValidBytes, buf, count);
80 memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
82 memcpy(mBuffer + cacheSize - count, buf, count);
83 mValidBytes = cacheSize;
91 memcpy(mBuffer, (
const char*)buf + count - cacheSize, cacheSize);
92 mValidBytes = mPos = cacheSize;
97 size_t read(
void* buf,
size_t count)
100 rb = (rb < count) ? rb : count;
101 memcpy(buf, mBuffer + mPos, rb);
121 bool ff(
size_t count)
138 return mValidBytes -
mPos;
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; }
209 template<
typename T>
DataStream& operator>>(T& val);
216 virtual size_t read(
void* buf,
size_t count) = 0;
223 virtual size_t write(
const void* buf,
size_t count)
245 virtual size_t readLine(
char* buf,
size_t maxCount,
const String& delim =
"\n");
261 virtual String getLine(
bool trimAfter =
true );
268 virtual String getAsString(
void);
277 virtual size_t skipLine(
const String& delim =
"\n");
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; }
300 virtual void close(
void) = 0;
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);
422 MemoryDataStream(
size_t size,
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);
451 size_t readLine(
char* buf,
size_t maxCount,
const String& delim =
"\n");
455 size_t skipLine(
const String& delim =
"\n");
459 void skip(
long count);
463 void seek(
size_t pos );
467 size_t tell(
void)
const;
471 bool eof(
void)
const;
500 void determineAccess();
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);
587 size_t readLine(
char* buf,
size_t maxCount,
const String& delim =
"\n");
591 void skip(
long count);
595 void seek(
size_t pos );
599 size_t tell(
void)
const;
603 bool eof(
void)
const;
634 size_t read(
void* buf,
size_t count);
638 size_t write(
const void* buf,
size_t count);
642 void skip(
long count);
646 void seek(
size_t pos );
650 size_t tell(
void)
const;
654 bool eof(
void)
const;
void setFreeOnClose(bool free)
Sets whether or not to free the encapsulated memory on close.
SharedPtr< DataStreamList > DataStreamListPtr
Shared pointer to list of DataStream items.
uint16 getAccessMode() const
Gets the access mode of the stream.
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
uchar * mData
Pointer to the start of the data area.
String mName
The name (e.g. resource name) that can be used to identify the source for this data (optional) ...
std::ifstream * mFStreamRO
Reference to source file stream (read-only)
uchar * getPtr(void)
Get a pointer to the start of the memory block this stream holds.
Common subclass of DataStream for handling data from std::basic_istream.
bool ff(size_t count)
Step forward in cached stream by 'count' bytes.
unsigned char uchar
In order to avoid finger-aches :)
size_t mPos
Current read position.
size_t mSize
Size of the data in the stream (may be 0 if size cannot be determined)
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...
bool mFreeOnClose
Do we delete the memory on close.
std::istream * mInStream
Reference to source stream (read)
void clear()
Clear the cache.
size_t cacheData(const void *buf, size_t count)
Cache data pointed by 'buf'.
std::fstream * mFStream
Reference to source file stream (read-write)
Common subclass of DataStream for handling data from chunks of memory.
uchar * mPos
Pointer to the current position in the memory.
General purpose class used for encapsulating the reading and writing of data.
virtual bool isReadable() const
Reports whether this stream is readable.
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...
list< DataStreamPtr >::type DataStreamList
List of DataStream items.
uint16 mAccess
What type of access is allowed (AccessMode)
Template version of cache based on static array.
const String & getName(void)
Returns the name of the stream, if it has one.
uchar * getCurrentPtr(void)
Get a pointer to the current position in the memory block this stream holds.
DataStream(uint16 accessMode=READ)
Constructor for creating unnamed streams.
SharedPtr< DataStream > DataStreamPtr
Shared pointer to allow data streams to be passed around without worrying about deallocation.
virtual bool isWriteable() const
Reports whether this stream is writeable.
char mBuffer[cacheSize]
Static buffer.
Common subclass of DataStream for handling data from C-style file handles.
DataStream(const String &name, uint16 accessMode=READ)
Constructor for creating named streams.
StaticCache()
Constructor.
uchar * mEnd
Pointer to the end of the memory.
bool rewind(size_t count)
Step back in cached stream by 'count' bytes.
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
SharedPtr< MemoryDataStream > MemoryDataStreamPtr
Shared pointer to allow memory data streams to be passed around without worrying about deallocation...
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
size_t read(void *buf, size_t count)
Read data from cache to 'buf' (maximum 'count' bytes).