streamObject.h 4.6 KB

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