OgreDataStream.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __DataStream_H__
29 #define __DataStream_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreString.h"
33 #include "OgreSharedPtr.h"
34 #include <istream>
35 #include "OgreHeaderPrefix.h"
36 
37 namespace Ogre {
38 
41  template <size_t cacheSize>
43  {
44  protected:
46  char mBuffer[cacheSize];
47 
49  size_t mValidBytes;
51  size_t mPos;
52 
53  public:
56  {
57  mValidBytes = 0;
58  mPos = 0;
59  }
60 
63  size_t cacheData(const void* buf, size_t count)
64  {
65  assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
66 
67  if (count < cacheSize)
68  {
69  // number of bytes written is less than total size of cache
70  if (count + mValidBytes <= cacheSize)
71  {
72  // just append
73  memcpy(mBuffer + mValidBytes, buf, count);
74  mValidBytes += count;
75  }
76  else
77  {
78  size_t begOff = count - (cacheSize - mValidBytes);
79  // override old cache content in the beginning
80  memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
81  // append new data
82  memcpy(mBuffer + cacheSize - count, buf, count);
83  mValidBytes = cacheSize;
84  }
85  mPos = mValidBytes;
86  return count;
87  }
88  else
89  {
90  // discard all
91  memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
92  mValidBytes = mPos = cacheSize;
93  return cacheSize;
94  }
95  }
97  size_t read(void* buf, size_t count)
98  {
99  size_t rb = avail();
100  rb = (rb < count) ? rb : count;
101  memcpy(buf, mBuffer + mPos, rb);
102  mPos += rb;
103  return rb;
104  }
105 
107  bool rewind(size_t count)
108  {
109  if (mPos < count)
110  {
111  clear();
112  return false;
113  }
114  else
115  {
116  mPos -= count;
117  return true;
118  }
119  }
121  bool ff(size_t count)
122  {
123  if (avail() < count)
124  {
125  clear();
126  return false;
127  }
128  else
129  {
130  mPos += count;
131  return true;
132  }
133  }
134 
136  size_t avail() const
137  {
138  return mValidBytes - mPos;
139  }
140 
142  void clear()
143  {
144  mValidBytes = 0;
145  mPos = 0;
146  }
147  };
148 
149 
176  class _OgreExport DataStream : public StreamAlloc
177  {
178  public:
180  {
181  READ = 1,
182  WRITE = 2
183  };
184  protected:
188  size_t mSize;
191 
192  #define OGRE_STREAM_TEMP_SIZE 128
193  public:
195  DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
197  DataStream(const String& name, uint16 accessMode = READ)
198  : mName(name), mSize(0), mAccess(accessMode) {}
200  const String& getName(void) { return mName; }
202  uint16 getAccessMode() const { return mAccess; }
204  virtual bool isReadable() const { return (mAccess & READ) != 0; }
206  virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
207  virtual ~DataStream() {}
208  // Streaming operators
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)
224  {
225  (void)buf;
226  (void)count;
227  // default to not supported
228  return 0;
229  }
230 
245  virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
246 
261  virtual String getLine( bool trimAfter = true );
262 
268  virtual String getAsString(void);
269 
277  virtual size_t skipLine(const String& delim = "\n");
278 
281  virtual void skip(long count) = 0;
282 
285  virtual void seek( size_t pos ) = 0;
286 
288  virtual size_t tell(void) const = 0;
289 
292  virtual bool eof(void) const = 0;
293 
297  size_t size(void) const { return mSize; }
298 
300  virtual void close(void) = 0;
301 
302 
303  };
304 
309 
314 
317  class _OgreExport MemoryDataStream : public DataStream
318  {
319  protected:
328  public:
329 
340  MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
341 
353  MemoryDataStream(const String& name, void* pMem, size_t size,
354  bool freeOnClose = false, bool readOnly = false);
355 
367  MemoryDataStream(DataStream& sourceStream,
368  bool freeOnClose = true, bool readOnly = false);
369 
381  MemoryDataStream(DataStreamPtr& sourceStream,
382  bool freeOnClose = true, bool readOnly = false);
383 
397  MemoryDataStream(const String& name, DataStream& sourceStream,
398  bool freeOnClose = true, bool readOnly = false);
399 
413  MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
414  bool freeOnClose = true, bool readOnly = false);
415 
422  MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
430  MemoryDataStream(const String& name, size_t size,
431  bool freeOnClose = true, bool readOnly = false);
432 
433  ~MemoryDataStream();
434 
436  uchar* getPtr(void) { return mData; }
437 
439  uchar* getCurrentPtr(void) { return mPos; }
440 
443  size_t read(void* buf, size_t count);
444 
447  size_t write(const void* buf, size_t count);
448 
451  size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
452 
455  size_t skipLine(const String& delim = "\n");
456 
459  void skip(long count);
460 
463  void seek( size_t pos );
464 
467  size_t tell(void) const;
468 
471  bool eof(void) const;
472 
475  void close(void);
476 
478  void setFreeOnClose(bool free) { mFreeOnClose = free; }
479  };
480 
485 
489  class _OgreExport FileStreamDataStream : public DataStream
490  {
491  protected:
493  std::istream* mInStream;
495  std::ifstream* mFStreamRO;
497  std::fstream* mFStream;
499 
500  void determineAccess();
501  public:
507  FileStreamDataStream(std::ifstream* s,
508  bool freeOnClose = true);
514  FileStreamDataStream(std::fstream* s,
515  bool freeOnClose = true);
516 
523  FileStreamDataStream(const String& name,
524  std::ifstream* s,
525  bool freeOnClose = true);
526 
533  FileStreamDataStream(const String& name,
534  std::fstream* s,
535  bool freeOnClose = true);
536 
551  FileStreamDataStream(const String& name,
552  std::ifstream* s,
553  size_t size,
554  bool freeOnClose = true);
555 
570  FileStreamDataStream(const String& name,
571  std::fstream* s,
572  size_t size,
573  bool freeOnClose = true);
574 
576 
579  size_t read(void* buf, size_t count);
580 
583  size_t write(const void* buf, size_t count);
584 
587  size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
588 
591  void skip(long count);
592 
595  void seek( size_t pos );
596 
599  size_t tell(void) const;
600 
603  bool eof(void) const;
604 
607  void close(void);
608 
609 
610  };
611 
621  class _OgreExport FileHandleDataStream : public DataStream
622  {
623  protected:
624  FILE* mFileHandle;
625  public:
627  FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
629  FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
631 
634  size_t read(void* buf, size_t count);
635 
638  size_t write(const void* buf, size_t count);
639 
642  void skip(long count);
643 
646  void seek( size_t pos );
647 
650  size_t tell(void) const;
651 
654  bool eof(void) const;
655 
658  void close(void);
659 
660  };
663 }
664 
665 #include "OgreHeaderSuffix.h"
666 
667 #endif
668 
OgreSharedPtr.h
OgreHeaderSuffix.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::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::MemoryDataStream::getPtr
uchar * getPtr(void)
Get a pointer to the start of the memory block this stream holds.
Definition: OgreDataStream.h:436
Ogre::StaticCache::mValidBytes
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
Definition: OgreDataStream.h:49
Ogre::FileStreamDataStream::mInStream
std::istream * mInStream
Reference to source stream (read)
Definition: OgreDataStream.h:493
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
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
Definition: OgreAndroidLogListener.h:34
Ogre::MemoryDataStream::mPos
uchar * mPos
Pointer to the current position in the memory.
Definition: OgreDataStream.h:323
Ogre::DataStreamList
list< DataStreamPtr >::type DataStreamList
List of DataStream items.
Definition: OgreDataStream.h:311
Ogre::StaticCache::cacheData
size_t cacheData(const void *buf, size_t count)
Cache data pointed by 'buf'.
Definition: OgreDataStream.h:63
Ogre::DataStreamListPtr
SharedPtr< DataStreamList > DataStreamListPtr
Shared pointer to list of DataStream items.
Definition: OgreDataStream.h:313
Ogre::DataStream
General purpose class used for encapsulating the reading and writing of data.
Definition: OgreDataStream.h:176
Ogre::FileHandleDataStream::mFileHandle
FILE * mFileHandle
Definition: OgreDataStream.h:624
Ogre::MemoryDataStream
Common subclass of DataStream for handling data from chunks of memory.
Definition: OgreDataStream.h:317
Ogre::uint16
unsigned short uint16
Definition: OgrePlatform.h:360
Ogre::DataStream::mAccess
uint16 mAccess
What type of access is allowed (AccessMode)
Definition: OgreDataStream.h:190
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::FileStreamDataStream
Common subclass of DataStream for handling data from std::basic_istream.
Definition: OgreDataStream.h:489
Ogre::MemoryDataStream::mEnd
uchar * mEnd
Pointer to the end of the memory.
Definition: OgreDataStream.h:325
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::DataStream::AccessMode
AccessMode
Definition: OgreDataStream.h:179
Ogre::StaticCache::clear
void clear()
Clear the cache.
Definition: OgreDataStream.h:142
Ogre::StaticCache::mPos
size_t mPos
Current read position.
Definition: OgreDataStream.h:51
Ogre::StaticCache::avail
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
Definition: OgreDataStream.h:136
OgreHeaderPrefix.h
Ogre::list::type
std::list< T, A > type
Definition: OgrePrerequisites.h:508
OgrePrerequisites.h
Ogre::uchar
unsigned char uchar
In order to avoid finger-aches :)
Definition: OgrePrerequisites.h:112
Ogre::DataStream::DataStream
DataStream(uint16 accessMode=READ)
Constructor for creating unnamed streams.
Definition: OgreDataStream.h:195
Ogre::DataStream::isWriteable
virtual bool isWriteable() const
Reports whether this stream is writeable.
Definition: OgreDataStream.h:206
Ogre::DataStream::isReadable
virtual bool isReadable() const
Reports whether this stream is readable.
Definition: OgreDataStream.h:204
Ogre::FileHandleDataStream
Common subclass of DataStream for handling data from C-style file handles.
Definition: OgreDataStream.h:621
Ogre::DataStream::getAccessMode
uint16 getAccessMode() const
Gets the access mode of the stream.
Definition: OgreDataStream.h:202
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::DataStream::~DataStream
virtual ~DataStream()
Definition: OgreDataStream.h:207
Ogre::SharedPtr< DataStream >
Ogre::DataStream::getName
const String & getName(void)
Returns the name of the stream, if it has one.
Definition: OgreDataStream.h:200
Ogre::DataStream::DataStream
DataStream(const String &name, uint16 accessMode=READ)
Constructor for creating named streams.
Definition: OgreDataStream.h:197
Ogre::FileStreamDataStream::mFreeOnClose
bool mFreeOnClose
Definition: OgreDataStream.h:498
Ogre::StaticCache::read
size_t read(void *buf, size_t count)
Read data from cache to 'buf' (maximum 'count' bytes).
Definition: OgreDataStream.h:97
Ogre::FileStreamDataStream::mFStreamRO
std::ifstream * mFStreamRO
Reference to source file stream (read-only)
Definition: OgreDataStream.h:495
Ogre::DataStreamPtr
SharedPtr< DataStream > DataStreamPtr
Shared pointer to allow data streams to be passed around without worrying about deallocation.
Definition: OgreDataStream.h:308
Ogre::StaticCache::mBuffer
char mBuffer[cacheSize]
Static buffer.
Definition: OgreDataStream.h:46
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::MemoryDataStream::setFreeOnClose
void setFreeOnClose(bool free)
Sets whether or not to free the encapsulated memory on close.
Definition: OgreDataStream.h:478
Ogre::FileStreamDataStream::mFStream
std::fstream * mFStream
Reference to source file stream (read-write)
Definition: OgreDataStream.h:497
Ogre::StaticCache::rewind
bool rewind(size_t count)
Step back in cached stream by 'count' bytes.
Definition: OgreDataStream.h:107
Ogre::StaticCache
Template version of cache based on static array.
Definition: OgreDataStream.h:42
OgreString.h
Ogre::MemoryDataStream::mFreeOnClose
bool mFreeOnClose
Do we delete the memory on close.
Definition: OgreDataStream.h:327
Ogre::MemoryDataStream::mData
uchar * mData
Pointer to the start of the data area.
Definition: OgreDataStream.h:321
Ogre::StaticCache::ff
bool ff(size_t count)
Step forward in cached stream by 'count' bytes.
Definition: OgreDataStream.h:121
Ogre::StaticCache::StaticCache
StaticCache()
Constructor.
Definition: OgreDataStream.h:55

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 Tue Apr 13 2021 08:53:15