streamObject.h

Engine/source/core/stream/streamObject.h

More...

Classes:

class

Script wrapper for the Stream class.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _STREAMOBJECT_H_
 25#define _STREAMOBJECT_H_
 26
 27#ifndef _SIMOBJECT_H_
 28#include "console/simObject.h"
 29#endif
 30
 31/// @addtogroup zip_group
 32// @{
 33
 34//-----------------------------------------------------------------------------
 35/// @brief Script wrapper for the Stream class
 36/// 
 37/// It is not possible to instantiate StreamObject in script. Instead,
 38/// it is instantiated in C++ code and returned to script.
 39/// 
 40/// This was mainly intended to allow the \ref zip_group "zip code" to
 41/// provide the stream interface to script.
 42//-----------------------------------------------------------------------------
 43class StreamObject : public SimObject
 44{
 45   typedef SimObject Parent;
 46
 47protected:
 48   Stream *mStream;
 49
 50public:
 51   StreamObject();
 52   StreamObject(Stream *stream);
 53   virtual ~StreamObject();
 54
 55   DECLARE_CONOBJECT(StreamObject);
 56
 57   virtual bool onAdd();
 58
 59   /// Set the stream to allow reuse of the object
 60   void setStream(Stream *stream)      { mStream = stream; }
 61
 62   /// Get the underlying stream. Used with setStream() to support object reuse
 63   Stream *getStream()           { return mStream; }
 64
 65   /// Gets a printable string form of the status
 66   const char* getStatus();
 67
 68   bool isEOS()                  { return mStream ? mStream->getStatus() == Stream::EOS : true; }
 69
 70   /// Gets the position in the stream
 71   U32  getPosition() const
 72   {
 73      return mStream ? mStream->getPosition() : 0;
 74   }
 75
 76   /// Sets the position of the stream.  Returns if the new position is valid or not
 77   bool setPosition(const U32 in_newPosition)
 78   {
 79      return mStream ? mStream->setPosition(in_newPosition) : false;
 80   }
 81
 82   /// Gets the size of the stream
 83   U32  getStreamSize()
 84   {
 85      return mStream ? mStream->getStreamSize() : 0;
 86   }
 87
 88   /// Reads a line from the stream.
 89   const char * readLine();
 90
 91   /// Writes a line to the stream
 92   void writeLine(U8 *buffer)
 93   {
 94      if(mStream)
 95         mStream->writeLine(buffer);
 96   }
 97
 98   /// Reads a string and inserts it into the StringTable
 99   /// @see StringTable
100   const char *readSTString(bool casesens = false)
101   {
102      return mStream ? mStream->readSTString(casesens) : NULL;
103   }
104
105   /// Reads a string of maximum 255 characters long
106   const char *readString();
107   /// Reads a string that could potentially be more than 255 characters long.
108   /// @param maxStringLen Maximum length to read.  If the string is longer than maxStringLen, only maxStringLen bytes will be read.
109   /// @param stringBuf buffer where data is read into
110   const char * readLongString(U32 maxStringLen);
111   /// Writes a string to the stream.  This function is slightly unstable.
112   /// Only use this if you have a valid string that is not empty.
113   /// writeString is safer.
114   void writeLongString(U32 maxStringLen, const char *string)
115   {
116      if(mStream)
117         mStream->writeLongString(maxStringLen, string);
118   }
119
120   /// Writes a string to the stream.
121   void writeString(const char *stringBuf, S32 maxLen=255)
122   {
123      if(mStream)
124         mStream->writeString(stringBuf, maxLen);
125   }
126
127   /// Copy the contents of another stream into this one
128   bool copyFrom(StreamObject *other)
129   {
130      if(mStream)
131         return mStream->copyFrom(other->getStream());
132
133      return false;
134   }
135};
136
137// @}
138
139#endif // _STREAMOBJECT_H_
140