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.

300 lines
9.6 KiB

  1. // sstream standard header
  2. #ifndef _SSTREAM_
  3. #define _SSTREAM_
  4. #include <string>
  5. #ifdef _MSC_VER
  6. #pragma pack(push,8)
  7. #endif /* _MSC_VER */
  8. _STD_BEGIN
  9. enum __Strstate {_Allocated = 1, _Constant = 2, _Noread = 4};
  10. _BITMASK(__Strstate, _Strstate);
  11. _BITMASK_OPS(__Strstate)
  12. // TEMPLATE CLASS basic_stringbuf
  13. template<class _E,
  14. class _Tr /* = char_traits<_E> */,
  15. class _A /* = allocator<_E> */ >
  16. class basic_stringbuf : public basic_streambuf<_E, _Tr> {
  17. public:
  18. typedef basic_string<_E, _Tr, _A> _Mystr;
  19. explicit basic_stringbuf(
  20. ios_base::openmode _W = ios_base::in | ios_base::out)
  21. {_Init(0, 0, _Mode(_W)); }
  22. explicit basic_stringbuf(const _Mystr& _S,
  23. ios_base::openmode _W = ios_base::in | ios_base::out)
  24. {_Init(_S.c_str(), _S.size(), _Mode(_W)); }
  25. virtual ~basic_stringbuf()
  26. {_Tidy(); }
  27. _Mystr str() const
  28. {if (!(_Strmode & _Constant) && pptr() != 0)
  29. {_Mystr _Str(pbase(), (_Seekhigh < pptr()
  30. ? pptr() : _Seekhigh) - pbase());
  31. return (_Str); }
  32. else if (!(_Strmode & _Noread) && gptr() != 0)
  33. {_Mystr _Str(eback(), egptr() - eback());
  34. return (_Str); }
  35. else
  36. {_Mystr _Nul;
  37. return (_Nul); }}
  38. void str(const _Mystr& _S)
  39. {_Tidy();
  40. _Init(_S.c_str(), _S.size(), _Strmode); }
  41. protected:
  42. virtual int_type overflow(int_type _C = _Tr::eof())
  43. {if (_Tr::eq_int_type(_Tr::eof(), _C))
  44. return (_Tr::not_eof(_C));
  45. else if (pptr() != 0 && pptr() < epptr())
  46. {*_Pninc() = _Tr::to_char_type(_C);
  47. return (_C); }
  48. else if (_Strmode & _Constant)
  49. return (_Tr::eof());
  50. else
  51. {size_t _Os = gptr() == 0 ? 0 : epptr() - eback();
  52. size_t _Ns = _Os + _Alsize;
  53. _E *_P = _Al.allocate(_Ns, (void *)0);
  54. if (0 < _Os)
  55. _Tr::copy(_P, eback(), _Os);
  56. else if (_ALSIZE < _Alsize)
  57. _Alsize = _ALSIZE;
  58. if (_Strmode & _Allocated)
  59. _Al.deallocate(eback(), _Os);
  60. _Strmode |= _Allocated;
  61. if (_Os == 0)
  62. {_Seekhigh = _P;
  63. setp(_P, _P + _Ns);
  64. setg(_P, _P, _P); }
  65. else
  66. {_Seekhigh = _Seekhigh - eback() + _P;
  67. setp(pbase() - eback() + _P,
  68. pptr() - eback() + _P, _P + _Ns);
  69. if (_Strmode & _Noread)
  70. setg(_P, _P, _P);
  71. else
  72. setg(_P, gptr() - eback() + _P, pptr() + 1); }
  73. *_Pninc() = _Tr::to_char_type(_C);
  74. return (_C); }}
  75. virtual int_type pbackfail(int_type _C = _Tr::eof())
  76. {if (gptr() == 0 || gptr() <= eback()
  77. || !_Tr::eq_int_type(_Tr::eof(), _C)
  78. && !_Tr::eq(_Tr::to_char_type(_C), gptr()[-1])
  79. && _Strmode & _Constant)
  80. return (_Tr::eof());
  81. else
  82. {gbump(-1);
  83. if (!_Tr::eq_int_type(_Tr::eof(), _C))
  84. *gptr() = _Tr::to_char_type(_C);
  85. return (_Tr::not_eof(_C)); }}
  86. virtual int_type underflow()
  87. {if (gptr() == 0)
  88. return (_Tr::eof());
  89. else if (gptr() < egptr())
  90. return (_Tr::to_int_type(*gptr()));
  91. else if (_Strmode & _Noread || pptr() == 0
  92. || pptr() <= gptr() && _Seekhigh <= gptr())
  93. return (_Tr::eof());
  94. else
  95. {if (_Seekhigh < pptr())
  96. _Seekhigh = pptr();
  97. setg(eback(), gptr(), _Seekhigh);
  98. return (_Tr::to_int_type(*gptr())); }}
  99. virtual pos_type seekoff(off_type _O, ios_base::seekdir _Way,
  100. ios_base::openmode _Which = ios_base::in | ios_base::out)
  101. {if (pptr() != 0 && _Seekhigh < pptr())
  102. _Seekhigh = pptr();
  103. if (_Which & ios_base::in && gptr() != 0)
  104. {if (_Way == ios_base::end)
  105. _O += _Seekhigh - eback();
  106. else if (_Way == ios_base::cur
  107. && !(_Which & ios_base::out))
  108. _O += gptr() - eback();
  109. else if (_Way != ios_base::beg)
  110. _O = _BADOFF;
  111. if (0 <= _O && _O <= _Seekhigh - eback())
  112. {gbump(eback() - gptr() + _O);
  113. if (_Which & ios_base::out && pptr() != 0)
  114. setp(pbase(), gptr(), epptr()); }
  115. else
  116. _O = _BADOFF; }
  117. else if (_Which & ios_base::out && pptr() != 0)
  118. {if (_Way == ios_base::end)
  119. _O += _Seekhigh - eback();
  120. else if (_Way == ios_base::cur)
  121. _O += pptr() - eback();
  122. else if (_Way != ios_base::beg)
  123. _O = _BADOFF;
  124. if (0 <= _O && _O <= _Seekhigh - eback())
  125. pbump(eback() - pptr() + _O);
  126. else
  127. _O = _BADOFF; }
  128. else
  129. _O = _BADOFF;
  130. return (pos_type(_O)); }
  131. virtual pos_type seekpos(pos_type _P,
  132. ios_base::openmode _Which = ios_base::in | ios_base::out)
  133. {streamoff _O = (streamoff)_P;
  134. if (pptr() != 0 && _Seekhigh < pptr())
  135. _Seekhigh = pptr();
  136. if (_O == _BADOFF)
  137. ;
  138. else if (_Which & ios_base::in && gptr() != 0)
  139. {if (0 <= _O && _O <= _Seekhigh - eback())
  140. {gbump(eback() - gptr() + _O);
  141. if (_Which & ios_base::out && pptr() != 0)
  142. setp(pbase(), gptr(), epptr()); }
  143. else
  144. _O = _BADOFF; }
  145. else if (_Which & ios_base::out && pptr() != 0)
  146. {if (0 <= _O && _O <= _Seekhigh - eback())
  147. pbump(eback() - pptr() + _O);
  148. else
  149. _O = _BADOFF; }
  150. else
  151. _O = _BADOFF;
  152. return (streampos(_O)); }
  153. void _Init(const _E *_S, size_t _N, _Strstate _M)
  154. {_Pendsave = 0, _Seekhigh = 0;
  155. _Alsize = _MINSIZE, _Strmode = _M;
  156. if (_N == 0 || (_Strmode & (_Noread | _Constant))
  157. == (_Noread | _Constant))
  158. {setg(0, 0, 0);
  159. setp(0, 0); }
  160. else
  161. {_E *_P = _Al.allocate(_N, (void *)0);
  162. _Tr::copy(_P, _S, _N);
  163. _Seekhigh = _P + _N;
  164. if (!(_Strmode & _Noread))
  165. setg(_P, _P, _P + _N);
  166. if (!(_Strmode & _Constant))
  167. {setp(_P, _P + _N);
  168. if (gptr() == 0)
  169. setg(_P, _P, _P); }
  170. _Strmode |= _Allocated; }}
  171. void _Tidy()
  172. {if (_Strmode & _Allocated)
  173. _Al.deallocate(eback(),
  174. (pptr() != 0 ? epptr() : egptr()) - eback());
  175. _Seekhigh = 0;
  176. _Strmode &= ~_Allocated; }
  177. private:
  178. enum {_ALSIZE = 512, _MINSIZE = 32};
  179. _Strstate _Mode(ios_base::openmode _W)
  180. {_Strstate _St = (_Strstate)0;
  181. if (!(_W & ios_base::in))
  182. _St |= _Noread;
  183. if (!(_W & ios_base::out))
  184. _St |= _Constant;
  185. return (_St); }
  186. _E *_Pendsave, *_Seekhigh;
  187. int _Alsize;
  188. _Strstate _Strmode;
  189. allocator<_E> _Al;
  190. };
  191. // TEMPLATE CLASS basic_istringstream
  192. template<class _E,
  193. class _Tr /* = char_traits<_E> */,
  194. class _A /* = allocator<_E> */ >
  195. class basic_istringstream : public basic_istream<_E, _Tr> {
  196. public:
  197. typedef basic_stringbuf<_E, _Tr, _A> _Mysb;
  198. typedef basic_string<_E, _Tr, _A> _Mystr;
  199. explicit basic_istringstream(openmode _M = in)
  200. : basic_istream<_E, _Tr>(&_Sb), _Sb(_M | in) {}
  201. explicit basic_istringstream(const _Mystr& _S,
  202. openmode _M = in)
  203. : basic_istream<_E, _Tr>(&_Sb), _Sb(_S, _M | in) {}
  204. virtual ~basic_istringstream()
  205. {}
  206. _Mysb *rdbuf() const
  207. {return ((_Mysb *)&_Sb); }
  208. _Mystr str() const
  209. {return (_Sb.str()); }
  210. void str(const _Mystr& _S)
  211. {_Sb.str(_S); }
  212. private:
  213. _Mysb _Sb;
  214. };
  215. // TEMPLATE CLASS basic_ostringstream
  216. template<class _E,
  217. class _Tr /* = char_traits<_E> */,
  218. class _A /* = allocator<_E> */ >
  219. class basic_ostringstream : public basic_ostream<_E, _Tr> {
  220. public:
  221. typedef basic_stringbuf<_E, _Tr, _A> _Mysb;
  222. typedef basic_string<_E, _Tr, _A> _Mystr;
  223. explicit basic_ostringstream(openmode _M = out)
  224. : basic_ostream<_E, _Tr>(&_Sb), _Sb(_M | out) {}
  225. explicit basic_ostringstream(const _Mystr& _S,
  226. openmode _M = out)
  227. : basic_ostream<_E, _Tr>(&_Sb), _Sb(_S, _M | out) {}
  228. virtual ~basic_ostringstream()
  229. {}
  230. _Mysb *rdbuf() const
  231. {return ((_Mysb *)&_Sb); }
  232. _Mystr str() const
  233. {return (_Sb.str()); }
  234. void str(const _Mystr& _S)
  235. {_Sb.str(_S); }
  236. private:
  237. _Mysb _Sb;
  238. };
  239. // TEMPLATE CLASS basic_stringstream
  240. template<class _E,
  241. class _Tr /* = char_traits<_E> */,
  242. class _A /* = allocator<_E> */ >
  243. class basic_stringstream : public basic_iostream<_E, _Tr> {
  244. public:
  245. typedef basic_string<_E, _Tr, _A> _Mystr;
  246. explicit basic_stringstream(openmode _W = in | out)
  247. : basic_iostream<_E, _Tr>(&_Sb), _Sb(_W) {}
  248. explicit basic_stringstream(const _Mystr& _S,
  249. openmode _W = in | out)
  250. : basic_iostream<_E, _Tr>(&_Sb), _Sb(_S, _W) {}
  251. virtual ~basic_stringstream()
  252. {}
  253. basic_stringbuf<_E, _Tr, _A> *rdbuf() const
  254. {return ((basic_stringbuf<_E, _Tr, _A> *)&_Sb); }
  255. _Mystr str() const
  256. {return (_Sb.str()); }
  257. void str(const _Mystr& _S)
  258. {_Sb.str(_S); }
  259. private:
  260. basic_stringbuf<_E, _Tr, _A> _Sb;
  261. };
  262. #ifdef _DLL
  263. #ifdef __FORCE_INSTANCE
  264. template class _CRTIMP2 basic_stringbuf<char, char_traits<char> >;
  265. template class _CRTIMP2 basic_stringbuf<wchar_t, char_traits<wchar_t> >;
  266. template class _CRTIMP2 basic_istringstream<char, char_traits<char> >;
  267. template class _CRTIMP2 basic_istringstream<wchar_t, char_traits<wchar_t> >;
  268. template class _CRTIMP2 basic_ostringstream<char, char_traits<char> >;
  269. template class _CRTIMP2 basic_ostringstream<wchar_t, char_traits<wchar_t> >;
  270. template class _CRTIMP2 basic_stringstream<char, char_traits<char> >;
  271. template class _CRTIMP2 basic_stringstream<wchar_t, char_traits<wchar_t> >;
  272. #else // __FORCE_INSTANCE
  273. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  274. extern template class _CRTIMP2 basic_stringbuf<char, char_traits<char> >;
  275. extern template class _CRTIMP2 basic_stringbuf<wchar_t, char_traits<wchar_t> >;
  276. extern template class _CRTIMP2 basic_istringstream<char, char_traits<char> >;
  277. extern template class _CRTIMP2 basic_istringstream<wchar_t, char_traits<wchar_t> >;
  278. extern template class _CRTIMP2 basic_ostringstream<char, char_traits<char> >;
  279. extern template class _CRTIMP2 basic_ostringstream<wchar_t, char_traits<wchar_t> >;
  280. extern template class _CRTIMP2 basic_stringstream<char, char_traits<char> >;
  281. extern template class _CRTIMP2 basic_stringstream<wchar_t, char_traits<wchar_t> >;
  282. #pragma warning(default:4231) /* restore previous warning */
  283. #endif // __FORCE_INSTANCE
  284. #endif // _DLL
  285. _STD_END
  286. #ifdef _MSC_VER
  287. #pragma pack(pop)
  288. #endif /* _MSC_VER */
  289. #endif /* _SSTREAM_ */
  290. /*
  291. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  292. * Consult your license regarding permissions and restrictions.
  293. */