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.

250 lines
6.1 KiB

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