llcorebufferstream.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file llcorebufferstream.cpp
  3. * @brief Implements the BufferStream adapter class
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llcorebufferstream.h"
  28. namespace LLCore
  29. {
  30. BufferArrayStreamBuf::BufferArrayStreamBuf(BufferArray* array)
  31. : mBufferArray(array),
  32. mReadCurPos(0),
  33. mReadCurBlock(-1),
  34. mReadBegin(NULL),
  35. mReadCur(NULL),
  36. mReadEnd(NULL),
  37. mWriteCurPos(0)
  38. {
  39. if (array)
  40. {
  41. array->addRef();
  42. mWriteCurPos = array->mLen;
  43. }
  44. }
  45. BufferArrayStreamBuf::~BufferArrayStreamBuf()
  46. {
  47. if (mBufferArray)
  48. {
  49. mBufferArray->release();
  50. mBufferArray = NULL;
  51. }
  52. }
  53. BufferArrayStreamBuf::int_type BufferArrayStreamBuf::underflow()
  54. {
  55. if (!mBufferArray)
  56. {
  57. return traits_type::eof();
  58. }
  59. if (mReadCur == mReadEnd)
  60. {
  61. // Find the next block with actual data or leave mCurBlock/mCur/mEnd
  62. // unchanged if we're at the end of any block chain.
  63. const char* new_begin = NULL;
  64. const char* new_end = NULL;
  65. S32 new_cur_block = mReadCurBlock + 1;
  66. while (mBufferArray->getBlockStartEnd(new_cur_block, &new_begin,
  67. &new_end))
  68. {
  69. if (new_begin != new_end)
  70. {
  71. break;
  72. }
  73. ++new_cur_block;
  74. }
  75. if (new_begin == new_end)
  76. {
  77. return traits_type::eof();
  78. }
  79. mReadCurBlock = new_cur_block;
  80. mReadBegin = mReadCur = new_begin;
  81. mReadEnd = new_end;
  82. }
  83. return traits_type::to_int_type(*mReadCur);
  84. }
  85. BufferArrayStreamBuf::int_type BufferArrayStreamBuf::uflow()
  86. {
  87. const int_type ret(underflow());
  88. if (traits_type::eof() != ret)
  89. {
  90. ++mReadCur;
  91. ++mReadCurPos;
  92. }
  93. return ret;
  94. }
  95. BufferArrayStreamBuf::int_type BufferArrayStreamBuf::pbackfail(int_type ch)
  96. {
  97. if (!mBufferArray)
  98. {
  99. return traits_type::eof();
  100. }
  101. if (mReadCur == mReadBegin)
  102. {
  103. // Find the previous block with actual data or leave mCurBlock/mBegin/
  104. // mCur/mEnd unchanged if we're at the beginning of any block chain.
  105. const char* new_begin = NULL;
  106. const char* new_end = NULL;
  107. S32 new_cur_block = mReadCurBlock - 1;
  108. while (mBufferArray->getBlockStartEnd(new_cur_block, &new_begin,
  109. &new_end))
  110. {
  111. if (new_begin != new_end)
  112. {
  113. break;
  114. }
  115. --new_cur_block;
  116. }
  117. if (new_begin == new_end)
  118. {
  119. return traits_type::eof();
  120. }
  121. mReadCurBlock = new_cur_block;
  122. mReadBegin = new_begin;
  123. mReadEnd = mReadCur = new_end;
  124. }
  125. if (traits_type::eof() != ch && mReadCur[-1] != ch)
  126. {
  127. return traits_type::eof();
  128. }
  129. --mReadCurPos;
  130. return traits_type::to_int_type(*--mReadCur);
  131. }
  132. std::streamsize BufferArrayStreamBuf::showmanyc()
  133. {
  134. return mBufferArray ? mBufferArray->mLen - mReadCurPos : -1;
  135. }
  136. BufferArrayStreamBuf::int_type BufferArrayStreamBuf::overflow(int c)
  137. {
  138. if (!mBufferArray || mWriteCurPos > mBufferArray->mLen)
  139. {
  140. return traits_type::eof();
  141. }
  142. const size_t wrote(mBufferArray->write(mWriteCurPos, &c, 1));
  143. mWriteCurPos += wrote;
  144. return wrote ? c : traits_type::eof();
  145. }
  146. std::streamsize BufferArrayStreamBuf::xsputn(const char* src,
  147. std::streamsize count)
  148. {
  149. if (!mBufferArray || mWriteCurPos > mBufferArray->mLen)
  150. {
  151. return 0;
  152. }
  153. const size_t wrote(mBufferArray->write(mWriteCurPos, src, count));
  154. mWriteCurPos += wrote;
  155. return wrote;
  156. }
  157. std::streampos BufferArrayStreamBuf::seekoff(std::streamoff off,
  158. std::ios_base::seekdir way,
  159. std::ios_base::openmode which)
  160. {
  161. std::streampos ret = -1;
  162. if (!mBufferArray)
  163. {
  164. return ret;
  165. }
  166. if (std::ios_base::in == which)
  167. {
  168. size_t pos = 0;
  169. switch (way)
  170. {
  171. case std::ios_base::beg:
  172. pos = off;
  173. break;
  174. case std::ios_base::cur:
  175. pos = mReadCurPos += off;
  176. break;
  177. case std::ios_base::end:
  178. pos = mBufferArray->mLen - off;
  179. break;
  180. default:
  181. return ret;
  182. }
  183. size_t buff_size = mBufferArray->size();
  184. if (pos >= buff_size)
  185. {
  186. if (buff_size)
  187. {
  188. pos = buff_size - 1;
  189. }
  190. else
  191. {
  192. pos = 0;
  193. }
  194. }
  195. size_t ba_offset = 0;
  196. S32 block = mBufferArray->findBlock(pos, ba_offset);
  197. if (block < 0)
  198. {
  199. return ret;
  200. }
  201. const char* start = NULL;
  202. const char* end = NULL;
  203. if (!mBufferArray->getBlockStartEnd(block, &start, &end))
  204. {
  205. return ret;
  206. }
  207. mReadCurBlock = block;
  208. mReadBegin = start;
  209. mReadCur = start + ba_offset;
  210. mReadEnd = end;
  211. ret = mReadCurPos = pos;
  212. }
  213. else if (std::ios_base::out == which)
  214. {
  215. int pos = 0;
  216. switch (way)
  217. {
  218. case std::ios_base::beg:
  219. pos = off;
  220. break;
  221. case std::ios_base::cur:
  222. pos = mWriteCurPos += off;
  223. break;
  224. case std::ios_base::end:
  225. pos = mBufferArray->mLen - off;
  226. break;
  227. default:
  228. return ret;
  229. }
  230. if (pos < 0)
  231. {
  232. return ret;
  233. }
  234. if (pos > (int)mBufferArray->size())
  235. {
  236. pos = mBufferArray->size();
  237. }
  238. ret = mWriteCurPos = pos;
  239. }
  240. return ret;
  241. }
  242. BufferArrayStream::BufferArrayStream(BufferArray* ba)
  243. : std::iostream(&mStreamBuf),
  244. mStreamBuf(ba)
  245. {
  246. }
  247. } // End namespace LLCore