nStream.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 = dVsprintf(buffer, sizeof(buffer), format, args);
  72. return write( length, buffer );
  73. }
  74. void Stream::readString(char buf[256])
  75. {
  76. U8 len;
  77. read(&len);
  78. read(S32(len), buf);
  79. buf[len] = 0;
  80. }
  81. const char *Stream::readSTString(bool casesens)
  82. {
  83. char buf[256];
  84. readString(buf);
  85. return StringTable->insert(buf, casesens);
  86. }
  87. void Stream::readLongString(U32 maxStringLen, char *stringBuf)
  88. {
  89. U32 len;
  90. read(&len);
  91. if(len > maxStringLen)
  92. {
  93. m_streamStatus = IOError;
  94. return;
  95. }
  96. read(len, stringBuf);
  97. stringBuf[len] = 0;
  98. }
  99. void Stream::writeLongString(U32 maxStringLen, const char *string)
  100. {
  101. U32 len = dStrlen(string);
  102. if(len > maxStringLen)
  103. len = maxStringLen;
  104. write(len);
  105. write(len, string);
  106. }
  107. void Stream::readLine(U8 *buffer, U32 bufferSize)
  108. {
  109. bufferSize--; // account for NULL terminator
  110. U8 *buff = buffer;
  111. U8 *buffEnd = buff + bufferSize;
  112. *buff = '\r';
  113. // strip off preceding white space
  114. while ( *buff == '\r' )
  115. {
  116. if ( !read(buff) || *buff == '\n' )
  117. {
  118. *buff = 0;
  119. return;
  120. }
  121. }
  122. // read line
  123. while ( buff != buffEnd && read(++buff) && *buff != '\n' )
  124. {
  125. if ( *buff == '\r' )
  126. {
  127. #if defined(TORQUE_OS_OSX)
  128. U32 pushPos = getPosition(); // in case we need to back up.
  129. if (read(buff)) // feeling free to overwrite the \r as the NULL below will overwrite again...
  130. if (*buff != '\n') // then push our position back.
  131. setPosition(pushPos);
  132. break; // we're always done after seeing the CR...
  133. #else
  134. buff--; // 'erases' the CR of a CRLF
  135. #endif
  136. }
  137. }
  138. *buff = 0;
  139. }
  140. void Stream::writeLine(U8 *buffer)
  141. {
  142. write(dStrlen((const char*)buffer), buffer);
  143. write(2, "\r\n");
  144. }
  145. bool Stream::write(const ColorI& rColor)
  146. {
  147. bool success = write(rColor.red);
  148. success |= write(rColor.green);
  149. success |= write(rColor.blue);
  150. success |= write(rColor.alpha);
  151. return success;
  152. }
  153. bool Stream::write(const ColorF& rColor)
  154. {
  155. ColorI temp = rColor;
  156. return write(temp);
  157. }
  158. bool Stream::read(ColorI* pColor)
  159. {
  160. bool success = read(&pColor->red);
  161. success |= read(&pColor->green);
  162. success |= read(&pColor->blue);
  163. success |= read(&pColor->alpha);
  164. return success;
  165. }
  166. bool Stream::read(ColorF* pColor)
  167. {
  168. ColorI temp;
  169. bool success = read(&temp);
  170. *pColor = temp;
  171. return success;
  172. }
  173. bool Stream::copyFrom(Stream *other)
  174. {
  175. U8 buffer[1024];
  176. U32 numBytes = other->getStreamSize() - other->getPosition();
  177. while((other->getStatus() != Stream::EOS) && numBytes > 0)
  178. {
  179. U32 numRead = numBytes > sizeof(buffer) ? sizeof(buffer) : numBytes;
  180. if(! other->read(numRead, buffer))
  181. return false;
  182. if(! write(numRead, buffer))
  183. return false;
  184. numBytes -= numRead;
  185. }
  186. return true;
  187. }