MemoryBuffer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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_(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. unsigned char* destPtr = (unsigned char*)dest;
  62. position_ += size;
  63. unsigned copySize = size;
  64. while (copySize >= sizeof(unsigned))
  65. {
  66. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  67. srcPtr += sizeof(unsigned);
  68. destPtr += sizeof(unsigned);
  69. copySize -= sizeof(unsigned);
  70. }
  71. if (copySize & sizeof(unsigned short))
  72. {
  73. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  74. srcPtr += sizeof(unsigned short);
  75. destPtr += sizeof(unsigned short);
  76. }
  77. if (copySize & 1)
  78. *destPtr = *srcPtr;
  79. return size;
  80. }
  81. unsigned MemoryBuffer::Seek(unsigned position)
  82. {
  83. if (position > size_)
  84. position = size_;
  85. position_ = position;
  86. return position_;
  87. }
  88. unsigned MemoryBuffer::Write(const void* data, unsigned size)
  89. {
  90. if (size + position_ > size_)
  91. size = size_ - position_;
  92. if (!size)
  93. return 0;
  94. unsigned char* srcPtr = (unsigned char*)data;
  95. unsigned char* destPtr = &buffer_[position_];
  96. position_ += size;
  97. unsigned copySize = size;
  98. while (copySize >= sizeof(unsigned))
  99. {
  100. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  101. srcPtr += sizeof(unsigned);
  102. destPtr += sizeof(unsigned);
  103. copySize -= sizeof(unsigned);
  104. }
  105. if (copySize & sizeof(unsigned short))
  106. {
  107. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  108. srcPtr += sizeof(unsigned short);
  109. destPtr += sizeof(unsigned short);
  110. }
  111. if (copySize & 1)
  112. *destPtr = *srcPtr;
  113. return size;
  114. }
  115. }