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.

374 lines
11 KiB

  1. // fstream standard header
  2. #ifndef _FSTREAM_
  3. #define _FSTREAM_
  4. #include <istream>
  5. #include <new>
  6. #ifdef _MSC_VER
  7. #pragma pack(push,8)
  8. #endif /* _MSC_VER */
  9. _STD_BEGIN
  10. extern _CRTIMP FILE *__cdecl __Fiopen(const char *,ios_base::openmode);
  11. // TEMPLATE FUNCTION _Fgetc
  12. template<class _E> inline
  13. bool _Fgetc(_E& _C, _Filet *_Fi)
  14. {return (fread(&_C, sizeof (_E), 1, _Fi) == 1); }
  15. inline bool _Fgetc(char& _C, _Filet *_Fi)
  16. {int _Ch;
  17. if ((_Ch = fgetc(_Fi)) == EOF)
  18. return (false);
  19. else
  20. {_C = (char)_Ch;
  21. return (true); }}
  22. inline bool _Fgetc(wchar_t& _C, _Filet *_Fi)
  23. {_Wint_t _Ch;
  24. if ((_Ch = fgetwc(_Fi)) == WEOF)
  25. return (false);
  26. else
  27. {_C = _Ch;
  28. return (true); }}
  29. // TEMPLATE FUNCTION _Fputc
  30. template<class _E> inline
  31. bool _Fputc(_E _C, _Filet *_Fi)
  32. {return (fwrite(&_C, sizeof (_E), 1, _Fi) == 1); }
  33. inline bool _Fputc(char _C, _Filet *_Fi)
  34. {return (fputc(_C, _Fi) != EOF); }
  35. inline bool _Fputc(wchar_t _C, _Filet *_Fi)
  36. {return (fputwc(_C, _Fi) != WEOF); }
  37. // TEMPLATE FUNCTION _Ungetc
  38. template<class _E> inline
  39. bool _Ungetc(const _E& _C, _Filet *_Fi, size_t _N)
  40. {const unsigned char *_P = (const unsigned char *)&_C;
  41. for (_P += _N; 0 < _N && ungetc(*--_P, _Fi) != EOF; --_N)
  42. ;
  43. if (_N == 0)
  44. return (true);
  45. else
  46. {for (; _N < sizeof (_E); ++_N)
  47. fgetc(_Fi);
  48. return (false); }}
  49. template<class _E> inline
  50. bool _Ungetc(const _E& _C, _Filet *_Fi)
  51. {return (_Ungetc(_C, _Fi, sizeof (_E))); }
  52. inline bool _Ungetc(char _C, _Filet *_Fi)
  53. {return (ungetc((unsigned char)_C, _Fi) != EOF); }
  54. inline bool _Ungetc(wchar_t _C, _Filet *_Fi)
  55. {return (ungetwc(_C, _Fi) != WEOF); }
  56. // TEMPLATE CLASS basic_filebuf
  57. template<class _E, class _Tr /* = char_traits<_E> */ >
  58. class basic_filebuf : public basic_streambuf<_E, _Tr> {
  59. public:
  60. typedef basic_filebuf<_E, _Tr> _Myt;
  61. typedef basic_streambuf<_E, _Tr> _Mysb;
  62. typedef codecvt<_E, char, typename _Tr::state_type> _Cvt;
  63. basic_filebuf(_Filet *_F = 0)
  64. : _Loc(), _Mysb() {_Init(_F, _Newfl); }
  65. basic_filebuf(_Uninitialized)
  66. : _Loc(_Noinit), _Mysb(_Noinit) {}
  67. virtual ~basic_filebuf()
  68. {if (_Closef)
  69. close();
  70. delete _Str; }
  71. enum _Initfl {_Newfl, _Openfl, _Closefl};
  72. bool is_open() const
  73. {return (_File != 0); }
  74. _Myt *open(const char *_S, ios_base::openmode _M)
  75. {_Filet *_Fp;
  76. if (_File != 0 || (_Fp = __Fiopen(_S, _M)) == 0)
  77. return (0);
  78. _Init(_Fp, _Openfl);
  79. _Initcvt();
  80. return (this); }
  81. _Myt *open(const char *_N, ios::open_mode _M)
  82. {return (open(_N, (ios::openmode)_M)); }
  83. _Myt *close()
  84. {if (_File != 0 && fclose(_File) == 0)
  85. {_Init(0, _Closefl);
  86. return (this); }
  87. else
  88. return (0); }
  89. protected:
  90. virtual int_type overflow(int_type _C = _Tr::eof())
  91. {if (_Tr::eq_int_type(_Tr::eof(), _C))
  92. return (_Tr::not_eof(_C));
  93. else if (pptr() != 0 && pptr() < epptr())
  94. {*_Pninc() = _Tr::to_char_type(_C);
  95. return (_C); }
  96. else if (_File == 0)
  97. return (_Tr::eof());
  98. else if (_Pcvt == 0)
  99. return (_Fputc(_Tr::to_char_type(_C), _File)
  100. ? _C : _Tr::eof());
  101. else
  102. {const int _NC = 8;
  103. const _E _X = _Tr::to_char_type(_C);
  104. const _E *_S;
  105. char *_D;
  106. _Str->erase();
  107. for (size_t _I = _NC; ; _I += _NC)
  108. {_Str->append(_NC, '\0');
  109. switch (_Pcvt->out(_State,
  110. &_X, &_X + 1, _S,
  111. _Str->begin(), _Str->end(), _D))
  112. {case codecvt_base::partial:
  113. if (_S == &_X)
  114. return (_Tr::eof());
  115. case codecvt_base::ok: // can fall through
  116. {size_t _N = _D - _Str->begin();
  117. return (fwrite(_Str->begin(), 1, _N, _File)
  118. == _N ? _C : _Tr::eof()); }
  119. case codecvt_base::noconv:
  120. return (_Fputc(_X, _File) ? _C : _Tr::eof());
  121. default:
  122. return (_Tr::eof()); }}}}
  123. virtual int_type pbackfail(int_type _C = _Tr::eof())
  124. {if (gptr() != 0 && eback() < gptr()
  125. && (_Tr::eq_int_type(_Tr::eof(), _C)
  126. || _Tr::eq_int_type(_Tr::to_int_type(gptr()[-1]),
  127. _C)))
  128. {_Gndec();
  129. return (_Tr::not_eof(_C)); }
  130. else if (_File == 0 || _Tr::eq_int_type(_Tr::eof(), _C))
  131. return (_Tr::eof());
  132. else if (_Pcvt == 0)
  133. return (_Ungetc(_Tr::to_char_type(_C), _File)
  134. ? _C : _Tr::eof());
  135. else if (0 < _Str->size()
  136. && _Ungetc(*_Str->begin(), _File, _Str->size()))
  137. {_Str->erase();
  138. _State = _State0;
  139. return (_C); }
  140. else
  141. return (_Tr::eof()); }
  142. virtual int_type underflow()
  143. {if (gptr() != 0 && gptr() < egptr())
  144. return (_Tr::to_int_type(*gptr()));
  145. else
  146. return (pbackfail(uflow())); }
  147. virtual int_type uflow()
  148. {if (gptr() != 0 && gptr() < egptr())
  149. return (_Tr::to_int_type(*_Gninc()));
  150. else if (_File == 0)
  151. return (_Tr::eof());
  152. else if (_Pcvt == 0)
  153. {_E _C;
  154. return (_Fgetc(_C, _File) ? _Tr::to_int_type(_C)
  155. : _Tr::eof()); }
  156. else
  157. for (_State0 = _State, _Str->erase(); ; )
  158. {_E _X, *_D;
  159. const char *_S;
  160. int _C = fgetc(_File);
  161. if (_C == EOF)
  162. return (_Tr::eof()); // partial char?
  163. _Str->append(1, (char)_C);
  164. _State = _State0;
  165. switch (_Pcvt->in(_State,
  166. _Str->begin(), _Str->end(), _S,
  167. &_X, &_X + 1, _D))
  168. {case codecvt_base::partial:
  169. break;
  170. case codecvt_base::noconv:
  171. if (_Str->size() < sizeof (_E))
  172. break;
  173. memcpy(&_X, _Str->begin(), sizeof (_E));
  174. case codecvt_base::ok: // can fall through
  175. return (_Tr::to_int_type(_X));
  176. default:
  177. return (_Tr::eof()); }}}
  178. virtual pos_type seekoff(off_type _O, ios_base::seekdir _Way,
  179. ios_base::openmode =
  180. (ios_base::openmode)(ios_base::in | ios_base::out))
  181. {fpos_t _Fp;
  182. if (_File == 0 || fseek(_File, (long)_O, _Way) != 0
  183. || fgetpos(_File, &_Fp) != 0)
  184. return (pos_type(_BADOFF));
  185. return (pos_type(_State, _Fp)); }
  186. virtual pos_type seekpos(pos_type _P,
  187. ios_base::openmode =
  188. (ios_base::openmode)(ios_base::in | ios_base::out))
  189. {fpos_t _Fp = _P.get_fpos_t();
  190. off_type _Off = (off_type)_P - _FPOSOFF(_Fp);
  191. if (_File == 0
  192. || fsetpos(_File, &_Fp) != 0
  193. || _Off != 0 && fseek(_File, (long)_Off, SEEK_CUR) != 0
  194. || fgetpos(_File, &_Fp) != 0)
  195. return (pos_type(_BADOFF));
  196. if (_Str != 0)
  197. _State = _P.state(), _Str->erase();
  198. return (pos_type(_State, _Fp)); }
  199. virtual _Mysb *setbuf(_E *_S, streamsize _N)
  200. {return (_File == 0 || setvbuf(_File, (char *)_S,
  201. _IOFBF, _N * sizeof (_E)) != 0 ? 0 : this); }
  202. virtual int sync()
  203. {return (_File == 0 || 0 <= fflush(_File) ? 0 : -1); }
  204. void _Init(_Filet *_Fp, _Initfl _Which)
  205. {static _Tr::state_type _Stinit;
  206. _Closef = _Which == _Openfl;
  207. if (_Which == _Newfl)
  208. {_Loc.locale::~locale();
  209. new (&_Loc) locale;
  210. _Str = 0; }
  211. _Mysb::_Init();
  212. if (_Fp != 0 && !_Closef && sizeof (_E) == 1)
  213. {_Mysb::_Init((_E **)&_Fp->_base,
  214. (_E **)&_Fp->_ptr, &_Fp->_cnt,
  215. (_E **)&_Fp->_base, (_E **)&_Fp->_ptr,
  216. &_Fp->_cnt); }
  217. _File = _Fp;
  218. _State = _Stinit;
  219. _State0 = _Stinit;
  220. _Pcvt = 0; }
  221. void _Initcvt()
  222. {_Pcvt = (_Cvt *)&_USE(getloc(), _Cvt);
  223. _Loc = _ADDFAC(_Loc, _Pcvt);
  224. if (_Pcvt->always_noconv())
  225. _Pcvt = 0;
  226. if (_Str == 0)
  227. _Str = new string; }
  228. private:
  229. _Cvt *_Pcvt;
  230. typename _Tr::state_type _State0;
  231. typename _Tr::state_type _State;
  232. string *_Str;
  233. bool _Closef;
  234. locale _Loc;
  235. _Filet *_File;
  236. };
  237. #ifdef _DLL
  238. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  239. extern template class _CRTIMP basic_filebuf<char, char_traits<char> >;
  240. extern template class _CRTIMP basic_filebuf<wchar_t, char_traits<wchar_t> >;
  241. #pragma warning(default:4231) /* restore previous warning */
  242. #endif // _DLL
  243. // TEMPLATE CLASS basic_ifstream
  244. template<class _E, class _Tr /* = char_traits<_E> */ >
  245. class basic_ifstream : public basic_istream<_E, _Tr> {
  246. public:
  247. typedef basic_ifstream<_E, _Tr> _Myt;
  248. typedef basic_filebuf<_E, _Tr> _Myfb;
  249. basic_ifstream()
  250. : basic_istream<_E, _Tr>(&_Fb) {}
  251. explicit basic_ifstream(const char *_S,
  252. ios_base::openmode _M = in)
  253. : basic_istream<_E, _Tr>(&_Fb)
  254. {if (_Fb.open(_S, _M | in) == 0)
  255. setstate(failbit); }
  256. virtual ~basic_ifstream()
  257. {}
  258. _Myfb *rdbuf() const
  259. {return ((_Myfb *)&_Fb); }
  260. bool is_open() const
  261. {return (_Fb.is_open()); }
  262. void open(const char *_S, ios_base::openmode _M = in)
  263. {if (_Fb.open(_S, _M | in) == 0)
  264. setstate(failbit); }
  265. void open(const char *_S, ios_base::open_mode _M)
  266. {open(_S, (openmode)_M); }
  267. void close()
  268. {if (_Fb.close() == 0)
  269. setstate(failbit); }
  270. private:
  271. _Myfb _Fb;
  272. };
  273. #ifdef _DLL
  274. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  275. extern template class _CRTIMP basic_ifstream<char, char_traits<char> >;
  276. extern template class _CRTIMP basic_ifstream<wchar_t, char_traits<wchar_t> >;
  277. #pragma warning(default:4231) /* restore previous warning */
  278. #endif // _DLL
  279. // TEMPLATE CLASS basic_ofstream
  280. template<class _E, class _Tr /* = char_traits<_E> */ >
  281. class basic_ofstream : public basic_ostream<_E, _Tr> {
  282. public:
  283. typedef basic_ofstream<_E, _Tr> _Myt;
  284. typedef basic_filebuf<_E, _Tr> _Myfb;
  285. basic_ofstream()
  286. : basic_ostream<_E, _Tr>(&_Fb) {}
  287. explicit basic_ofstream(const char *_S,
  288. ios_base::openmode _M = out | trunc)
  289. : basic_ostream<_E, _Tr>(&_Fb)
  290. {if (_Fb.open(_S, _M | out) == 0)
  291. setstate(failbit); }
  292. virtual ~basic_ofstream()
  293. {}
  294. _Myfb *rdbuf() const
  295. {return ((_Myfb *)&_Fb); }
  296. bool is_open() const
  297. {return (_Fb.is_open()); }
  298. void open(const char *_S, ios_base::openmode _M = out | trunc)
  299. {if (_Fb.open(_S, _M | out) == 0)
  300. setstate(failbit); }
  301. void open(const char *_S, ios_base::open_mode _M)
  302. {open(_S, (openmode)_M); }
  303. void close()
  304. {if (_Fb.close() == 0)
  305. setstate(failbit); }
  306. private:
  307. _Myfb _Fb;
  308. };
  309. #ifdef _DLL
  310. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  311. extern template class _CRTIMP basic_ofstream<char, char_traits<char> >;
  312. extern template class _CRTIMP basic_ofstream<wchar_t, char_traits<wchar_t> >;
  313. #pragma warning(default:4231) /* restore previous warning */
  314. #endif // _DLL
  315. // TEMPLATE CLASS basic_fstream
  316. template<class _E, class _Tr /* = char_traits<_E> */ >
  317. class basic_fstream : public basic_iostream<_E, _Tr> {
  318. public:
  319. basic_fstream()
  320. : basic_iostream<_E, _Tr>(&_Fb) {}
  321. explicit basic_fstream(const char *_S,
  322. ios_base::openmode _M = in | out)
  323. : basic_iostream<_E, _Tr>(&_Fb)
  324. {if (_Fb.open(_S, _M) == 0)
  325. setstate(failbit); }
  326. virtual ~basic_fstream()
  327. {}
  328. basic_filebuf<_E, _Tr> *rdbuf() const
  329. {return ((basic_filebuf<_E, _Tr> *)&_Fb); }
  330. bool is_open() const
  331. {return (_Fb.is_open()); }
  332. void open(const char *_S, ios_base::openmode _M = in | out)
  333. {if (_Fb.open(_S, _M) == 0)
  334. setstate(failbit); }
  335. void open(const char *_S, ios_base::open_mode _M)
  336. {open(_S, (openmode)_M); }
  337. void close()
  338. {if (_Fb.close() == 0)
  339. setstate(failbit); }
  340. private:
  341. basic_filebuf<_E, _Tr> _Fb;
  342. };
  343. #ifdef _DLL
  344. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  345. extern template class _CRTIMP basic_fstream<char, char_traits<char> >;
  346. extern template class _CRTIMP basic_fstream<wchar_t, char_traits<wchar_t> >;
  347. #pragma warning(default:4231) /* restore previous warning */
  348. #endif // _DLL
  349. _STD_END
  350. #ifdef _MSC_VER
  351. #pragma pack(pop)
  352. #endif /* _MSC_VER */
  353. #endif /* _FSTREAM_ */
  354. /*
  355. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  356. * Consult your license regarding permissions and restrictions.
  357. */