Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
7.4 KiB

  1. // strstreambuf -- strstreambuf basic members
  2. #include <climits>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <strstream>
  6. #include <xdebug>
  7. #pragma warning(disable:4244 4097)
  8. _STD_BEGIN
  9. _CRTIMP2 istrstream::~istrstream()
  10. { // destroy an istrstream
  11. }
  12. _CRTIMP2 ostrstream::ostrstream(char *ptr, streamsize count,
  13. ios_base::openmode mode)
  14. : ostream(&_Mysb),
  15. _Mysb(ptr, count, ptr == 0 || (mode & app) == 0 ? ptr : ptr + strlen(ptr))
  16. { // construct with [ptr, ptr + count)
  17. }
  18. _CRTIMP2 ostrstream::~ostrstream()
  19. { // destroy an ostrstream
  20. }
  21. _CRTIMP2 strstream::strstream(char *ptr, streamsize count,
  22. ios_base::openmode mode)
  23. : iostream(&_Mysb),
  24. _Mysb(ptr, count, ptr == 0 || (mode & app) == 0 ? ptr : ptr + strlen(ptr))
  25. { // construct with [ptr, ptr + count)
  26. }
  27. _CRTIMP2 strstream::~strstream()
  28. { // destroy a strstream
  29. }
  30. _CRTIMP2 strstreambuf::~strstreambuf()
  31. { // destroy a strstreambuf
  32. _Tidy();
  33. }
  34. _CRTIMP2 void strstreambuf::freeze(bool freezeit)
  35. { // freeze or unfreeze writing
  36. if (freezeit && !(_Strmode & _Frozen))
  37. { // disable writing
  38. _Strmode |= _Frozen;
  39. _Pendsave = epptr();
  40. setp(pbase(), pptr(), eback());
  41. }
  42. else if (!freezeit && _Strmode & _Frozen)
  43. { // re-enable writing
  44. _Strmode &= ~_Frozen;
  45. setp(pbase(), pptr(), _Pendsave);
  46. }
  47. }
  48. _CRTIMP2 int strstreambuf::overflow(int meta)
  49. { // try to extend write area
  50. if (meta == EOF)
  51. return (0); // nothing to write
  52. else if (pptr() != 0 && pptr() < epptr())
  53. return ((unsigned char)(*_Pninc() = meta)); // room in buffer
  54. else if (!(_Strmode & _Dynamic)
  55. || _Strmode & (_Constant | _Frozen))
  56. return (EOF); // can't extend
  57. else
  58. { // okay to extend
  59. int oldsize = gptr() == 0 ? 0 : epptr() - eback();
  60. int newsize = oldsize;
  61. int inc = newsize / 2 < _Minsize
  62. ? _Minsize : newsize / 2; // grow by 50 per cent if possible
  63. _Minsize = _MINSIZE; // back to default for future growth
  64. char *ptr = 0;
  65. while (0 < inc && INT_MAX - inc < newsize)
  66. inc /= 2; // reduce growth increment if too big
  67. if (0 < inc)
  68. { // room to grow, increase size
  69. newsize += inc;
  70. ptr = _Palloc != 0 ? (char *)(*_Palloc)(newsize)
  71. : _NEW_CRT char[newsize];
  72. }
  73. if (ptr == 0)
  74. return (EOF); // couldn't grow, return failure
  75. if (0 < oldsize)
  76. memcpy(ptr, eback(), oldsize); // copy existing buffer
  77. if (!(_Strmode & _Allocated))
  78. ; // no buffer to free
  79. else if (_Pfree != 0)
  80. (*_Pfree)(eback()); // free with function call
  81. else
  82. _DELETE_CRT_VEC(eback()); // free by deleting array
  83. _Strmode |= _Allocated;
  84. if (oldsize == 0)
  85. { // set up new buffer
  86. _Seekhigh = ptr;
  87. setp(ptr, ptr + newsize);
  88. setg(ptr, ptr, ptr);
  89. }
  90. else
  91. { // revise old pointers
  92. _Seekhigh = _Seekhigh - eback() + ptr;
  93. setp(pbase() - eback() + ptr, pptr() - eback() + ptr,
  94. ptr + newsize);
  95. setg(ptr, gptr() - eback() + ptr, pptr() + 1);
  96. }
  97. return ((unsigned char)(*_Pninc() = meta));
  98. }
  99. }
  100. _CRTIMP2 int strstreambuf::pbackfail(int meta)
  101. { // try to putback a character
  102. if (gptr() == 0 || gptr() <= eback() || meta != EOF
  103. && (unsigned char)meta != (unsigned char)gptr()[-1]
  104. && _Strmode & _Constant)
  105. return (EOF); // can't put it back
  106. else
  107. { // safe to back up
  108. gbump(-1);
  109. return (meta == EOF ? 0 : (unsigned char)(*gptr() = meta));
  110. }
  111. }
  112. _CRTIMP2 int strstreambuf::underflow()
  113. { // read if read position available
  114. if (gptr() == 0)
  115. return (EOF); // no read buffer
  116. else if (gptr() < egptr())
  117. return ((unsigned char)*gptr()); // char in buffer, read it
  118. else if (pptr() == 0 || pptr() <= gptr() && _Seekhigh <= gptr())
  119. return (EOF); // no write buffer to read
  120. else
  121. { // update _Seekhigh and expand read region
  122. if (_Seekhigh < pptr())
  123. _Seekhigh = pptr();
  124. setg(eback(), gptr(), _Seekhigh);
  125. return ((unsigned char)*gptr());
  126. }
  127. }
  128. _CRTIMP2 streampos strstreambuf::seekoff(streamoff off,
  129. ios_base::seekdir way, ios_base::openmode which)
  130. { // seek by specified offset
  131. if (pptr() != 0 && _Seekhigh < pptr())
  132. _Seekhigh = pptr(); // update high water mark
  133. if (which & ios_base::in && gptr() != 0)
  134. { // set input (and maybe output) pointer
  135. if (way == ios_base::end)
  136. off += _Seekhigh - eback(); // seek from end
  137. else if (way == ios_base::cur
  138. && !(which & ios_base::out))
  139. off += gptr() - eback(); // seek from current position
  140. else if (way != ios_base::beg || off == _BADOFF)
  141. off = _BADOFF; // invalid seek
  142. if (0 <= off && off <= _Seekhigh - eback())
  143. { // seek from beginning, set one or two pointers
  144. gbump(eback() - gptr() + off);
  145. if (which & ios_base::out && pptr() != 0)
  146. setp(pbase(), gptr(), epptr());
  147. }
  148. else
  149. off = _BADOFF; // invalid seek from beginning
  150. }
  151. else if (which & ios_base::out && pptr() != 0)
  152. { // set only output pointer
  153. if (way == ios_base::end)
  154. off += _Seekhigh - eback(); // seek from end
  155. else if (way == ios_base::cur)
  156. off += pptr() - eback(); // seek from current position
  157. else if (way != ios_base::beg || off == _BADOFF)
  158. off = _BADOFF; // invalid seek
  159. if (0 <= off && off <= _Seekhigh - eback())
  160. pbump(eback() - pptr() + off); // seek from beginning
  161. else
  162. off = _BADOFF; // invalid seek from beginning
  163. }
  164. else // nothing to set
  165. off = _BADOFF;
  166. return (streampos(off));
  167. }
  168. _CRTIMP2 streampos strstreambuf::seekpos(streampos sp,
  169. ios_base::openmode which)
  170. { // seek to memorized position
  171. streamoff off = (streamoff)sp;
  172. if (pptr() != 0 && _Seekhigh < pptr())
  173. _Seekhigh = pptr(); // update high water mark
  174. if (off == _BADOFF)
  175. ; // invalid seek
  176. else if (which & ios_base::in && gptr() != 0)
  177. { // set input (and maybe output) pointer
  178. if (0 <= off && off <= _Seekhigh - eback())
  179. { // set valid offset
  180. gbump(eback() - gptr() + off);
  181. if (which & ios_base::out && pptr() != 0)
  182. setp(pbase(), gptr(), epptr());
  183. }
  184. else
  185. off = _BADOFF; // offset invalid, don't seek
  186. }
  187. else if (which & ios_base::out && pptr() != 0)
  188. { // set output pointer
  189. if (0 <= off && off <= _Seekhigh - eback())
  190. pbump(eback() - pptr() + off);
  191. else
  192. off = _BADOFF; // offset invalid, don't seek
  193. }
  194. else // nothing to set
  195. off = _BADOFF;
  196. return (streampos(off));
  197. }
  198. _CRTIMP2 void strstreambuf::_Init(streamsize count, char *gp, char *pp,
  199. _Strstate mode)
  200. { // initialize with possibly static buffer
  201. streambuf::_Init();
  202. _Minsize = _MINSIZE;
  203. _Pendsave = 0;
  204. _Seekhigh = 0;
  205. _Palloc = 0;
  206. _Pfree = 0;
  207. _Strmode = mode;
  208. if (gp == 0)
  209. { // make dynamic
  210. _Strmode |= _Dynamic;
  211. if (_Minsize < count)
  212. _Minsize = count;
  213. }
  214. else
  215. { // make static
  216. int size = count < 0 ? INT_MAX : count == 0 ? (int)strlen(gp) : count;
  217. _Seekhigh = gp + size;
  218. if (pp == 0)
  219. setg(gp, gp, gp + size); // set read pointers only
  220. else
  221. { // make writable too
  222. if (pp < gp)
  223. pp = gp;
  224. else if (gp + size < pp)
  225. pp = gp + size;
  226. setp(pp, gp + size);
  227. setg(gp, gp, pp);
  228. }
  229. }
  230. }
  231. _CRTIMP2 void strstreambuf::_Tidy()
  232. { // free any allocated storage
  233. if ((_Strmode & (_Allocated | _Frozen)) != _Allocated)
  234. ; // no buffer to free
  235. else if (_Pfree != 0)
  236. (*_Pfree)(eback()); // free with function call
  237. else
  238. _DELETE_CRT_VEC(eback()); // free by deleting array
  239. _Seekhigh = 0;
  240. _Strmode &= ~(_Allocated | _Frozen);
  241. }
  242. _STD_END
  243. /*
  244. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  245. * Consult your license regarding permissions and restrictions.
  246. V3.10:0009 */