nStream.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include "platform/platform.h"
  23. #include "stream.h"
  24. #include "string/stringTable.h"
  25. #include "graphics/gColor.h"
  26. #include "io/rawData.h"
  27. #include "io/byteBuffer.h"
  28. #ifndef _INC_STDARG
  29. #include <stdarg.h>
  30. #endif
  31. Stream::Stream()
  32. : m_streamStatus(Closed)
  33. {
  34. //
  35. }
  36. Stream::~Stream()
  37. {
  38. //
  39. }
  40. const char* Stream::getStatusString(const Status in_status)
  41. {
  42. switch (in_status) {
  43. case Ok:
  44. return "StreamOk";
  45. case IOError:
  46. return "StreamIOError";
  47. case EOS:
  48. return "StreamEOS";
  49. case IllegalCall:
  50. return "StreamIllegalCall";
  51. case Closed:
  52. return "StreamClosed";
  53. case UnknownError:
  54. return "StreamUnknownError";
  55. default:
  56. return "Invalid Stream::Status";
  57. }
  58. }
  59. void Stream::writeString(const char *string, S32 maxLen)
  60. {
  61. S32 len = string ? dStrlen(string) : 0;
  62. if(len > maxLen)
  63. len = maxLen;
  64. write(U8(len));
  65. if(len)
  66. write(len, string);
  67. }
  68. bool Stream::writeFormattedBuffer(const char *format, ...)
  69. {
  70. char buffer[4096];
  71. va_list args;
  72. va_start(args, format);
  73. const S32 length = vsprintf(buffer, format, args);
  74. // Sanity!
  75. AssertFatal(length <= sizeof(buffer), "writeFormattedBuffer - String format exceeded buffer size. This will cause corruption.");
  76. return write( length, buffer );
  77. }
  78. void Stream::readString(char buf[256])
  79. {
  80. U8 len;
  81. read(&len);
  82. read(S32(len), buf);
  83. buf[len] = 0;
  84. }
  85. const char *Stream::readSTString(bool casesens)
  86. {
  87. char buf[256];
  88. readString(buf);
  89. return StringTable->insert(buf, casesens);
  90. }
  91. void Stream::readLongString(U32 maxStringLen, char *stringBuf)
  92. {
  93. U32 len;
  94. read(&len);
  95. if(len > maxStringLen)
  96. {
  97. m_streamStatus = IOError;
  98. return;
  99. }
  100. read(len, stringBuf);
  101. stringBuf[len] = 0;
  102. }
  103. void Stream::writeLongString(U32 maxStringLen, const char *string)
  104. {
  105. U32 len = dStrlen(string);
  106. if(len > maxStringLen)
  107. len = maxStringLen;
  108. write(len);
  109. write(len, string);
  110. }
  111. void Stream::readLine(U8 *buffer, U32 bufferSize)
  112. {
  113. bufferSize--; // account for NULL terminator
  114. U8 *buff = buffer;
  115. U8 *buffEnd = buff + bufferSize;
  116. *buff = '\r';
  117. // strip off preceding white space
  118. while ( *buff == '\r' )
  119. {
  120. if ( !read(buff) || *buff == '\n' )
  121. {
  122. *buff = 0;
  123. return;
  124. }
  125. }
  126. // read line
  127. while ( buff != buffEnd && read(++buff) && *buff != '\n' )
  128. {
  129. if ( *buff == '\r' )
  130. {
  131. #if defined(TORQUE_OS_OSX)
  132. U32 pushPos = getPosition(); // in case we need to back up.
  133. if (read(buff)) // feeling free to overwrite the \r as the NULL below will overwrite again...
  134. if (*buff != '\n') // then push our position back.
  135. setPosition(pushPos);
  136. break; // we're always done after seeing the CR...
  137. #else
  138. buff--; // 'erases' the CR of a CRLF
  139. #endif
  140. }
  141. }
  142. *buff = 0;
  143. }
  144. void Stream::writeLine(U8 *buffer)
  145. {
  146. write(dStrlen((const char*)buffer), buffer);
  147. write(2, "\r\n");
  148. }
  149. bool Stream::write(const ColorI& rColor)
  150. {
  151. bool success = write(rColor.red);
  152. success |= write(rColor.green);
  153. success |= write(rColor.blue);
  154. success |= write(rColor.alpha);
  155. return success;
  156. }
  157. bool Stream::write(const ColorF& rColor)
  158. {
  159. ColorI temp = rColor;
  160. return write(temp);
  161. }
  162. bool Stream::read(ColorI* pColor)
  163. {
  164. bool success = read(&pColor->red);
  165. success |= read(&pColor->green);
  166. success |= read(&pColor->blue);
  167. success |= read(&pColor->alpha);
  168. return success;
  169. }
  170. bool Stream::read(ColorF* pColor)
  171. {
  172. ColorI temp;
  173. bool success = read(&temp);
  174. *pColor = temp;
  175. return success;
  176. }
  177. bool Stream::write(const NetAddress &na)
  178. {
  179. bool success = write(na.type);
  180. success &= write(na.port);
  181. success &= write(sizeof(na.address), &na.address);
  182. return success;
  183. }
  184. bool Stream::read(NetAddress *na)
  185. {
  186. bool success = read(&na->type);
  187. success &= read(&na->port);
  188. success &= read(sizeof(na->address), &na->address);
  189. return success;
  190. }
  191. bool Stream::write(const NetSocket &so)
  192. {
  193. return write(so.getHandle());
  194. }
  195. bool Stream::read(NetSocket* so)
  196. {
  197. S32 handle = -1;
  198. bool success = read(&handle);
  199. *so = NetSocket::fromHandle(handle);
  200. return success;
  201. }
  202. bool Stream::copyFrom(Stream *other)
  203. {
  204. U8 buffer[1024];
  205. U32 numBytes = other->getStreamSize() - other->getPosition();
  206. while((other->getStatus() != Stream::EOS) && numBytes > 0)
  207. {
  208. U32 numRead = numBytes > sizeof(buffer) ? sizeof(buffer) : numBytes;
  209. if(! other->read(numRead, buffer))
  210. return false;
  211. if(! write(numRead, buffer))
  212. return false;
  213. numBytes -= numRead;
  214. }
  215. return true;
  216. }