bufferStream.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "bufferStream.h"
  23. #include "platform/platform.h"
  24. //-----------------------------------------------------------------------------
  25. // BufferStream methods...
  26. //-----------------------------------------------------------------------------
  27. //-----------------------------------------------------------------------------
  28. BufferStream::BufferStream()
  29. {
  30. // initialize the buffer stream
  31. init();
  32. }
  33. //-----------------------------------------------------------------------------
  34. BufferStream::~BufferStream()
  35. {
  36. // make sure the file stream is closed
  37. close();
  38. }
  39. //-----------------------------------------------------------------------------
  40. void BufferStream::close() {
  41. mReadPosition = 0;//we are done reading for this go-around
  42. }
  43. void BufferStream::open() {
  44. init();
  45. }
  46. //-----------------------------------------------------------------------------
  47. bool BufferStream::_read(const U32 i_numBytes, void *o_pBuffer)
  48. {
  49. if( mReadPosition + i_numBytes > mBufferLen ) {
  50. //-Mat reading too far, error our here
  51. Platform::debugBreak();//we are about to have a buffer overrun
  52. return false;
  53. }
  54. //is off by 16 bytes the second time through (byte alignment after 0 is off?)
  55. U8 *bufferStart = &mBuffer[mReadPosition * sizeof(U8)];
  56. dMemcpy( o_pBuffer, bufferStart, i_numBytes );
  57. mReadPosition += i_numBytes;
  58. return true;
  59. }
  60. //-----------------------------------------------------------------------------
  61. bool BufferStream::_write(const U32 i_numBytes, const void *i_pBuffer)
  62. {
  63. if( (mBufferLen + i_numBytes) >= BUFFER_SIZE ) {
  64. //too many bytes, we'll have to try resizing (ugh!)
  65. return false;
  66. }
  67. //copy to "fresh" part of mBuffer
  68. U8 *bufferStart = &mBuffer[mBufferLen] ;
  69. dMemcpy( bufferStart, i_pBuffer, i_numBytes );
  70. mBufferLen += i_numBytes;
  71. return true;
  72. }
  73. //-----------------------------------------------------------------------------
  74. void BufferStream::init()
  75. {
  76. mBuffer[0] = '\0';
  77. mBufferLen = 0;
  78. mReadPosition = 0;
  79. }