stream.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _STREAM_H_
  23. #define _STREAM_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. // This should ideally be done with templates...
  29. //
  30. /// @defgroup stream_overload Primitive Type Stream Operation Overloads
  31. /// These macros define the read and write functions for all primitive types.
  32. /// @{
  33. #define DECLARE_OVERLOADED_READ(type) \
  34. bool read(type* out_read) { \
  35. return read(sizeof(type), out_read); \
  36. }
  37. #define DECLARE_OVERLOADED_WRITE(type) \
  38. bool write(type in_write) { \
  39. return write(sizeof(type), &in_write); \
  40. }
  41. #define DECLARE_ENDIAN_OVERLOADED_READ(type) \
  42. bool read(type* out_read) { \
  43. type temp; \
  44. bool success = read(sizeof(type), &temp); \
  45. *out_read = convertLEndianToHost(temp); \
  46. return success; \
  47. }
  48. #define DECLARE_ENDIAN_OVERLOADED_WRITE(type) \
  49. bool write(type in_write) { \
  50. type temp = convertHostToLEndian(in_write); \
  51. return write(sizeof(type), &temp); \
  52. }
  53. /// @}
  54. class ColorI;
  55. class ColorF;
  56. struct NetAddress;
  57. class RawData;
  58. // class String;
  59. class NetSocket;
  60. //------------------------------------------------------------------------------
  61. //-------------------------------------- Base Stream class
  62. //
  63. /// Base stream class for streaming data across a specific media
  64. class Stream {
  65. // Public structs and enumerations...
  66. public:
  67. /// Status constantants for the stream
  68. enum Status {
  69. Ok = 0, ///< Ok!
  70. IOError, ///< Read or Write error
  71. EOS, ///< End of Stream reached (mostly for reads)
  72. IllegalCall, ///< An unsupported operation used. Always w/ accompanied by AssertWarn
  73. Closed, ///< Tried to operate on a closed stream (or detached filter)
  74. UnknownError ///< Catchall
  75. };
  76. enum Capability {
  77. StreamWrite = BIT(0), ///< Can this stream write?
  78. StreamRead = BIT(1), ///< Can this stream read?
  79. StreamPosition = BIT(2) ///< Can this stream position?
  80. };
  81. // Accessible only through inline accessors
  82. private:
  83. Status m_streamStatus;
  84. // Derived accessible data modifiers...
  85. protected:
  86. void setStatus(const Status in_newStatus) { m_streamStatus = in_newStatus; }
  87. public:
  88. Stream();
  89. virtual ~Stream();
  90. /// Gets the status of the stream
  91. Stream::Status getStatus() const { return m_streamStatus; }
  92. /// Gets a printable string form of the status
  93. static const char* getStatusString(const Status in_status);
  94. // Derived classes must override these...
  95. protected:
  96. virtual bool _read(const U32 in_numBytes, void* out_pBuffer) = 0;
  97. virtual bool _write(const U32 in_numBytes, const void* in_pBuffer) = 0;
  98. public:
  99. /// Checks to see if this stream has the capability of a given function
  100. virtual bool hasCapability(const Capability) const = 0;
  101. /// Gets the position in the stream
  102. virtual U32 getPosition() const = 0;
  103. /// Sets the position of the stream. Returns if the new position is valid or not
  104. virtual bool setPosition(const U32 in_newPosition) = 0;
  105. /// Gets the size of the stream
  106. virtual U32 getStreamSize() = 0;
  107. /// Reads a line from the stream.
  108. /// @param buffer buffer to be read into
  109. /// @param bufferSize max size of the buffer. Will not read more than the "bufferSize"
  110. void readLine(U8 *buffer, U32 bufferSize);
  111. /// writes a line to the stream
  112. void writeLine(U8 *buffer);
  113. /// Reads a string and inserts it into the StringTable
  114. /// @see StringTable
  115. const char *readSTString(bool casesens = false);
  116. /// Reads a string of maximum 255 characters long
  117. virtual void readString(char stringBuf[256]);
  118. /// Reads a string that could potentially be more than 255 characters long.
  119. /// @param maxStringLen Maximum length to read. If the string is longer than maxStringLen, only maxStringLen bytes will be read.
  120. /// @param stringBuf buffer where data is read into
  121. void readLongString(U32 maxStringLen, char *stringBuf);
  122. /// Writes a string to the stream. This function is slightly unstable.
  123. /// Only use this if you have a valid string that is not empty.
  124. /// writeString is safer.
  125. void writeLongString(U32 maxStringLen, const char *string);
  126. inline bool Put( char character ) { return write( character ); }
  127. /// Writes a string to the stream.
  128. virtual void writeString(const char *stringBuf, S32 maxLen=255);
  129. /// Writes a formatted buffer to the stream.
  130. /// NOTE: A maximum string length of 4K is allowed.
  131. bool writeFormattedBuffer(const char *format, ...);
  132. /// Writes a NULL terminated string buffer.
  133. bool writeStringBuffer(const char* buffer) { return write( dStrlen(buffer), buffer ); }
  134. /// Write an integral color to the stream.
  135. bool write(const ColorI&);
  136. /// Write a floating point color to the stream.
  137. bool write(const ColorF&);
  138. /// Read an integral color from the stream.
  139. bool read(ColorI*);
  140. /// Read a floating point color from the stream.
  141. bool read(ColorF*);
  142. /// Write a network address to the stream.
  143. bool write(const NetAddress &);
  144. /// Read a network address from the stream.
  145. bool read(NetAddress*);
  146. /// Write a network socket to the stream.
  147. bool write(const NetSocket &);
  148. /// Read a network socket from the stream.
  149. bool read(NetSocket*);
  150. // Overloaded write and read ops..
  151. public:
  152. bool read(const U32 in_numBytes, void* out_pBuffer) {
  153. return _read(in_numBytes, out_pBuffer);
  154. }
  155. bool write(const U32 in_numBytes, const void* in_pBuffer) {
  156. return _write(in_numBytes, in_pBuffer);
  157. }
  158. DECLARE_OVERLOADED_WRITE(S8)
  159. DECLARE_OVERLOADED_WRITE(U8)
  160. DECLARE_OVERLOADED_WRITE(UTF8)
  161. DECLARE_ENDIAN_OVERLOADED_WRITE(S16)
  162. DECLARE_ENDIAN_OVERLOADED_WRITE(S32)
  163. DECLARE_ENDIAN_OVERLOADED_WRITE(U16)
  164. DECLARE_ENDIAN_OVERLOADED_WRITE(U32)
  165. DECLARE_ENDIAN_OVERLOADED_WRITE(F32)
  166. DECLARE_ENDIAN_OVERLOADED_WRITE(F64)
  167. DECLARE_OVERLOADED_READ(S8)
  168. DECLARE_OVERLOADED_READ(U8)
  169. DECLARE_ENDIAN_OVERLOADED_READ(S16)
  170. DECLARE_ENDIAN_OVERLOADED_READ(S32)
  171. DECLARE_ENDIAN_OVERLOADED_READ(U16)
  172. DECLARE_ENDIAN_OVERLOADED_READ(U32)
  173. DECLARE_ENDIAN_OVERLOADED_READ(F32)
  174. DECLARE_ENDIAN_OVERLOADED_READ(F64)
  175. // We have to do the bool's by hand, since they are different sizes
  176. // on different compilers...
  177. //
  178. bool read(bool* out_pRead) {
  179. U8 translate;
  180. bool success = read(&translate);
  181. if (success == false)
  182. return false;
  183. *out_pRead = translate != 0;
  184. return true;
  185. }
  186. bool write(const bool& in_rWrite) {
  187. U8 translate = in_rWrite ? U8(1) : U8(0);
  188. return write(translate);
  189. }
  190. /// Copy the contents of another stream into this one
  191. bool copyFrom(Stream *other);
  192. /// Write a number of tabs to this stream
  193. void writeTabs(U32 count)
  194. {
  195. char tab[] = " ";
  196. while(count--)
  197. write(3, (void*)tab);
  198. }
  199. };
  200. #endif //_STREAM_H_