MemoryBuffer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/MemoryBuffer.h"
  24. namespace Urho3D
  25. {
  26. MemoryBuffer::MemoryBuffer(void* data, unsigned size) :
  27. AbstractFile(size),
  28. buffer_((unsigned char*)data),
  29. readOnly_(false)
  30. {
  31. if (!buffer_)
  32. size_ = 0;
  33. }
  34. MemoryBuffer::MemoryBuffer(const void* data, unsigned size) :
  35. AbstractFile(size),
  36. buffer_((unsigned char*)data),
  37. readOnly_(true)
  38. {
  39. if (!buffer_)
  40. size_ = 0;
  41. }
  42. MemoryBuffer::MemoryBuffer(PODVector<unsigned char>& data) :
  43. AbstractFile(data.Size()),
  44. buffer_(data.Begin().ptr_),
  45. readOnly_(false)
  46. {
  47. }
  48. MemoryBuffer::MemoryBuffer(const PODVector<unsigned char>& data) :
  49. AbstractFile(data.Size()),
  50. buffer_(const_cast<unsigned char*>(data.Begin().ptr_)),
  51. readOnly_(true)
  52. {
  53. }
  54. unsigned MemoryBuffer::Read(void* dest, unsigned size)
  55. {
  56. if (size + position_ > size_)
  57. size = size_ - position_;
  58. if (!size)
  59. return 0;
  60. unsigned char* srcPtr = &buffer_[position_];
  61. auto* destPtr = (unsigned char*)dest;
  62. position_ += size;
  63. memcpy(destPtr, srcPtr, size);
  64. return size;
  65. }
  66. unsigned MemoryBuffer::Seek(unsigned position)
  67. {
  68. if (position > size_)
  69. position = size_;
  70. position_ = position;
  71. return position_;
  72. }
  73. unsigned MemoryBuffer::Write(const void* data, unsigned size)
  74. {
  75. if (size + position_ > size_)
  76. size = size_ - position_;
  77. if (!size)
  78. return 0;
  79. auto* srcPtr = (unsigned char*)data;
  80. unsigned char* destPtr = &buffer_[position_];
  81. position_ += size;
  82. memcpy(destPtr, srcPtr, size);
  83. return size;
  84. }
  85. }