nStream.cc 5.4 KB

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