VectorBuffer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "Exception.h"
  25. #include "VectorBuffer.h"
  26. #include <cstring>
  27. #include "DebugNew.h"
  28. VectorBuffer::VectorBuffer()
  29. {
  30. }
  31. VectorBuffer::VectorBuffer(const std::vector<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. void VectorBuffer::read(void* dest, unsigned size)
  44. {
  45. if (!size)
  46. return;
  47. if (size + mPosition > mSize)
  48. SAFE_EXCEPTION("Attempted to read past buffer end");
  49. unsigned char* srcPtr = &mBuffer[mPosition];
  50. unsigned char* destPtr = (unsigned char*)dest;
  51. mPosition += size;
  52. while (size >= sizeof(unsigned))
  53. {
  54. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  55. srcPtr += sizeof(unsigned);
  56. destPtr += sizeof(unsigned);
  57. size -= sizeof(unsigned);
  58. }
  59. if (size & sizeof(unsigned short))
  60. {
  61. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  62. srcPtr += sizeof(unsigned short);
  63. destPtr += sizeof(unsigned short);
  64. }
  65. if (size & 1)
  66. *destPtr = *srcPtr;
  67. }
  68. unsigned VectorBuffer::seek(unsigned position)
  69. {
  70. if (position > mSize)
  71. position = mSize;
  72. mPosition = position;
  73. return mPosition;
  74. }
  75. void VectorBuffer::write(const void* data, unsigned size)
  76. {
  77. if (!size)
  78. return;
  79. if (size + mPosition > mSize)
  80. {
  81. mSize = size + mPosition;
  82. mBuffer.resize(mSize);
  83. }
  84. unsigned char* srcPtr = (unsigned char*)data;
  85. unsigned char* destPtr = &mBuffer[mPosition];
  86. mPosition += size;
  87. while (size >= sizeof(unsigned))
  88. {
  89. *((unsigned*)destPtr) = *((unsigned*)srcPtr);
  90. srcPtr += sizeof(unsigned);
  91. destPtr += sizeof(unsigned);
  92. size -= sizeof(unsigned);
  93. }
  94. if (size & sizeof(unsigned short))
  95. {
  96. *((unsigned short*)destPtr) = *((unsigned short*)srcPtr);
  97. srcPtr += sizeof(unsigned short);
  98. destPtr += sizeof(unsigned short);
  99. }
  100. if (size & 1)
  101. *destPtr = *srcPtr;
  102. }
  103. void VectorBuffer::setData(const std::vector<unsigned char>& data)
  104. {
  105. mBuffer = data;
  106. mPosition = 0;
  107. mSize = data.size();
  108. }
  109. void VectorBuffer::setData(const void* data, unsigned size)
  110. {
  111. if ((!data) && (size))
  112. SAFE_EXCEPTION("Null VectorBuffer source");
  113. mBuffer.resize(size);
  114. if (size)
  115. memcpy(&mBuffer[0], data, size);
  116. mPosition = 0;
  117. mSize = size;
  118. }
  119. void VectorBuffer::setData(Deserializer& source, unsigned size)
  120. {
  121. mBuffer.resize(size);
  122. if (size)
  123. source.read(&mBuffer[0], size);
  124. mPosition = 0;
  125. mSize = size;
  126. }
  127. void VectorBuffer::clear()
  128. {
  129. mBuffer.clear();
  130. mPosition = 0;
  131. mSize = 0;
  132. }
  133. void VectorBuffer::resize(unsigned size)
  134. {
  135. mBuffer.resize(size);
  136. mSize = size;
  137. if (mPosition > mSize)
  138. mPosition = mSize;
  139. }