VectorBuffer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Copyright (c) 2008-2020 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 Urho3D
  25. {
  26. VectorBuffer::VectorBuffer() = default;
  27. VectorBuffer::VectorBuffer(const PODVector<unsigned char>& data)
  28. {
  29. SetData(data);
  30. }
  31. VectorBuffer::VectorBuffer(const void* data, unsigned size)
  32. {
  33. SetData(data, size);
  34. }
  35. VectorBuffer::VectorBuffer(Deserializer& source, unsigned size)
  36. {
  37. SetData(source, size);
  38. }
  39. unsigned VectorBuffer::Read(void* dest, unsigned size)
  40. {
  41. if (size + position_ > size_)
  42. size = size_ - position_;
  43. if (!size)
  44. return 0;
  45. unsigned char* srcPtr = &buffer_[position_];
  46. auto* destPtr = (unsigned char*)dest;
  47. position_ += size;
  48. memcpy(destPtr, srcPtr, size);
  49. return size;
  50. }
  51. unsigned VectorBuffer::Seek(unsigned position)
  52. {
  53. if (position > size_)
  54. position = size_;
  55. position_ = position;
  56. return position_;
  57. }
  58. unsigned VectorBuffer::Write(const void* data, unsigned size)
  59. {
  60. if (!size)
  61. return 0;
  62. if (size + position_ > size_)
  63. {
  64. size_ = size + position_;
  65. buffer_.Resize(size_);
  66. }
  67. auto* srcPtr = (unsigned char*)data;
  68. unsigned char* destPtr = &buffer_[position_];
  69. position_ += size;
  70. memcpy(destPtr, srcPtr, size);
  71. return size;
  72. }
  73. void VectorBuffer::SetData(const PODVector<unsigned char>& data)
  74. {
  75. buffer_ = data;
  76. position_ = 0;
  77. size_ = data.Size();
  78. }
  79. void VectorBuffer::SetData(const void* data, unsigned size)
  80. {
  81. if (!data)
  82. size = 0;
  83. buffer_.Resize(size);
  84. if (size)
  85. memcpy(&buffer_[0], data, size);
  86. position_ = 0;
  87. size_ = size;
  88. }
  89. void VectorBuffer::SetData(Deserializer& source, unsigned size)
  90. {
  91. buffer_.Resize(size);
  92. unsigned actualSize = source.Read(&buffer_[0], size);
  93. if (actualSize != size)
  94. buffer_.Resize(actualSize);
  95. position_ = 0;
  96. size_ = actualSize;
  97. }
  98. void VectorBuffer::Clear()
  99. {
  100. buffer_.Clear();
  101. position_ = 0;
  102. size_ = 0;
  103. }
  104. void VectorBuffer::Resize(unsigned size)
  105. {
  106. buffer_.Resize(size);
  107. size_ = size;
  108. if (position_ > size_)
  109. position_ = size_;
  110. }
  111. }