_fstream.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Copyright (c) 1999
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. // This header defines classes basic_filebuf, basic_ifstream,
  19. // basic_ofstream, and basic_fstream. These classes represent
  20. // streambufs and streams whose sources or destinations are files.
  21. #ifndef _STLP_INTERNAL_FSTREAM_H
  22. #define _STLP_INTERNAL_FSTREAM_H
  23. #if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
  24. # error This header file requires the -LANG:std option
  25. #endif
  26. #ifndef _STLP_INTERNAL_STREAMBUF
  27. # include <stl/_streambuf.h>
  28. #endif
  29. #ifndef _STLP_INTERNAL_ISTREAM
  30. # include <stl/_istream.h>
  31. #endif
  32. #ifndef _STLP_INTERNAL_CODECVT_H
  33. # include <stl/_codecvt.h>
  34. #endif
  35. #if defined (_STLP_USE_WIN32_IO)
  36. typedef void* _STLP_fd;
  37. #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
  38. typedef int _STLP_fd;
  39. #else
  40. # error "Configure i/o !"
  41. #endif
  42. _STLP_BEGIN_NAMESPACE
  43. //----------------------------------------------------------------------
  44. // Class _Filebuf_base, a private base class to factor out the system-
  45. // dependent code from basic_filebuf<>.
  46. class _STLP_CLASS_DECLSPEC _Filebuf_base {
  47. public: // Opening and closing files.
  48. _Filebuf_base();
  49. bool _M_open(const char*, ios_base::openmode, long __protection);
  50. bool _M_open(const char*, ios_base::openmode);
  51. bool _M_open(int __id, ios_base::openmode = ios_base::__default_mode);
  52. #if defined (_STLP_USE_WIN32_IO)
  53. bool _M_open(_STLP_fd __id, ios_base::openmode = ios_base::__default_mode);
  54. #endif /* _STLP_USE_WIN32_IO */
  55. bool _M_close();
  56. public: // Low-level I/O, like Unix read/write
  57. ptrdiff_t _M_read(char* __buf, ptrdiff_t __n);
  58. streamoff _M_seek(streamoff __offset, ios_base::seekdir __dir);
  59. streamoff _M_file_size();
  60. bool _M_write(char* __buf, ptrdiff_t __n);
  61. public: // Memory-mapped I/O.
  62. void* _M_mmap(streamoff __offset, streamoff __len);
  63. void _M_unmap(void* __mmap_base, streamoff __len);
  64. public:
  65. // Returns a value n such that, if pos is the file pointer at the
  66. // beginning of the range [first, last), pos + n is the file pointer at
  67. // the end. On many operating systems n == __last - __first.
  68. // In Unix, writing n characters always bumps the file position by n.
  69. // In Windows text mode, however, it bumps the file position by n + m,
  70. // where m is the number of newlines in the range. That's because an
  71. // internal \n corresponds to an external two-character sequence.
  72. streamoff _M_get_offset(char* __first, char* __last) {
  73. #if defined (_STLP_UNIX) || defined (_STLP_MAC)
  74. return __last - __first;
  75. #else // defined (_STLP_WIN32)
  76. return ( (_M_openmode & ios_base::binary) != 0 )
  77. ? (__last - __first)
  78. : count(__first, __last, '\n') + (__last - __first);
  79. #endif
  80. }
  81. // Returns true if we're in binary mode or if we're using an OS or file
  82. // system where there is no distinction between text and binary mode.
  83. bool _M_in_binary_mode() const {
  84. #if defined (_STLP_UNIX) || defined (_STLP_MAC) || defined(__BEOS__) || defined (__amigaos__)
  85. return true;
  86. #elif defined (_STLP_WIN32) || defined (_STLP_VM)
  87. return (_M_openmode & ios_base::binary) != 0;
  88. #else
  89. # error "Port!"
  90. #endif
  91. }
  92. static void _S_initialize();
  93. protected: // Static data members.
  94. static size_t _M_page_size;
  95. protected: // Data members.
  96. _STLP_fd _M_file_id;
  97. #if defined (_STLP_USE_STDIO_IO)
  98. // for stdio, the whole FILE* is being kept here
  99. FILE* _M_file;
  100. #endif
  101. ios_base::openmode _M_openmode ;
  102. unsigned char _M_is_open ;
  103. unsigned char _M_should_close ;
  104. unsigned char _M_regular_file ;
  105. #if defined (_STLP_USE_WIN32_IO)
  106. _STLP_fd _M_view_id;
  107. #endif
  108. public :
  109. static size_t _STLP_CALL __page_size() { return _M_page_size; }
  110. int __o_mode() const { return (int)_M_openmode; }
  111. bool __is_open() const { return (_M_is_open !=0 ); }
  112. bool __should_close() const { return (_M_should_close != 0); }
  113. bool __regular_file() const { return (_M_regular_file != 0); }
  114. _STLP_fd __get_fd() const { return _M_file_id; }
  115. };
  116. //----------------------------------------------------------------------
  117. // Class basic_filebuf<>.
  118. // Forward declaration of two helper classes.
  119. template <class _Traits> class _Noconv_input;
  120. template <class _Traits> class _Noconv_output;
  121. // There is a specialized version of underflow, for basic_filebuf<char>,
  122. // in fstream.cpp.
  123. template <class _CharT, class _Traits>
  124. class _Underflow;
  125. template <class _CharT, class _Traits>
  126. class basic_filebuf : public basic_streambuf<_CharT, _Traits> {
  127. public: // Types.
  128. typedef _CharT char_type;
  129. typedef typename _Traits::int_type int_type;
  130. typedef typename _Traits::pos_type pos_type;
  131. typedef typename _Traits::off_type off_type;
  132. typedef _Traits traits_type;
  133. typedef typename _Traits::state_type _State_type;
  134. typedef basic_streambuf<_CharT, _Traits> _Base;
  135. typedef basic_filebuf<_CharT, _Traits> _Self;
  136. public: // Constructors, destructor.
  137. basic_filebuf();
  138. ~basic_filebuf();
  139. public: // Opening and closing files.
  140. bool is_open() const { return _M_base.__is_open(); }
  141. _Self* open(const char* __s, ios_base::openmode __m) {
  142. return _M_base._M_open(__s, __m) ? this : 0;
  143. }
  144. #if !defined (_STLP_NO_EXTENSIONS)
  145. // These two version of open() and file descriptor getter are extensions.
  146. _Self* open(const char* __s, ios_base::openmode __m,
  147. long __protection) {
  148. return _M_base._M_open(__s, __m, __protection) ? this : 0;
  149. }
  150. _STLP_fd fd() const { return _M_base.__get_fd(); }
  151. _Self* open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
  152. return this->_M_open(__id, _Init_mode);
  153. }
  154. # if defined (_STLP_USE_WIN32_IO)
  155. _Self* open(_STLP_fd __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
  156. return _M_base._M_open(__id, _Init_mode) ? this : 0;
  157. }
  158. # endif /* _STLP_USE_WIN32_IO */
  159. #endif
  160. _Self* _M_open(int __id, ios_base::openmode _Init_mode = ios_base::__default_mode) {
  161. return _M_base._M_open(__id, _Init_mode) ? this : 0;
  162. }
  163. _Self* close();
  164. protected: // Virtual functions from basic_streambuf.
  165. virtual streamsize showmanyc();
  166. virtual int_type underflow();
  167. virtual int_type pbackfail(int_type = traits_type::eof());
  168. virtual int_type overflow(int_type = traits_type::eof());
  169. virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
  170. virtual pos_type seekoff(off_type, ios_base::seekdir,
  171. ios_base::openmode = ios_base::in | ios_base::out);
  172. virtual pos_type seekpos(pos_type,
  173. ios_base::openmode = ios_base::in | ios_base::out);
  174. virtual int sync();
  175. virtual void imbue(const locale&);
  176. private: // Helper functions.
  177. // Precondition: we are currently in putback input mode. Effect:
  178. // switches back to ordinary input mode.
  179. void _M_exit_putback_mode() {
  180. this->setg(_M_saved_eback, _M_saved_gptr, _M_saved_egptr);
  181. _M_in_putback_mode = false;
  182. }
  183. bool _M_switch_to_input_mode();
  184. void _M_exit_input_mode();
  185. bool _M_switch_to_output_mode();
  186. int_type _M_input_error();
  187. int_type _M_underflow_aux();
  188. friend class _Underflow<_CharT, _Traits>;
  189. int_type _M_output_error();
  190. bool _M_unshift();
  191. bool _M_allocate_buffers(_CharT* __buf, streamsize __n);
  192. bool _M_allocate_buffers();
  193. void _M_deallocate_buffers();
  194. pos_type _M_seek_return(off_type __off, _State_type __state) {
  195. if (__off != -1) {
  196. if (_M_in_input_mode)
  197. _M_exit_input_mode();
  198. _M_in_input_mode = false;
  199. _M_in_output_mode = false;
  200. _M_in_putback_mode = false;
  201. _M_in_error_mode = false;
  202. this->setg(0, 0, 0);
  203. this->setp(0, 0);
  204. }
  205. pos_type __result(__off);
  206. __result.state(__state);
  207. return __result;
  208. }
  209. bool _M_seek_init(bool __do_unshift);
  210. void _M_setup_codecvt(const locale&, bool __on_imbue = true);
  211. private: // Data members used in all modes.
  212. _Filebuf_base _M_base;
  213. private: // Locale-related information.
  214. unsigned char _M_constant_width;
  215. unsigned char _M_always_noconv;
  216. // private: // Mode flags.
  217. unsigned char _M_int_buf_dynamic; // True if internal buffer is heap allocated,
  218. // false if it was supplied by the user.
  219. unsigned char _M_in_input_mode;
  220. unsigned char _M_in_output_mode;
  221. unsigned char _M_in_error_mode;
  222. unsigned char _M_in_putback_mode;
  223. // Internal buffer: characters seen by the filebuf's clients.
  224. _CharT* _M_int_buf;
  225. _CharT* _M_int_buf_EOS;
  226. // External buffer: characters corresponding to the external file.
  227. char* _M_ext_buf;
  228. char* _M_ext_buf_EOS;
  229. // The range [_M_ext_buf, _M_ext_buf_converted) contains the external
  230. // characters corresponding to the sequence in the internal buffer. The
  231. // range [_M_ext_buf_converted, _M_ext_buf_end) contains characters that
  232. // have been read into the external buffer but have not been converted
  233. // to an internal sequence.
  234. char* _M_ext_buf_converted;
  235. char* _M_ext_buf_end;
  236. // State corresponding to beginning of internal buffer.
  237. _State_type _M_state;
  238. private: // Data members used only in input mode.
  239. // Similar to _M_state except that it corresponds to
  240. // the end of the internal buffer instead of the beginning.
  241. _State_type _M_end_state;
  242. // This is a null pointer unless we are in mmap input mode.
  243. void* _M_mmap_base;
  244. streamoff _M_mmap_len;
  245. private: // Data members used only in putback mode.
  246. _CharT* _M_saved_eback;
  247. _CharT* _M_saved_gptr;
  248. _CharT* _M_saved_egptr;
  249. typedef codecvt<_CharT, char, _State_type> _Codecvt;
  250. const _Codecvt* _M_codecvt;
  251. int _M_width; // Width of the encoding (if constant), else 1
  252. int _M_max_width; // Largest possible width of single character.
  253. enum { _S_pback_buf_size = 8 };
  254. _CharT _M_pback_buf[_S_pback_buf_size];
  255. // for _Noconv_output
  256. public:
  257. bool _M_write(char* __buf, ptrdiff_t __n) {return _M_base._M_write(__buf, __n); }
  258. public:
  259. int_type
  260. _M_do_noconv_input() {
  261. _M_ext_buf_converted = _M_ext_buf_end;
  262. /* this-> */ _Base::setg((char_type*)_M_ext_buf, (char_type*)_M_ext_buf, (char_type*)_M_ext_buf_end);
  263. return traits_type::to_int_type(*_M_ext_buf);
  264. }
  265. };
  266. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  267. _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<char, char_traits<char> >;
  268. # if ! defined (_STLP_NO_WCHAR_T)
  269. _STLP_EXPORT_TEMPLATE_CLASS basic_filebuf<wchar_t, char_traits<wchar_t> >;
  270. # endif
  271. #endif /* _STLP_USE_TEMPLATE_EXPORT */
  272. //
  273. // This class had to be designed very carefully to work
  274. // with Visual C++.
  275. //
  276. template <class _Traits>
  277. class _Noconv_output {
  278. public:
  279. typedef typename _Traits::char_type char_type;
  280. static bool _STLP_CALL _M_doit(basic_filebuf<char_type, _Traits >*,
  281. char_type*, char_type*)
  282. { return false; }
  283. };
  284. _STLP_TEMPLATE_NULL
  285. class _STLP_CLASS_DECLSPEC _Noconv_output< char_traits<char> > {
  286. public:
  287. static bool _STLP_CALL
  288. _M_doit(basic_filebuf<char, char_traits<char> >* __buf,
  289. char* __first, char* __last) {
  290. ptrdiff_t __n = __last - __first;
  291. return (__buf->_M_write(__first, __n));
  292. }
  293. };
  294. //----------------------------------------------------------------------
  295. // basic_filebuf<> helper functions.
  296. //----------------------------------------
  297. // Helper functions for switching between modes.
  298. //
  299. // This class had to be designed very carefully to work
  300. // with Visual C++.
  301. //
  302. template <class _Traits>
  303. class _Noconv_input {
  304. public:
  305. typedef typename _Traits::int_type int_type;
  306. typedef typename _Traits::char_type char_type;
  307. static inline int_type _STLP_CALL
  308. _M_doit(basic_filebuf<char_type, _Traits>*)
  309. { return _Traits::eof(); }
  310. };
  311. _STLP_TEMPLATE_NULL
  312. class _Noconv_input<char_traits<char> > {
  313. public:
  314. static inline int _STLP_CALL
  315. _M_doit(basic_filebuf<char, char_traits<char> >* __buf) {
  316. return __buf->_M_do_noconv_input();
  317. }
  318. };
  319. // underflow() may be called for one of two reasons. (1) We've
  320. // been going through the special putback buffer, and we need to move back
  321. // to the regular internal buffer. (2) We've exhausted the internal buffer,
  322. // and we need to replentish it.
  323. template <class _CharT, class _Traits>
  324. class _Underflow {
  325. public:
  326. typedef typename _Traits::int_type int_type;
  327. typedef _Traits traits_type;
  328. // There is a specialized version of underflow, for basic_filebuf<char>,
  329. // in fstream.cpp.
  330. static int_type _STLP_CALL _M_doit(basic_filebuf<_CharT, _Traits>* __this) {
  331. if (!__this->_M_in_input_mode) {
  332. if (!__this->_M_switch_to_input_mode())
  333. return traits_type::eof();
  334. }
  335. else if (__this->_M_in_putback_mode) {
  336. __this->_M_exit_putback_mode();
  337. if (__this->gptr() != __this->egptr()) {
  338. int_type __c = traits_type::to_int_type(*__this->gptr());
  339. return __c;
  340. }
  341. }
  342. return __this->_M_underflow_aux();
  343. }
  344. };
  345. // Specialization of underflow: if the character type is char, maybe
  346. // we can use mmap instead of read.
  347. _STLP_TEMPLATE_NULL
  348. class _STLP_CLASS_DECLSPEC _Underflow< char, char_traits<char> >
  349. {
  350. public:
  351. typedef char_traits<char>::int_type int_type;
  352. typedef char_traits<char> traits_type;
  353. static int_type _STLP_CALL _M_doit(basic_filebuf<char, traits_type >* __this);
  354. };
  355. #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_NO_WCHAR_T)
  356. _STLP_EXPORT_TEMPLATE_CLASS _Underflow<wchar_t, char_traits<wchar_t> >;
  357. #endif
  358. //----------------------------------------------------------------------
  359. // Class basic_ifstream<>
  360. template <class _CharT, class _Traits>
  361. class basic_ifstream : public basic_istream<_CharT, _Traits> {
  362. public: // Types
  363. typedef _CharT char_type;
  364. typedef typename _Traits::int_type int_type;
  365. typedef typename _Traits::pos_type pos_type;
  366. typedef typename _Traits::off_type off_type;
  367. typedef _Traits traits_type;
  368. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  369. typedef basic_istream<_CharT, _Traits> _Base;
  370. typedef basic_filebuf<_CharT, _Traits> _Buf;
  371. public: // Constructors, destructor.
  372. basic_ifstream() :
  373. basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
  374. this->init(&_M_buf);
  375. }
  376. explicit basic_ifstream(const char* __s, ios_base::openmode __mod = ios_base::in) :
  377. basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0),
  378. _M_buf() {
  379. this->init(&_M_buf);
  380. if (!_M_buf.open(__s, __mod | ios_base::in))
  381. this->setstate(ios_base::failbit);
  382. }
  383. #if !defined (_STLP_NO_EXTENSIONS)
  384. explicit basic_ifstream(int __id, ios_base::openmode __mod = ios_base::in) :
  385. basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
  386. this->init(&_M_buf);
  387. if (!_M_buf.open(__id, __mod | ios_base::in))
  388. this->setstate(ios_base::failbit);
  389. }
  390. basic_ifstream(const char* __s, ios_base::openmode __m,
  391. long __protection) :
  392. basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
  393. this->init(&_M_buf);
  394. if (!_M_buf.open(__s, __m | ios_base::in, __protection))
  395. this->setstate(ios_base::failbit);
  396. }
  397. # if defined (_STLP_USE_WIN32_IO)
  398. explicit basic_ifstream(_STLP_fd __id, ios_base::openmode __mod = ios_base::in) :
  399. basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), _M_buf() {
  400. this->init(&_M_buf);
  401. if (!_M_buf.open(__id, __mod | ios_base::in))
  402. this->setstate(ios_base::failbit);
  403. }
  404. # endif /* _STLP_USE_WIN32_IO */
  405. #endif
  406. ~basic_ifstream() {}
  407. public: // File and buffer operations.
  408. basic_filebuf<_CharT, _Traits>* rdbuf() const
  409. { return __CONST_CAST(_Buf*,&_M_buf); }
  410. bool is_open() {
  411. return this->rdbuf()->is_open();
  412. }
  413. void open(const char* __s, ios_base::openmode __mod = ios_base::in) {
  414. if (!this->rdbuf()->open(__s, __mod | ios_base::in))
  415. this->setstate(ios_base::failbit);
  416. }
  417. void close() {
  418. if (!this->rdbuf()->close())
  419. this->setstate(ios_base::failbit);
  420. }
  421. private:
  422. basic_filebuf<_CharT, _Traits> _M_buf;
  423. };
  424. //----------------------------------------------------------------------
  425. // Class basic_ofstream<>
  426. template <class _CharT, class _Traits>
  427. class basic_ofstream : public basic_ostream<_CharT, _Traits> {
  428. public: // Types
  429. typedef _CharT char_type;
  430. typedef typename _Traits::int_type int_type;
  431. typedef typename _Traits::pos_type pos_type;
  432. typedef typename _Traits::off_type off_type;
  433. typedef _Traits traits_type;
  434. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  435. typedef basic_ostream<_CharT, _Traits> _Base;
  436. typedef basic_filebuf<_CharT, _Traits> _Buf;
  437. public: // Constructors, destructor.
  438. basic_ofstream() :
  439. basic_ios<_CharT, _Traits>(),
  440. basic_ostream<_CharT, _Traits>(0), _M_buf() {
  441. this->init(&_M_buf);
  442. }
  443. explicit basic_ofstream(const char* __s, ios_base::openmode __mod = ios_base::out)
  444. : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0), _M_buf() {
  445. this->init(&_M_buf);
  446. if (!_M_buf.open(__s, __mod | ios_base::out))
  447. this->setstate(ios_base::failbit);
  448. }
  449. #if !defined (_STLP_NO_EXTENSIONS)
  450. explicit basic_ofstream(int __id, ios_base::openmode __mod = ios_base::out)
  451. : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
  452. _M_buf() {
  453. this->init(&_M_buf);
  454. if (!_M_buf.open(__id, __mod | ios_base::out))
  455. this->setstate(ios_base::failbit);
  456. }
  457. basic_ofstream(const char* __s, ios_base::openmode __m, long __protection) :
  458. basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0), _M_buf() {
  459. this->init(&_M_buf);
  460. if (!_M_buf.open(__s, __m | ios_base::out, __protection))
  461. this->setstate(ios_base::failbit);
  462. }
  463. # if defined (_STLP_USE_WIN32_IO)
  464. explicit basic_ofstream(_STLP_fd __id, ios_base::openmode __mod = ios_base::out)
  465. : basic_ios<_CharT, _Traits>(), basic_ostream<_CharT, _Traits>(0),
  466. _M_buf() {
  467. this->init(&_M_buf);
  468. if (!_M_buf.open(__id, __mod | ios_base::out))
  469. this->setstate(ios_base::failbit);
  470. }
  471. # endif /* _STLP_USE_WIN32_IO */
  472. #endif
  473. ~basic_ofstream() {}
  474. public: // File and buffer operations.
  475. basic_filebuf<_CharT, _Traits>* rdbuf() const
  476. { return __CONST_CAST(_Buf*,&_M_buf); }
  477. bool is_open() {
  478. return this->rdbuf()->is_open();
  479. }
  480. void open(const char* __s, ios_base::openmode __mod= ios_base::out) {
  481. if (!this->rdbuf()->open(__s, __mod | ios_base::out))
  482. this->setstate(ios_base::failbit);
  483. }
  484. void close() {
  485. if (!this->rdbuf()->close())
  486. this->setstate(ios_base::failbit);
  487. }
  488. private:
  489. basic_filebuf<_CharT, _Traits> _M_buf;
  490. };
  491. //----------------------------------------------------------------------
  492. // Class basic_fstream<>
  493. template <class _CharT, class _Traits>
  494. class basic_fstream : public basic_iostream<_CharT, _Traits> {
  495. public: // Types
  496. typedef _CharT char_type;
  497. typedef typename _Traits::int_type int_type;
  498. typedef typename _Traits::pos_type pos_type;
  499. typedef typename _Traits::off_type off_type;
  500. typedef _Traits traits_type;
  501. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  502. typedef basic_iostream<_CharT, _Traits> _Base;
  503. typedef basic_filebuf<_CharT, _Traits> _Buf;
  504. public: // Constructors, destructor.
  505. basic_fstream()
  506. : basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
  507. this->init(&_M_buf);
  508. }
  509. explicit basic_fstream(const char* __s,
  510. ios_base::openmode __mod = ios_base::in | ios_base::out) :
  511. basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
  512. this->init(&_M_buf);
  513. if (!_M_buf.open(__s, __mod))
  514. this->setstate(ios_base::failbit);
  515. }
  516. #if !defined (_STLP_NO_EXTENSIONS)
  517. explicit basic_fstream(int __id,
  518. ios_base::openmode __mod = ios_base::in | ios_base::out) :
  519. basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
  520. this->init(&_M_buf);
  521. if (!_M_buf.open(__id, __mod))
  522. this->setstate(ios_base::failbit);
  523. }
  524. basic_fstream(const char* __s, ios_base::openmode __m, long __protection) :
  525. basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
  526. this->init(&_M_buf);
  527. if (!_M_buf.open(__s, __m, __protection))
  528. this->setstate(ios_base::failbit);
  529. }
  530. # if defined (_STLP_USE_WIN32_IO)
  531. explicit basic_fstream(_STLP_fd __id,
  532. ios_base::openmode __mod = ios_base::in | ios_base::out) :
  533. basic_ios<_CharT, _Traits>(), basic_iostream<_CharT, _Traits>(0), _M_buf() {
  534. this->init(&_M_buf);
  535. if (!_M_buf.open(__id, __mod))
  536. this->setstate(ios_base::failbit);
  537. }
  538. # endif /* _STLP_USE_WIN32_IO */
  539. #endif
  540. ~basic_fstream() {}
  541. public: // File and buffer operations.
  542. basic_filebuf<_CharT, _Traits>* rdbuf() const
  543. { return __CONST_CAST(_Buf*,&_M_buf); }
  544. bool is_open() {
  545. return this->rdbuf()->is_open();
  546. }
  547. void open(const char* __s,
  548. ios_base::openmode __mod =
  549. ios_base::in | ios_base::out) {
  550. if (!this->rdbuf()->open(__s, __mod))
  551. this->setstate(ios_base::failbit);
  552. }
  553. void close() {
  554. if (!this->rdbuf()->close())
  555. this->setstate(ios_base::failbit);
  556. }
  557. private:
  558. basic_filebuf<_CharT, _Traits> _M_buf;
  559. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  560. typedef basic_fstream<_CharT, _Traits> _Self;
  561. //explicitely defined as private to avoid warnings:
  562. basic_fstream(_Self const&);
  563. _Self& operator = (_Self const&);
  564. #endif
  565. };
  566. _STLP_END_NAMESPACE
  567. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  568. # include <stl/_fstream.c>
  569. #endif
  570. _STLP_BEGIN_NAMESPACE
  571. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  572. _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<char, char_traits<char> >;
  573. _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<char, char_traits<char> >;
  574. _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<char, char_traits<char> >;
  575. # if ! defined (_STLP_NO_WCHAR_T)
  576. _STLP_EXPORT_TEMPLATE_CLASS basic_ifstream<wchar_t, char_traits<wchar_t> >;
  577. _STLP_EXPORT_TEMPLATE_CLASS basic_ofstream<wchar_t, char_traits<wchar_t> >;
  578. _STLP_EXPORT_TEMPLATE_CLASS basic_fstream<wchar_t, char_traits<wchar_t> >;
  579. # endif
  580. #endif /* _STLP_USE_TEMPLATE_EXPORT */
  581. _STLP_END_NAMESPACE
  582. #endif /* _STLP_FSTREAM */
  583. // Local Variables:
  584. // mode:C++
  585. // End: