streamObject.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #ifndef _STREAMOBJECT_H_
  23. #define _STREAMOBJECT_H_
  24. #ifndef _SIMOBJECT_H_
  25. #include "console/simObject.h"
  26. #endif
  27. /// @addtogroup zip_group
  28. // @{
  29. //-----------------------------------------------------------------------------
  30. /// @brief Script wrapper for the Stream class
  31. ///
  32. /// It is not possible to instantiate StreamObject in script. Instead,
  33. /// it is instantiated in C++ code and returned to script.
  34. ///
  35. /// This was mainly intended to allow the \ref zip_group "zip code" to
  36. /// provide the stream interface to script.
  37. //-----------------------------------------------------------------------------
  38. class StreamObject : public SimObject
  39. {
  40. typedef SimObject Parent;
  41. protected:
  42. Stream *mStream;
  43. public:
  44. StreamObject();
  45. StreamObject(Stream *stream);
  46. virtual ~StreamObject();
  47. DECLARE_CONOBJECT(StreamObject);
  48. virtual bool onAdd();
  49. /// Set the stream to allow reuse of the object
  50. void setStream(Stream *stream) { mStream = stream; }
  51. /// Get the underlying stream. Used with setStream() to support object reuse
  52. Stream *getStream() { return mStream; }
  53. /// Gets a printable string form of the status
  54. const char* getStatus();
  55. bool isEOS() { return mStream ? mStream->getStatus() == Stream::EOS : true; }
  56. /// Gets the position in the stream
  57. U32 getPosition() const
  58. {
  59. return mStream ? mStream->getPosition() : 0;
  60. }
  61. /// Sets the position of the stream. Returns if the new position is valid or not
  62. bool setPosition(const U32 in_newPosition)
  63. {
  64. return mStream ? mStream->setPosition(in_newPosition) : false;
  65. }
  66. /// Gets the size of the stream
  67. U32 getStreamSize()
  68. {
  69. return mStream ? mStream->getStreamSize() : 0;
  70. }
  71. /// Reads a line from the stream.
  72. const char * readLine();
  73. /// Writes a line to the stream
  74. void writeLine(U8 *buffer)
  75. {
  76. if(mStream)
  77. mStream->writeLine(buffer);
  78. }
  79. /// Reads a string and inserts it into the StringTable
  80. /// @see StringTable
  81. const char *readSTString(bool casesens = false)
  82. {
  83. return mStream ? mStream->readSTString(casesens) : NULL;
  84. }
  85. /// Reads a string of maximum 255 characters long
  86. const char *readString();
  87. /// Reads a string that could potentially be more than 255 characters long.
  88. /// @param maxStringLen Maximum length to read. If the string is longer than maxStringLen, only maxStringLen bytes will be read.
  89. /// @param stringBuf buffer where data is read into
  90. const char * readLongString(U32 maxStringLen);
  91. /// Writes a string to the stream. This function is slightly unstable.
  92. /// Only use this if you have a valid string that is not empty.
  93. /// writeString is safer.
  94. void writeLongString(U32 maxStringLen, const char *string)
  95. {
  96. if(mStream)
  97. mStream->writeLongString(maxStringLen, string);
  98. }
  99. /// Writes a string to the stream.
  100. void writeString(const char *stringBuf, S32 maxLen=255)
  101. {
  102. if(mStream)
  103. mStream->writeString(stringBuf, maxLen);
  104. }
  105. /// Copy the contents of another stream into this one
  106. bool copyFrom(StreamObject *other)
  107. {
  108. if(mStream)
  109. return mStream->copyFrom(other->getStream());
  110. return false;
  111. }
  112. };
  113. // @}
  114. #endif // _STREAMOBJECT_H_