stream.h 8.5 KB

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