VectorBuffer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Copyright (c) 2008-2014 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 "VectorBuffer.h"
  24. #include <cstring>
  25. #include "DebugNew.h"
  26. namespace Urho3D
  27. {
  28. VectorBuffer::VectorBuffer()
  29. {
  30. }
  31. VectorBuffer::VectorBuffer(const PODVector<unsigned char>& data)
  32. {
  33. SetData(data);
  34. }
  35. VectorBuffer::VectorBuffer(const void* data, unsigned size)
  36. {
  37. SetData(data, size);
  38. }
  39. VectorBuffer::VectorBuffer(Deserializer& source, unsigned size)
  40. {
  41. SetData(source, size);
  42. }
  43. unsigned VectorBuffer::Read(void* dest, unsigned size)
  44. {
  45. if (size + position_ > size_)
  46. size = size_ - position_;
  47. if (!size)
  48. return 0;
  49. unsigned char* srcPtr = &buffer_[position_];
  50. unsigned char* destPtr = (unsigned char*)dest;
  51. position_ += size;
  52. unsigned copySize = size;
  53. while (copySize >= sizeof(unsigned))
  54. {
  55. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  56. srcPtr += sizeof(unsigned);
  57. destPtr += sizeof(unsigned);
  58. copySize -= sizeof(unsigned);
  59. }
  60. if (copySize & sizeof(unsigned short))
  61. {
  62. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  63. srcPtr += sizeof(unsigned short);
  64. destPtr += sizeof(unsigned short);
  65. }
  66. if (copySize & 1)
  67. *destPtr = *srcPtr;
  68. return size;
  69. }
  70. unsigned VectorBuffer::Seek(unsigned position)
  71. {
  72. if (position > size_)
  73. position = size_;
  74. position_ = position;
  75. return position_;
  76. }
  77. unsigned VectorBuffer::Write(const void* data, unsigned size)
  78. {
  79. if (!size)
  80. return 0;
  81. if (size + position_ > size_)
  82. {
  83. size_ = size + position_;
  84. buffer_.Resize(size_);
  85. }
  86. unsigned char* srcPtr = (unsigned char*)data;
  87. unsigned char* destPtr = &buffer_[position_];
  88. position_ += size;
  89. unsigned copySize = size;
  90. while (copySize >= sizeof(unsigned))
  91. {
  92. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  93. srcPtr += sizeof(unsigned);
  94. destPtr += sizeof(unsigned);
  95. copySize -= sizeof(unsigned);
  96. }
  97. if (copySize & sizeof(unsigned short))
  98. {
  99. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  100. srcPtr += sizeof(unsigned short);
  101. destPtr += sizeof(unsigned short);
  102. }
  103. if (copySize & 1)
  104. *destPtr = *srcPtr;
  105. return size;
  106. }
  107. void VectorBuffer::SetData(const PODVector<unsigned char>& data)
  108. {
  109. buffer_ = data;
  110. position_ = 0;
  111. size_ = data.Size();
  112. }
  113. void VectorBuffer::SetData(const void* data, unsigned size)
  114. {
  115. if (!data)
  116. size = 0;
  117. buffer_.Resize(size);
  118. if (size)
  119. memcpy(&buffer_[0], data, size);
  120. position_ = 0;
  121. size_ = size;
  122. }
  123. void VectorBuffer::SetData(Deserializer& source, unsigned size)
  124. {
  125. buffer_.Resize(size);
  126. unsigned actualSize = source.Read(&buffer_[0], size);
  127. if (actualSize != size)
  128. buffer_.Resize(actualSize);
  129. position_ = 0;
  130. size_ = actualSize;
  131. }
  132. void VectorBuffer::Clear()
  133. {
  134. buffer_.Clear();
  135. position_ = 0;
  136. size_ = 0;
  137. }
  138. void VectorBuffer::Resize(unsigned size)
  139. {
  140. buffer_.Resize(size);
  141. size_ = size;
  142. if (position_ > size_)
  143. position_ = size_;
  144. }
  145. }