stream.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //------------------------------------------------------------------------------
  57. //-------------------------------------- Base Stream class
  58. //
  59. /// Base stream class for streaming data across a specific media
  60. class Stream {
  61. // Public structs and enumerations...
  62. public:
  63. /// Status constantants for the stream
  64. enum Status {
  65. Ok = 0, ///< Ok!
  66. IOError, ///< Read or Write error
  67. EOS, ///< End of Stream reached (mostly for reads)
  68. IllegalCall, ///< An unsupported operation used. Always w/ accompanied by AssertWarn
  69. Closed, ///< Tried to operate on a closed stream (or detached filter)
  70. UnknownError ///< Catchall
  71. };
  72. enum Capability {
  73. StreamWrite = BIT(0), ///< Can this stream write?
  74. StreamRead = BIT(1), ///< Can this stream read?
  75. StreamPosition = BIT(2) ///< Can this stream position?
  76. };
  77. // Accessible only through inline accessors
  78. private:
  79. Status m_streamStatus;
  80. // Derived accessible data modifiers...
  81. protected:
  82. void setStatus(const Status in_newStatus) { m_streamStatus = in_newStatus; }
  83. public:
  84. Stream();
  85. virtual ~Stream();
  86. /// Gets the status of the stream
  87. Stream::Status getStatus() const { return m_streamStatus; }
  88. /// Gets a printable string form of the status
  89. static const char* getStatusString(const Status in_status);
  90. // Derived classes must override these...
  91. protected:
  92. virtual bool _read(const U32 in_numBytes, void* out_pBuffer) = 0;
  93. virtual bool _write(const U32 in_numBytes, const void* in_pBuffer) = 0;
  94. public:
  95. /// Checks to see if this stream has the capability of a given function
  96. virtual bool hasCapability(const Capability) const = 0;
  97. /// Gets the position in the stream
  98. virtual U32 getPosition() const = 0;
  99. /// Sets the position of the stream. Returns if the new position is valid or not
  100. virtual bool setPosition(const U32 in_newPosition) = 0;
  101. /// Gets the size of the stream
  102. virtual U32 getStreamSize() = 0;
  103. /// Reads a line from the stream.
  104. /// @param buffer buffer to be read into
  105. /// @param bufferSize max size of the buffer. Will not read more than the "bufferSize"
  106. void readLine(U8 *buffer, U32 bufferSize);
  107. /// writes a line to the stream
  108. void writeLine(U8 *buffer);
  109. /// Reads a string and inserts it into the StringTable
  110. /// @see StringTable
  111. const char *readSTString(bool casesens = false);
  112. /// Reads a string of maximum 255 characters long
  113. virtual void readString(char stringBuf[256]);
  114. /// Reads a string that could potentially be more than 255 characters long.
  115. /// @param maxStringLen Maximum length to read. If the string is longer than maxStringLen, only maxStringLen bytes will be read.
  116. /// @param stringBuf buffer where data is read into
  117. void readLongString(U32 maxStringLen, char *stringBuf);
  118. /// Writes a string to the stream. This function is slightly unstable.
  119. /// Only use this if you have a valid string that is not empty.
  120. /// writeString is safer.
  121. void writeLongString(U32 maxStringLen, const char *string);
  122. inline bool Put( char character ) { return write( character ); }
  123. /// Writes a string to the stream.
  124. virtual void writeString(const char *stringBuf, S32 maxLen=255);
  125. /// Writes a formatted buffer to the stream.
  126. /// NOTE: A maximum string length of 4K is allowed.
  127. bool writeFormattedBuffer(const char *format, ...);
  128. /// Writes a NULL terminated string buffer.
  129. bool writeStringBuffer(const char* buffer) { return write( dStrlen(buffer), buffer ); }
  130. /// Write an integral color to the stream.
  131. bool write(const ColorI&);
  132. /// Write a floating point color to the stream.
  133. bool write(const ColorF&);
  134. /// Read an integral color from the stream.
  135. bool read(ColorI*);
  136. /// Read a floating point color from the stream.
  137. bool read(ColorF*);
  138. // Overloaded write and read ops..
  139. public:
  140. bool read(const U32 in_numBytes, void* out_pBuffer) {
  141. return _read(in_numBytes, out_pBuffer);
  142. }
  143. bool write(const U32 in_numBytes, const void* in_pBuffer) {
  144. return _write(in_numBytes, in_pBuffer);
  145. }
  146. DECLARE_OVERLOADED_WRITE(S8)
  147. DECLARE_OVERLOADED_WRITE(U8)
  148. DECLARE_OVERLOADED_WRITE(UTF8)
  149. DECLARE_ENDIAN_OVERLOADED_WRITE(S16)
  150. DECLARE_ENDIAN_OVERLOADED_WRITE(S32)
  151. DECLARE_ENDIAN_OVERLOADED_WRITE(U16)
  152. DECLARE_ENDIAN_OVERLOADED_WRITE(U32)
  153. DECLARE_ENDIAN_OVERLOADED_WRITE(F32)
  154. DECLARE_ENDIAN_OVERLOADED_WRITE(F64)
  155. DECLARE_OVERLOADED_READ(S8)
  156. DECLARE_OVERLOADED_READ(U8)
  157. DECLARE_ENDIAN_OVERLOADED_READ(S16)
  158. DECLARE_ENDIAN_OVERLOADED_READ(S32)
  159. DECLARE_ENDIAN_OVERLOADED_READ(U16)
  160. DECLARE_ENDIAN_OVERLOADED_READ(U32)
  161. DECLARE_ENDIAN_OVERLOADED_READ(F32)
  162. DECLARE_ENDIAN_OVERLOADED_READ(F64)
  163. // We have to do the bool's by hand, since they are different sizes
  164. // on different compilers...
  165. //
  166. bool read(bool* out_pRead) {
  167. U8 translate;
  168. bool success = read(&translate);
  169. if (success == false)
  170. return false;
  171. *out_pRead = translate != 0;
  172. return true;
  173. }
  174. bool write(const bool& in_rWrite) {
  175. U8 translate = in_rWrite ? U8(1) : U8(0);
  176. return write(translate);
  177. }
  178. /// Copy the contents of another stream into this one
  179. bool copyFrom(Stream *other);
  180. /// Write a number of tabs to this stream
  181. void writeTabs(U32 count)
  182. {
  183. char tab[] = " ";
  184. while(count--)
  185. write(3, (void*)tab);
  186. }
  187. };
  188. #endif //_STREAM_H_