/project/jni/stlport/stlport/stl/_fstream.h

https://github.com/aichunyu/FFPlayer · C Header · 757 lines · 525 code · 139 blank · 93 comment · 62 complexity · 80bcf8dc705035817fff3d97b0b4be1e MD5 · raw file

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