memStream.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "memstream.h"
  24. MemStream::MemStream(const U32 in_bufferSize,
  25. void* io_pBuffer,
  26. const bool in_allowRead,
  27. const bool in_allowWrite)
  28. : cm_bufferSize(in_bufferSize),
  29. m_pBufferBase(io_pBuffer),
  30. m_instCaps(0),
  31. m_currentPosition(0)
  32. {
  33. AssertFatal(io_pBuffer != NULL, "Invalid buffer pointer");
  34. AssertFatal(in_bufferSize > 0, "Invalid buffer size");
  35. AssertFatal(in_allowRead || in_allowWrite, "Either write or read must be allowed");
  36. if (in_allowRead)
  37. m_instCaps |= Stream::StreamRead;
  38. if (in_allowWrite)
  39. m_instCaps |= Stream::StreamWrite;
  40. setStatus(Ok);
  41. }
  42. MemStream::~MemStream()
  43. {
  44. m_pBufferBase = NULL;
  45. m_currentPosition = 0;
  46. setStatus(Closed);
  47. }
  48. U32 MemStream::getStreamSize()
  49. {
  50. AssertFatal(getStatus() != Closed, "Stream not open, size undefined");
  51. return cm_bufferSize;
  52. }
  53. bool MemStream::hasCapability(const Capability in_cap) const
  54. {
  55. // Closed streams can't do anything
  56. //
  57. if (getStatus() == Closed)
  58. return false;
  59. U32 totalCaps = U32(Stream::StreamPosition) | m_instCaps;
  60. return (U32(in_cap) & totalCaps) != 0;
  61. }
  62. U32 MemStream::getPosition() const
  63. {
  64. AssertFatal(getStatus() != Closed, "Position of a closed stream is undefined");
  65. return m_currentPosition;
  66. }
  67. bool MemStream::setPosition(const U32 in_newPosition)
  68. {
  69. AssertFatal(getStatus() != Closed, "SetPosition of a closed stream is not allowed");
  70. AssertFatal(in_newPosition <= cm_bufferSize, "Invalid position");
  71. m_currentPosition = in_newPosition;
  72. if (m_currentPosition > cm_bufferSize) {
  73. // Never gets here in debug version, this is for the release builds...
  74. //
  75. setStatus(UnknownError);
  76. return false;
  77. } else if (m_currentPosition == cm_bufferSize) {
  78. setStatus(EOS);
  79. } else {
  80. setStatus(Ok);
  81. }
  82. return true;
  83. }
  84. bool MemStream::_read(const U32 in_numBytes, void *out_pBuffer)
  85. {
  86. AssertFatal(getStatus() != Closed, "Attempted read from a closed stream");
  87. if (in_numBytes == 0)
  88. return true;
  89. AssertFatal(out_pBuffer != NULL, "Invalid output buffer");
  90. if (hasCapability(StreamRead) == false) {
  91. AssertWarn(false, "Reading is disallowed on this stream");
  92. setStatus(IllegalCall);
  93. return false;
  94. }
  95. bool success = true;
  96. U32 actualBytes = in_numBytes;
  97. if ((m_currentPosition + in_numBytes) > cm_bufferSize) {
  98. success = false;
  99. actualBytes = cm_bufferSize - m_currentPosition;
  100. }
  101. // Obtain a current pointer, and do the copy
  102. const void* pCurrent = (const void*)((const U8*)m_pBufferBase + m_currentPosition);
  103. dMemcpy(out_pBuffer, pCurrent, actualBytes);
  104. // Advance the stream position
  105. m_currentPosition += actualBytes;
  106. if (!success)
  107. setStatus(EOS);
  108. else
  109. setStatus(Ok);
  110. return success;
  111. }
  112. bool MemStream::_write(const U32 in_numBytes, const void *in_pBuffer)
  113. {
  114. AssertFatal(getStatus() != Closed, "Attempted write to a closed stream");
  115. if (in_numBytes == 0)
  116. return true;
  117. AssertFatal(in_pBuffer != NULL, "Invalid input buffer");
  118. if (hasCapability(StreamWrite) == false) {
  119. AssertWarn(0, "Writing is disallowed on this stream");
  120. setStatus(IllegalCall);
  121. return false;
  122. }
  123. bool success = true;
  124. U32 actualBytes = in_numBytes;
  125. if ((m_currentPosition + in_numBytes) > cm_bufferSize) {
  126. success = false;
  127. actualBytes = cm_bufferSize - m_currentPosition;
  128. }
  129. // Obtain a current pointer, and do the copy
  130. void* pCurrent = (void*)((U8*)m_pBufferBase + m_currentPosition);
  131. dMemcpy(pCurrent, in_pBuffer, actualBytes);
  132. // Advance the stream position
  133. m_currentPosition += actualBytes;
  134. if (m_currentPosition == cm_bufferSize)
  135. setStatus(EOS);
  136. else
  137. setStatus(Ok);
  138. return success;
  139. }