AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzDataIO.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CZ_DATAIO_H_)
00015 #define _CZ_DATAIO_H_
00016 
00017 #include "CzUtil.h" 
00018 #include "CzString.h" 
00019 
00020 /**
00021  @addtogroup Core
00022  @{
00023  */
00024 
00025 /**
00026  @class CzDataInput
00027 
00028  @brief Provides stream like read access to a memory buffer.
00029 
00030  From the perspective of AppEasy a stream is data that can be read from or written to. When data is read from a stream, the position at which we read the next lot of data moves to the end of 
00031  the previously read data. Streams have a stream pointer / position which marks the place where new data will be read or written.
00032 
00033  AppEasy provides two simple classes that allow you to treat memory as though it was a stream. This can come in very useful when serialising binary data or parsing data located in a memory 
00034  buffer.
00035 
00036  Two classes are provided which provide support for input and output:
00037  - CzDataInput - Input stream access to a a memory buffer
00038  - CzDataOutput - Output stream access to a a memory buffer
00039 
00040  Lets take a look at an example showing how to use :
00041 
00042  @code
00043 // Create and initialise an input stream of 1024 bytes
00044 CzDataInput *stream  = new CzDataInput();
00045 stream->Init(1024);
00046 
00047 // Read some data into the stream
00048 if (!file->Read(stream->getData(), size))
00049     return -1;
00050 
00051 // Do something with the data
00052 while (!in->isEOF())
00053 {
00054     char b = getByte();
00055 }
00056  @endcode
00057 
00058  */
00059 
00060 class CzDataInput
00061 {
00062     // Properties
00063 protected:
00064     char*           Data;                   ///< Streams data
00065     int             Position;               ///< Current position in stream
00066     int             Length;                 ///< Length of stream (actual allocated memory sie is aligned to 32 bytes)
00067     bool            EndOfFile;              ///< true if end of stream reached
00068     bool            Managed;                ///< If true data buffer will not be released when object deleted
00069 public:
00070     // Properties End
00071     
00072 private:
00073 public:
00074     CzDataInput(bool managed) : Data(NULL), Position(0), Length(0), EndOfFile(false), Managed(managed) {}
00075     CzDataInput() : Data(NULL), Position(0), Length(0), EndOfFile(false), Managed(false) {}
00076     virtual ~CzDataInput() { Release(); }
00077 
00078     bool    Init(int length);
00079     virtual void    Release();
00080 
00081     char*   getData() { return Data; }
00082     void    setData(char* data);
00083     void    setData(char* data, int length);
00084     void    setManaged(bool managed)    { Managed = managed; }
00085     bool    isManaged() const           { return Managed; }
00086     int     Skip(int num_bytes);
00087     int     setPos(int pos);
00088     int     getPos() const          { return Position; }
00089     int     getLength() const       { return Length; }
00090     bool    isEOF() const           { return EndOfFile; }
00091     int     Remaining() const       { return Length - Position; }
00092     char    getByte();
00093     float   getByteAsFloat();
00094     unsigned char   getUByte();
00095     int     getUByteAsInt();
00096     float   getUByteAsFloat();
00097     int     getBytes(char* data, int count);
00098     int     getBytes(short* data, int count);
00099     int     getBytes(int* data, int count);
00100     int     getBytes(float* data, int count);
00101     int     getUBytes(unsigned char* data, int count);
00102     int     getUBytes(int* data, int count);
00103     int     getUBytes(float* data, int count);
00104     int     getChars(char* data, int count);
00105     short   getShort();
00106     short   getShortSwab();
00107     float   getShortAsFloat();
00108     int     getUShort();
00109     float   getUShortAsFloat();
00110     int     getShorts(short* data, int count);
00111     int     getShorts(int* data, int count);
00112     int     getShorts(float* data, int count);
00113     int     getUShorts(unsigned short* data, int count);
00114     int     getUShorts(int* data, int count);
00115     int     getUShorts(float* data, int count);
00116     int     getInt();
00117     int     getIntSwab();
00118     int     getInts(int* data, int count);
00119     int     getInts(float* data, int count);
00120     float   getFloat();
00121     bool    getNextString(CzString *pString);
00122     bool    getNextString(char *pString, int max_len);
00123     bool    getNextQuotedStringAsint(int *pNum);
00124     bool    getNextQuotedString(CzString *pString);
00125     bool    getNextQuotedString(char *pString, int max_len);
00126     bool    getNextOccuranceOf(char chr);
00127     int     getNextTag(char tag_start_char, char tag_end_char, int range, int &start_pos);
00128     int     getNextTagOrValue(char tag_start_char, char tag_end_char, int range, int &start_pos);
00129     int     FindString(const char* pString, int str_len) const;
00130 
00131     int     SkipToWhitespaceInTag();
00132     int     SkipToNoneWhitespaceInTag();
00133     int     SkipToCharInTag(char chr);
00134     
00135     int     getLineNumber(int pos) const;
00136     
00137     int     CalculateCheckSum();
00138     bool    StripXMLComments();
00139     bool    CountXmlEntities(int& tags, int& attributes);
00140 };
00141 
00142 /**
00143  @class CzDataOutput
00144 
00145  @brief Provides stream write like access to a memory buffer.
00146 
00147  Lets take a look at an example showing how to use this class:
00148 
00149  @code
00150 // Create and initialise an input stream of 1024 bytes
00151 CzDataOutput *stream  = new CzDataOutput();
00152 stream->Init(1024);
00153 
00154 // Write some data into the stream
00155 stream->set('a');           // Write a character
00156 stream->set(10);            // Write an integer
00157 stream->set(1.2f);          // Write a floating point value
00158 stream->set("Hello", 5);    // Write some text
00159  @endcode
00160 
00161  */
00162 
00163 class CzDataOutput
00164 {
00165 protected:
00166     // Properties
00167     char*           Data;                   ///< Streams data
00168     int             Position;               ///< Current position in stream
00169     int             Length;                 ///< Length of stream (actual allocated memory sie is aligned to 32 bytes)
00170     bool            EndOfFile;              ///< true if end of stream reached
00171     bool            Managed;                ///< If true data buffer will not be released when object deleted
00172 public:
00173     // Properties End
00174     
00175 private:
00176 public:
00177     CzDataOutput(bool managed) : Data(NULL), Position(0), Length(0), EndOfFile(false), Managed(managed) {}
00178     CzDataOutput() :    Data(NULL), Position(0), Length(0), EndOfFile(false), Managed(false) {}
00179     virtual ~CzDataOutput() { Release(); }
00180 
00181     bool    Init(int length);
00182     virtual void    Release();
00183 
00184     char*   getData() { return Data; }
00185     void    setData(char* data);
00186     void    setData(char* data, int length);
00187     void    setManaged(bool managed)    { Managed = managed; }
00188     bool    isManaged() const           { return Managed; }
00189     int     Skip(int num_bytes);
00190     int     setPos(int pos);
00191     int     getPos() const { return Position; }
00192     int     getLength() const { return Length; }
00193     bool    isEOF() const { return EndOfFile; }
00194     int     Remaining() const { return Length - Position; }
00195     void    set(char data);
00196     void    set(short data);
00197     void    set(int data);
00198     void    set(float data);
00199     void    set(char* data, int count);
00200     void    set(short* data, int count);
00201     void    set(int* data, int count);
00202     void    set(float* data, int count);
00203     void    set(CzString* pString);
00204     
00205     int     CalculateCheckSum();
00206 };
00207 
00208 /// @}
00209 
00210 #endif  //_CZ_DATAIO_H_