VectorBuffer.cpp 4.3 KB

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