streamObject.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "streamObject.h"
  23. //////////////////////////////////////////////////////////////////////////
  24. // Constructor/Destructor
  25. //////////////////////////////////////////////////////////////////////////
  26. StreamObject::StreamObject()
  27. {
  28. mStream = NULL;
  29. }
  30. StreamObject::StreamObject(Stream *stream)
  31. {
  32. mStream = stream;
  33. }
  34. StreamObject::~StreamObject()
  35. {
  36. }
  37. IMPLEMENT_CONOBJECT(StreamObject);
  38. //////////////////////////////////////////////////////////////////////////
  39. bool StreamObject::onAdd()
  40. {
  41. if(mStream == NULL)
  42. {
  43. Con::errorf("StreamObject::onAdd - StreamObject can not be instantiated from script.");
  44. return false;
  45. }
  46. return Parent::onAdd();
  47. }
  48. //////////////////////////////////////////////////////////////////////////
  49. // Public Methods
  50. //////////////////////////////////////////////////////////////////////////
  51. const char * StreamObject::getStatus()
  52. {
  53. if(mStream == NULL)
  54. return "";
  55. switch(mStream->getStatus())
  56. {
  57. case Stream::Ok:
  58. return "Ok";
  59. case Stream::IOError:
  60. return "IOError";
  61. case Stream::EOS:
  62. return "EOS";
  63. case Stream::IllegalCall:
  64. return "IllegalCall";
  65. case Stream::Closed:
  66. return "Closed";
  67. case Stream::UnknownError:
  68. return "UnknownError";
  69. default:
  70. return "Invalid";
  71. }
  72. }
  73. //////////////////////////////////////////////////////////////////////////
  74. const char * StreamObject::readLine()
  75. {
  76. if(mStream == NULL)
  77. return NULL;
  78. char *buffer = Con::getReturnBuffer(256);
  79. mStream->readLine((U8 *)buffer, 256);
  80. return buffer;
  81. }
  82. const char * StreamObject::readString()
  83. {
  84. if(mStream == NULL)
  85. return NULL;
  86. char *buffer = Con::getReturnBuffer(256);
  87. mStream->readString(buffer);
  88. return buffer;
  89. }
  90. const char *StreamObject::readLongString(U32 maxStringLen)
  91. {
  92. if(mStream == NULL)
  93. return NULL;
  94. char *buffer = Con::getReturnBuffer(maxStringLen + 1);
  95. mStream->readLongString(maxStringLen, buffer);
  96. return buffer;
  97. }
  98. //////////////////////////////////////////////////////////////////////////
  99. // Console Methods
  100. //////////////////////////////////////////////////////////////////////////
  101. ConsoleMethod(StreamObject, getStatus, const char *, 2, 2, "() Gets the current status of the StreamObject"
  102. "@return The current status as a string (Ok, IOError, EOS, IllegalCall, Closed, UnknownError, Invalid)")
  103. {
  104. return object->getStatus();
  105. }
  106. ConsoleMethod(StreamObject, isEOS, bool, 2, 2, "() Test for end of stream"
  107. "@return Returns true if at the end of the stream, false otherwise.")
  108. {
  109. return object->isEOS();
  110. }
  111. ConsoleMethod(StreamObject, isEOF, bool, 2, 2, "() Test for end of file stream (identical to isEOS()"
  112. "@return Returns true if at the end of the stream, false otherwise."
  113. "@sa isEOS")
  114. {
  115. return object->isEOS();
  116. }
  117. //////////////////////////////////////////////////////////////////////////
  118. ConsoleMethod(StreamObject, getPosition, S32, 2, 2, "()\n"
  119. "@return Returns the current position in the stream as an integer or zero if failed")
  120. {
  121. return object->getPosition();
  122. }
  123. ConsoleMethod(StreamObject, setPosition, bool, 3, 3, "(newPosition) Resets the current stream position\n"
  124. "@param The desired index\n"
  125. "@return Returns true if succeeded, flase otherwise")
  126. {
  127. return object->setPosition(dAtoi(argv[2]));
  128. }
  129. ConsoleMethod(StreamObject, getStreamSize, S32, 2, 2, "() Get the size of the stream"
  130. "@return The size of the stream as an integer")
  131. {
  132. return object->getStreamSize();
  133. }
  134. //////////////////////////////////////////////////////////////////////////
  135. ConsoleMethod(StreamObject, readLine, const char *, 2, 2, "() Read the stream until '\\n' or EOS\n"
  136. "@return A string containing the read line or an empty string if failed\n")
  137. {
  138. const char *str = object->readLine();
  139. return str ? str : "";
  140. }
  141. ConsoleMethod(StreamObject, writeLine, void, 3, 3, "(line) Writes a line of text to the stream buffer\n"
  142. "@param The line to write\n"
  143. "@return No return value.")
  144. {
  145. object->writeLine((U8 *)argv[2]);
  146. }
  147. //////////////////////////////////////////////////////////////////////////
  148. ConsoleMethod(StreamObject, readSTString, const char *, 2, 3, "([caseSensitive = false]) Read a String and insert it into a StringTable\n"
  149. "@param caseSensitive A boolean representing whether the parser should ignore case or not (default false)\n"
  150. "@return Returns the string, or empty string if failed\n")
  151. {
  152. const char *str = object->readSTString(argc > 2 ? dAtob(argv[2]) : false);
  153. return str ? str : "";
  154. }
  155. ConsoleMethod(StreamObject, readString, const char *, 2, 2, "() Reads a string from a stream buffer\n"
  156. "@return The string or an empty string if failed.")
  157. {
  158. const char *str = object->readString();
  159. return str ? str : "";
  160. }
  161. ConsoleMethod(StreamObject, readLongString, const char *, 3, 3, "(maxLength) Reads a string of provided length from the stream buffer.\n"
  162. "@param The maximum length to read in\n"
  163. "@return The requested string")
  164. {
  165. const char *str = object->readLongString(dAtoi(argv[2]));
  166. return str ? str : "";
  167. }
  168. ConsoleMethod(StreamObject, writeLongString, void, 4, 4, "(maxLength, string) Writes a string to buffer or provided length\n"
  169. "@param maxLength The maximum length to write\n"
  170. "@param string The string to write\n"
  171. "@return No return value")
  172. {
  173. object->writeLongString(dAtoi(argv[2]), argv[3]);
  174. }
  175. ConsoleMethod(StreamObject, writeString, void, 3, 4, "(string, [maxLength = 255]) Write a string to the stream buffer\n"
  176. "@param string The string to write\n"
  177. "@param maxLength The maximum length to write (default 255).\n"
  178. "@return No return value.")
  179. {
  180. object->writeString(argv[2], argc > 3 ? dAtoi(argv[3]) : 255);
  181. }
  182. //////////////////////////////////////////////////////////////////////////
  183. ConsoleMethod(StreamObject, copyFrom, bool, 3, 3, "(StreamObject other) Copies stream contents from current position\n"
  184. "@param other The StreamObject from which to copy\n"
  185. "@return Returns true on success, and false otherwise.")
  186. {
  187. StreamObject *other = dynamic_cast<StreamObject *>(Sim::findObject(argv[2]));
  188. if(other == NULL)
  189. return false;
  190. return object->copyFrom(other);
  191. }