VectorBuffer.cpp 4.3 KB

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