stream.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #ifndef _TORQUE_TYPES_H_
  25. #include "platform/types.h"
  26. #endif
  27. #ifndef _ENDIAN_H_
  28. #include "core/util/endian.h"
  29. #endif
  30. /// @defgroup stream_overload Primitive Type Stream Operation Overloads
  31. /// These macros declare the read and write functions for all primitive types.
  32. /// @{
  33. #define DECLARE_OVERLOADED_READ(type) bool read(type* out_read);
  34. #define DECLARE_OVERLOADED_WRITE(type) bool write(type in_write);
  35. /// @}
  36. class ColorI;
  37. class ColorF;
  38. struct NetAddress;
  39. class RawData;
  40. class String;
  41. namespace Torque {
  42. class ByteBuffer;
  43. }
  44. //------------------------------------------------------------------------------
  45. //-------------------------------------- Base Stream class
  46. //
  47. /// Base stream class for streaming data across a specific media
  48. class Stream
  49. {
  50. // Public structs and enumerations...
  51. public:
  52. /// Status constants for the stream
  53. enum StreamStatus {
  54. Ok = 0, ///< Ok!
  55. IOError, ///< Read or Write error
  56. EOS, ///< End of Stream reached (mostly for reads)
  57. IllegalCall, ///< An unsupported operation used. Always w/ accompanied by AssertWarn
  58. Closed, ///< Tried to operate on a closed stream (or detached filter)
  59. UnknownError ///< Catchall
  60. };
  61. enum Capability {
  62. StreamWrite = BIT(0), ///< Can this stream write?
  63. StreamRead = BIT(1), ///< Can this stream read?
  64. StreamPosition = BIT(2) ///< Can this stream position?
  65. };
  66. // Accessible only through inline accessors
  67. private:
  68. StreamStatus m_streamStatus;
  69. // Derived accessible data modifiers...
  70. protected:
  71. void setStatus(const StreamStatus in_newStatus) { m_streamStatus = in_newStatus; }
  72. public:
  73. Stream();
  74. virtual ~Stream() {}
  75. /// Gets the status of the stream
  76. Stream::StreamStatus getStatus() const { return m_streamStatus; }
  77. /// Gets a printable string form of the status
  78. static const char* getStatusString(const StreamStatus in_status);
  79. // Derived classes must override these...
  80. protected:
  81. virtual bool _read(const U32 in_numBytes, void* out_pBuffer) = 0;
  82. virtual bool _write(const U32 in_numBytes, const void* in_pBuffer) = 0;
  83. virtual void _write(const String & str);
  84. virtual void _read(String * str);
  85. public:
  86. /// Checks to see if this stream has the capability of a given function
  87. virtual bool hasCapability(const Capability caps) const = 0;
  88. /// Gets the position in the stream
  89. virtual U32 getPosition() const = 0;
  90. /// Sets the position of the stream. Returns if the new position is valid or not
  91. virtual bool setPosition(const U32 in_newPosition) = 0;
  92. /// Gets the size of the stream
  93. virtual U32 getStreamSize() = 0;
  94. /// Reads a line from the stream.
  95. /// @param buffer buffer to be read into
  96. /// @param bufferSize max size of the buffer. Will not read more than the "bufferSize"
  97. void readLine(U8 *buffer, U32 bufferSize);
  98. /// writes a line to the stream
  99. void writeLine(const U8 *buffer);
  100. /// Reads a string and inserts it into the StringTable
  101. /// @see StringTable
  102. const char *readSTString(bool casesens = false);
  103. /// Reads a string of maximum 255 characters long
  104. virtual void readString(char stringBuf[256]);
  105. /// Reads a string that could potentially be more than 255 characters long.
  106. /// @param maxStringLen Maximum length to read. If the string is longer than maxStringLen, only maxStringLen bytes will be read.
  107. /// @param stringBuf buffer where data is read into
  108. void readLongString(U32 maxStringLen, char *stringBuf);
  109. /// Writes a string to the stream. This function is slightly unstable.
  110. /// Only use this if you have a valid string that is not empty.
  111. /// writeString is safer.
  112. void writeLongString(U32 maxStringLen, const char *string);
  113. /// Write raw text to the stream
  114. void writeText(const char *text);
  115. /// Writes a string to the stream.
  116. virtual void writeString(const char *stringBuf, S32 maxLen=255);
  117. // read/write real strings
  118. void write(const String & str) { _write(str); }
  119. void read(String * str) { _read(str); }
  120. /// Write an integral color to the stream.
  121. bool write(const ColorI&);
  122. /// Write a floating point color to the stream.
  123. bool write(const ColorF&);
  124. /// Read an integral color from the stream.
  125. bool read(ColorI*);
  126. /// Read a floating point color from the stream.
  127. bool read(ColorF*);
  128. /// Write a network address to the stream.
  129. bool write(const NetAddress &);
  130. /// Read a network address from the stream.
  131. bool read(NetAddress*);
  132. /// Write some raw data onto the stream.
  133. bool write(const RawData &);
  134. /// Read some raw data from the stream.
  135. bool read(RawData *);
  136. /// Write some raw data onto the stream.
  137. bool write(const Torque::ByteBuffer &);
  138. /// Read some raw data from the stream.
  139. bool read(Torque::ByteBuffer *);
  140. // Overloaded write and read ops..
  141. public:
  142. bool read(const U32 in_numBytes, void* out_pBuffer) {
  143. return _read(in_numBytes, out_pBuffer);
  144. }
  145. bool write(const U32 in_numBytes, const void* in_pBuffer) {
  146. return _write(in_numBytes, in_pBuffer);
  147. }
  148. DECLARE_OVERLOADED_WRITE(S8)
  149. DECLARE_OVERLOADED_WRITE(U8)
  150. DECLARE_OVERLOADED_WRITE(S16)
  151. DECLARE_OVERLOADED_WRITE(S32)
  152. DECLARE_OVERLOADED_WRITE(U16)
  153. DECLARE_OVERLOADED_WRITE(U32)
  154. DECLARE_OVERLOADED_WRITE(U64)
  155. DECLARE_OVERLOADED_WRITE(F32)
  156. DECLARE_OVERLOADED_WRITE(F64)
  157. DECLARE_OVERLOADED_READ(S8)
  158. DECLARE_OVERLOADED_READ(U8)
  159. DECLARE_OVERLOADED_READ(S16)
  160. DECLARE_OVERLOADED_READ(S32)
  161. DECLARE_OVERLOADED_READ(U16)
  162. DECLARE_OVERLOADED_READ(U32)
  163. DECLARE_OVERLOADED_READ(U64)
  164. DECLARE_OVERLOADED_READ(F32)
  165. DECLARE_OVERLOADED_READ(F64)
  166. // We have to do the bool's by hand, since they are different sizes
  167. // on different compilers...
  168. //
  169. bool read(bool* out_pRead) {
  170. U8 translate;
  171. bool success = read(&translate);
  172. if (success == false)
  173. return false;
  174. *out_pRead = translate != 0;
  175. return true;
  176. }
  177. bool write(const bool& in_rWrite) {
  178. U8 translate = in_rWrite ? U8(1) : U8(0);
  179. return write(translate);
  180. }
  181. /// Copy the contents of another stream into this one
  182. bool copyFrom(Stream *other);
  183. /// Write a number of tabs to this stream
  184. void writeTabs(U32 count)
  185. {
  186. char tab[] = " ";
  187. while(count--)
  188. write(3, (void*)tab);
  189. }
  190. /// Create an exact replica of this stream.
  191. /// Return NULL if the cloning fails.
  192. virtual Stream* clone() const;
  193. };
  194. // This interface is used to provide the amount of bytes actually written/read when using the stream as a
  195. // file. The Stream interface does not provide this information. This is a lame workaround.
  196. class IStreamByteCount
  197. {
  198. public:
  199. virtual ~IStreamByteCount() {}
  200. virtual U32 getLastBytesRead() = 0;
  201. virtual U32 getLastBytesWritten() = 0;
  202. };
  203. #endif //_STREAM_H_