OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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-2014 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 "OgreSharedPtr.h"
33 #include <istream>
34 #include "OgreHeaderPrefix.h"
35 
36 namespace Ogre {
37 
38  typedef _StringBase String;
39 
42  template <size_t cacheSize>
44  {
45  protected:
47  char mBuffer[cacheSize];
48 
50  size_t mValidBytes;
52  size_t mPos;
53 
54  public:
57  {
58  mValidBytes = 0;
59  mPos = 0;
60  memset(mBuffer, 0, cacheSize);
61  }
62 
65  size_t cacheData(const void* buf, size_t count)
66  {
67  assert(avail() == 0 && "It is assumed that you cache data only after you have read everything.");
68 
69  if (count < cacheSize)
70  {
71  // number of bytes written is less than total size of cache
72  if (count + mValidBytes <= cacheSize)
73  {
74  // just append
75  memcpy(mBuffer + mValidBytes, buf, count);
76  mValidBytes += count;
77  }
78  else
79  {
80  size_t begOff = count - (cacheSize - mValidBytes);
81  // override old cache content in the beginning
82  memmove(mBuffer, mBuffer + begOff, mValidBytes - begOff);
83  // append new data
84  memcpy(mBuffer + cacheSize - count, buf, count);
85  mValidBytes = cacheSize;
86  }
87  mPos = mValidBytes;
88  return count;
89  }
90  else
91  {
92  // discard all
93  memcpy(mBuffer, (const char*)buf + count - cacheSize, cacheSize);
94  mValidBytes = mPos = cacheSize;
95  return cacheSize;
96  }
97  }
99  size_t read(void* buf, size_t count)
100  {
101  size_t rb = avail();
102  rb = (rb < count) ? rb : count;
103  memcpy(buf, mBuffer + mPos, rb);
104  mPos += rb;
105  return rb;
106  }
107 
109  bool rewind(size_t count)
110  {
111  if (mPos < count)
112  {
113  clear();
114  return false;
115  }
116  else
117  {
118  mPos -= count;
119  return true;
120  }
121  }
123  bool ff(size_t count)
124  {
125  if (avail() < count)
126  {
127  clear();
128  return false;
129  }
130  else
131  {
132  mPos += count;
133  return true;
134  }
135  }
136 
138  size_t avail() const
139  {
140  return mValidBytes - mPos;
141  }
142 
144  void clear()
145  {
146  mValidBytes = 0;
147  mPos = 0;
148  }
149  };
150 
151 
179  {
180  public:
182  {
183  READ = 1,
184  WRITE = 2
185  };
186  protected:
190  size_t mSize;
193 
194  #define OGRE_STREAM_TEMP_SIZE 128
195  public:
197  DataStream(uint16 accessMode = READ) : mSize(0), mAccess(accessMode) {}
199  DataStream(const String& name, uint16 accessMode = READ)
200  : mName(name), mSize(0), mAccess(accessMode) {}
202  const String& getName(void) { return mName; }
204  uint16 getAccessMode() const { return mAccess; }
206  virtual bool isReadable() const { return (mAccess & READ) != 0; }
208  virtual bool isWriteable() const { return (mAccess & WRITE) != 0; }
209  virtual ~DataStream() {}
210  // Streaming operators
211  template<typename T> DataStream& operator>>(T& val);
218  virtual size_t read(void* buf, size_t count) = 0;
225  virtual size_t write(const void* buf, size_t count)
226  {
227  (void)buf;
228  (void)count;
229  // default to not supported
230  return 0;
231  }
232 
247  virtual size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
248 
263  virtual String getLine( bool trimAfter = true );
264 
270  virtual String getAsString(void);
271 
279  virtual size_t skipLine(const String& delim = "\n");
280 
283  virtual void skip(long count) = 0;
284 
287  virtual void seek( size_t pos ) = 0;
288 
290  virtual size_t tell(void) const = 0;
291 
294  virtual bool eof(void) const = 0;
295 
299  size_t size(void) const { return mSize; }
300 
302  virtual void close(void) = 0;
303 
304 
305  };
306 
311 
315  {
316  protected:
325  public:
326 
337  MemoryDataStream(void* pMem, size_t size, bool freeOnClose = false, bool readOnly = false);
338 
350  MemoryDataStream(const String& name, void* pMem, size_t size,
351  bool freeOnClose = false, bool readOnly = false);
352 
364  MemoryDataStream(DataStream& sourceStream,
365  bool freeOnClose = true, bool readOnly = false);
366 
378  MemoryDataStream(DataStreamPtr& sourceStream,
379  bool freeOnClose = true, bool readOnly = false);
380 
394  MemoryDataStream(const String& name, DataStream& sourceStream,
395  bool freeOnClose = true, bool readOnly = false);
396 
410  MemoryDataStream(const String& name, const DataStreamPtr& sourceStream,
411  bool freeOnClose = true, bool readOnly = false);
412 
419  MemoryDataStream(size_t size, bool freeOnClose = true, bool readOnly = false);
427  MemoryDataStream(const String& name, size_t size,
428  bool freeOnClose = true, bool readOnly = false);
429 
430  ~MemoryDataStream();
431 
433  uchar* getPtr(void) { return mData; }
434 
436  uchar* getCurrentPtr(void) { return mPos; }
437 
440  size_t read(void* buf, size_t count);
441 
444  size_t write(const void* buf, size_t count);
445 
448  size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
449 
452  size_t skipLine(const String& delim = "\n");
453 
456  void skip(long count);
457 
460  void seek( size_t pos );
461 
464  size_t tell(void) const;
465 
468  bool eof(void) const;
469 
472  void close(void);
473 
475  void setFreeOnClose(bool free) { mFreeOnClose = free; }
476  };
477 
482  {
483  protected:
485  std::istream* mInStream;
487  std::ifstream* mFStreamRO;
489  std::fstream* mFStream;
491 
492  void determineAccess();
493  public:
499  FileStreamDataStream(std::ifstream* s,
500  bool freeOnClose = true);
506  FileStreamDataStream(std::fstream* s,
507  bool freeOnClose = true);
508 
515  FileStreamDataStream(const String& name,
516  std::ifstream* s,
517  bool freeOnClose = true);
518 
525  FileStreamDataStream(const String& name,
526  std::fstream* s,
527  bool freeOnClose = true);
528 
543  FileStreamDataStream(const String& name,
544  std::ifstream* s,
545  size_t size,
546  bool freeOnClose = true);
547 
562  FileStreamDataStream(const String& name,
563  std::fstream* s,
564  size_t size,
565  bool freeOnClose = true);
566 
568 
571  size_t read(void* buf, size_t count);
572 
575  size_t write(const void* buf, size_t count);
576 
579  size_t readLine(char* buf, size_t maxCount, const String& delim = "\n");
580 
583  void skip(long count);
584 
587  void seek( size_t pos );
588 
591  size_t tell(void) const;
592 
595  bool eof(void) const;
596 
599  void close(void);
600 
601 
602  };
603 
614  {
615  protected:
616  FILE* mFileHandle;
617  public:
619  FileHandleDataStream(FILE* handle, uint16 accessMode = READ);
621  FileHandleDataStream(const String& name, FILE* handle, uint16 accessMode = READ);
623 
626  size_t read(void* buf, size_t count);
627 
630  size_t write(const void* buf, size_t count);
631 
634  void skip(long count);
635 
638  void seek( size_t pos );
639 
642  size_t tell(void) const;
643 
646  bool eof(void) const;
647 
650  void close(void);
651 
652  };
655 }
656 
657 #include "OgreHeaderSuffix.h"
658 
659 #endif
660 
list< DataStreamPtr >::type DataStreamList
List of DataStream items.
char mBuffer[cacheSize]
Static buffer.
DataStream(uint16 accessMode=READ)
Constructor for creating unnamed streams.
#define _OgreExport
Definition: OgrePlatform.h:255
virtual ~DataStream()
std::string _StringBase
DataStream(const String &name, uint16 accessMode=READ)
Constructor for creating named streams.
Common subclass of DataStream for handling data from std::basic_istream.
std::istream * mInStream
Reference to source stream (read)
size_t cacheData(const void *buf, size_t count)
Cache data pointed by 'buf'.
virtual bool isReadable() const
Reports whether this stream is readable.
std::list< T, A > type
uchar * mData
Pointer to the start of the data area.
Template version of cache based on static array.
bool mFreeOnClose
Do we delete the memory on close.
bool rewind(size_t count)
Step back in cached stream by 'count' bytes.
const String & getName(void)
Returns the name of the stream, if it has one.
Common subclass of DataStream for handling data from chunks of memory.
size_t avail() const
Returns number of bytes available for reading in cache after rewinding.
uchar * getCurrentPtr(void)
Get a pointer to the current position in the memory block this stream holds.
Common subclass of DataStream for handling data from C-style file handles.
uint16 getAccessMode() const
Gets the access mode of the stream.
unsigned char uchar
In order to avoid finger-aches :)
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...
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
String mName
The name (e.g. resource name) that can be used to identify the source for this data (optional) ...
uchar * getPtr(void)
Get a pointer to the start of the memory block this stream holds.
void clear()
Clear the cache.
virtual bool isWriteable() const
Reports whether this stream is writeable.
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...
std::ifstream * mFStreamRO
Reference to source file stream (read-only)
uchar * mEnd
Pointer to the end of the memory.
bool ff(size_t count)
Step forward in cached stream by 'count' bytes.
std::fstream * mFStream
Reference to source file stream (read-write)
SharedPtr< DataStreamList > DataStreamListPtr
Shared pointer to list of DataStream items.
size_t read(void *buf, size_t count)
Read data from cache to 'buf' (maximum 'count' bytes).
General purpose class used for encapsulating the reading and writing of data.
Reference-counted shared pointer, used for objects where implicit destruction is required.
_StringBase String
Definition: OgreCommon.h:53
unsigned short uint16
Definition: OgrePlatform.h:421
void setFreeOnClose(bool free)
Sets whether or not to free the encapsulated memory on close.
uint16 mAccess
What type of access is allowed (AccessMode)
uchar * mPos
Pointer to the current position in the memory.
StaticCache()
Constructor.
size_t mValidBytes
Number of bytes valid in cache (written from the beginning of static buffer)
size_t mPos
Current read position.
size_t mSize
Size of the data in the stream (may be 0 if size cannot be determined)