stream.h 8.7 KB

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