streamObject.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "streamObject_ScriptBinding.h"
  24. //////////////////////////////////////////////////////////////////////////
  25. // Constructor/Destructor
  26. //////////////////////////////////////////////////////////////////////////
  27. StreamObject::StreamObject()
  28. {
  29. mStream = NULL;
  30. }
  31. StreamObject::StreamObject(Stream *stream)
  32. {
  33. mStream = stream;
  34. }
  35. StreamObject::~StreamObject()
  36. {
  37. }
  38. IMPLEMENT_CONOBJECT(StreamObject);
  39. //////////////////////////////////////////////////////////////////////////
  40. bool StreamObject::onAdd()
  41. {
  42. if(mStream == NULL)
  43. {
  44. Con::errorf("StreamObject::onAdd - StreamObject can not be instantiated from script.");
  45. return false;
  46. }
  47. return Parent::onAdd();
  48. }
  49. //////////////////////////////////////////////////////////////////////////
  50. // Public Methods
  51. //////////////////////////////////////////////////////////////////////////
  52. const char * StreamObject::getStatus()
  53. {
  54. if(mStream == NULL)
  55. return "";
  56. switch(mStream->getStatus())
  57. {
  58. case Stream::Ok:
  59. return "Ok";
  60. case Stream::IOError:
  61. return "IOError";
  62. case Stream::EOS:
  63. return "EOS";
  64. case Stream::IllegalCall:
  65. return "IllegalCall";
  66. case Stream::Closed:
  67. return "Closed";
  68. case Stream::UnknownError:
  69. return "UnknownError";
  70. default:
  71. return "Invalid";
  72. }
  73. }
  74. //////////////////////////////////////////////////////////////////////////
  75. const char * StreamObject::readLine()
  76. {
  77. if(mStream == NULL)
  78. return NULL;
  79. char *buffer = Con::getReturnBuffer(256);
  80. mStream->readLine((U8 *)buffer, 256);
  81. return buffer;
  82. }
  83. const char * StreamObject::readString()
  84. {
  85. if(mStream == NULL)
  86. return NULL;
  87. char *buffer = Con::getReturnBuffer(256);
  88. mStream->readString(buffer);
  89. return buffer;
  90. }
  91. const char *StreamObject::readLongString(U32 maxStringLen)
  92. {
  93. if(mStream == NULL)
  94. return NULL;
  95. char *buffer = Con::getReturnBuffer(maxStringLen + 1);
  96. mStream->readLongString(maxStringLen, buffer);
  97. return buffer;
  98. }