VectorBuffer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "VectorBuffer.h"
  25. #include <cstring>
  26. #include "DebugNew.h"
  27. namespace Urho3D
  28. {
  29. VectorBuffer::VectorBuffer()
  30. {
  31. }
  32. VectorBuffer::VectorBuffer(const PODVector<unsigned char>& data)
  33. {
  34. SetData(data);
  35. }
  36. VectorBuffer::VectorBuffer(const void* data, unsigned size)
  37. {
  38. SetData(data, size);
  39. }
  40. VectorBuffer::VectorBuffer(Deserializer& source, unsigned size)
  41. {
  42. SetData(source, size);
  43. }
  44. unsigned VectorBuffer::Read(void* dest, unsigned size)
  45. {
  46. if (size + position_ > size_)
  47. size = size_ - position_;
  48. if (!size)
  49. return 0;
  50. unsigned char* srcPtr = &buffer_[position_];
  51. unsigned char* destPtr = (unsigned char*)dest;
  52. position_ += size;
  53. unsigned copySize = size;
  54. while (copySize >= sizeof(unsigned))
  55. {
  56. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  57. srcPtr += sizeof(unsigned);
  58. destPtr += sizeof(unsigned);
  59. copySize -= sizeof(unsigned);
  60. }
  61. if (copySize & sizeof(unsigned short))
  62. {
  63. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  64. srcPtr += sizeof(unsigned short);
  65. destPtr += sizeof(unsigned short);
  66. }
  67. if (copySize & 1)
  68. *destPtr = *srcPtr;
  69. return size;
  70. }
  71. unsigned VectorBuffer::Seek(unsigned position)
  72. {
  73. if (position > size_)
  74. position = size_;
  75. position_ = position;
  76. return position_;
  77. }
  78. unsigned VectorBuffer::Write(const void* data, unsigned size)
  79. {
  80. if (!size)
  81. return 0;
  82. if (size + position_ > size_)
  83. {
  84. size_ = size + position_;
  85. buffer_.Resize(size_);
  86. }
  87. unsigned char* srcPtr = (unsigned char*)data;
  88. unsigned char* destPtr = &buffer_[position_];
  89. position_ += size;
  90. unsigned copySize = size;
  91. while (copySize >= sizeof(unsigned))
  92. {
  93. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  94. srcPtr += sizeof(unsigned);
  95. destPtr += sizeof(unsigned);
  96. copySize -= sizeof(unsigned);
  97. }
  98. if (copySize & sizeof(unsigned short))
  99. {
  100. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  101. srcPtr += sizeof(unsigned short);
  102. destPtr += sizeof(unsigned short);
  103. }
  104. if (copySize & 1)
  105. *destPtr = *srcPtr;
  106. return size;
  107. }
  108. void VectorBuffer::SetData(const PODVector<unsigned char>& data)
  109. {
  110. buffer_ = data;
  111. position_ = 0;
  112. size_ = data.Size();
  113. }
  114. void VectorBuffer::SetData(const void* data, unsigned size)
  115. {
  116. if (!data)
  117. size = 0;
  118. buffer_.Resize(size);
  119. if (size)
  120. memcpy(&buffer_[0], data, size);
  121. position_ = 0;
  122. size_ = size;
  123. }
  124. void VectorBuffer::SetData(Deserializer& source, unsigned size)
  125. {
  126. buffer_.Resize(size);
  127. unsigned actualSize = source.Read(&buffer_[0], size);
  128. if (actualSize != size)
  129. buffer_.Resize(actualSize);
  130. position_ = 0;
  131. size_ = actualSize;
  132. }
  133. void VectorBuffer::Clear()
  134. {
  135. buffer_.Clear();
  136. position_ = 0;
  137. size_ = 0;
  138. }
  139. void VectorBuffer::Resize(unsigned size)
  140. {
  141. buffer_.Resize(size);
  142. size_ = size;
  143. if (position_ > size_)
  144. position_ = size_;
  145. }
  146. }