fstream 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. // File based streams -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/fstream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.8 File-based streams
  25. //
  26. #ifndef _GLIBCXX_FSTREAM
  27. #define _GLIBCXX_FSTREAM 1
  28. #pragma GCC system_header
  29. #include <istream>
  30. #include <ostream>
  31. #include <bits/codecvt.h>
  32. #include <cstdio> // For BUFSIZ
  33. #include <bits/basic_file.h> // For __basic_file, __c_lock
  34. #if __cplusplus >= 201103L
  35. #include <string> // For std::string overloads.
  36. #endif
  37. // This can be overridden by the target's os_defines.h
  38. #ifndef _GLIBCXX_BUFSIZ
  39. # define _GLIBCXX_BUFSIZ BUFSIZ
  40. #endif
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. #if __cplusplus >= 201703L
  45. // Enable if _Path is a filesystem::path or experimental::filesystem::path
  46. template<typename _Path, typename _Result = _Path, typename _Path2
  47. = decltype(std::declval<_Path&>().make_preferred().filename())>
  48. using _If_fs_path = enable_if_t<is_same_v<_Path, _Path2>, _Result>;
  49. #endif // C++17
  50. // [27.8.1.1] template class basic_filebuf
  51. /**
  52. * @brief The actual work of input and output (for files).
  53. * @ingroup io
  54. *
  55. * @tparam _CharT Type of character stream.
  56. * @tparam _Traits Traits for character type, defaults to
  57. * char_traits<_CharT>.
  58. *
  59. * This class associates both its input and output sequence with an
  60. * external disk file, and maintains a joint file position for both
  61. * sequences. Many of its semantics are described in terms of similar
  62. * behavior in the Standard C Library's @c FILE streams.
  63. *
  64. * Requirements on traits_type, specific to this class:
  65. * - traits_type::pos_type must be fpos<traits_type::state_type>
  66. * - traits_type::off_type must be streamoff
  67. * - traits_type::state_type must be Assignable and DefaultConstructible,
  68. * - traits_type::state_type() must be the initial state for codecvt.
  69. */
  70. template<typename _CharT, typename _Traits>
  71. class basic_filebuf : public basic_streambuf<_CharT, _Traits>
  72. {
  73. #if __cplusplus >= 201103L
  74. template<typename _Tp>
  75. using __chk_state = __and_<is_copy_assignable<_Tp>,
  76. is_copy_constructible<_Tp>,
  77. is_default_constructible<_Tp>>;
  78. static_assert(__chk_state<typename _Traits::state_type>::value,
  79. "state_type must be CopyAssignable, CopyConstructible"
  80. " and DefaultConstructible");
  81. static_assert(is_same<typename _Traits::pos_type,
  82. fpos<typename _Traits::state_type>>::value,
  83. "pos_type must be fpos<state_type>");
  84. #endif
  85. public:
  86. // Types:
  87. typedef _CharT char_type;
  88. typedef _Traits traits_type;
  89. typedef typename traits_type::int_type int_type;
  90. typedef typename traits_type::pos_type pos_type;
  91. typedef typename traits_type::off_type off_type;
  92. typedef basic_streambuf<char_type, traits_type> __streambuf_type;
  93. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  94. typedef __basic_file<char> __file_type;
  95. typedef typename traits_type::state_type __state_type;
  96. typedef codecvt<char_type, char, __state_type> __codecvt_type;
  97. friend class ios_base; // For sync_with_stdio.
  98. protected:
  99. // Data Members:
  100. // MT lock inherited from libio or other low-level io library.
  101. __c_lock _M_lock;
  102. // External buffer.
  103. __file_type _M_file;
  104. /// Place to stash in || out || in | out settings for current filebuf.
  105. ios_base::openmode _M_mode;
  106. // Beginning state type for codecvt.
  107. __state_type _M_state_beg;
  108. // During output, the state that corresponds to pptr(),
  109. // during input, the state that corresponds to egptr() and
  110. // _M_ext_next.
  111. __state_type _M_state_cur;
  112. // Not used for output. During input, the state that corresponds
  113. // to eback() and _M_ext_buf.
  114. __state_type _M_state_last;
  115. /// Pointer to the beginning of internal buffer.
  116. char_type* _M_buf;
  117. /**
  118. * Actual size of internal buffer. This number is equal to the size
  119. * of the put area + 1 position, reserved for the overflow char of
  120. * a full area.
  121. */
  122. size_t _M_buf_size;
  123. // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
  124. bool _M_buf_allocated;
  125. /**
  126. * _M_reading == false && _M_writing == false for @b uncommitted mode;
  127. * _M_reading == true for @b read mode;
  128. * _M_writing == true for @b write mode;
  129. *
  130. * NB: _M_reading == true && _M_writing == true is unused.
  131. */
  132. bool _M_reading;
  133. bool _M_writing;
  134. ///@{
  135. /**
  136. * Necessary bits for putback buffer management.
  137. *
  138. * @note pbacks of over one character are not currently supported.
  139. */
  140. char_type _M_pback;
  141. char_type* _M_pback_cur_save;
  142. char_type* _M_pback_end_save;
  143. bool _M_pback_init;
  144. ///@}
  145. // Cached codecvt facet.
  146. const __codecvt_type* _M_codecvt;
  147. /**
  148. * Buffer for external characters. Used for input when
  149. * codecvt::always_noconv() == false. When valid, this corresponds
  150. * to eback().
  151. */
  152. char* _M_ext_buf;
  153. /**
  154. * Size of buffer held by _M_ext_buf.
  155. */
  156. streamsize _M_ext_buf_size;
  157. /**
  158. * Pointers into the buffer held by _M_ext_buf that delimit a
  159. * subsequence of bytes that have been read but not yet converted.
  160. * When valid, _M_ext_next corresponds to egptr().
  161. */
  162. const char* _M_ext_next;
  163. char* _M_ext_end;
  164. /**
  165. * Initializes pback buffers, and moves normal buffers to safety.
  166. * Assumptions:
  167. * _M_in_cur has already been moved back
  168. */
  169. void
  170. _M_create_pback()
  171. {
  172. if (!_M_pback_init)
  173. {
  174. _M_pback_cur_save = this->gptr();
  175. _M_pback_end_save = this->egptr();
  176. this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
  177. _M_pback_init = true;
  178. }
  179. }
  180. /**
  181. * Deactivates pback buffer contents, and restores normal buffer.
  182. * Assumptions:
  183. * The pback buffer has only moved forward.
  184. */
  185. void
  186. _M_destroy_pback() throw()
  187. {
  188. if (_M_pback_init)
  189. {
  190. // Length _M_in_cur moved in the pback buffer.
  191. _M_pback_cur_save += this->gptr() != this->eback();
  192. this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
  193. _M_pback_init = false;
  194. }
  195. }
  196. public:
  197. // Constructors/destructor:
  198. /**
  199. * @brief Does not open any files.
  200. *
  201. * The default constructor initializes the parent class using its
  202. * own default ctor.
  203. */
  204. basic_filebuf();
  205. #if __cplusplus >= 201103L
  206. basic_filebuf(const basic_filebuf&) = delete;
  207. basic_filebuf(basic_filebuf&&);
  208. #endif
  209. /**
  210. * @brief The destructor closes the file first.
  211. */
  212. virtual
  213. ~basic_filebuf()
  214. {
  215. __try
  216. { this->close(); }
  217. __catch(...)
  218. { }
  219. }
  220. #if __cplusplus >= 201103L
  221. basic_filebuf& operator=(const basic_filebuf&) = delete;
  222. basic_filebuf& operator=(basic_filebuf&&);
  223. void swap(basic_filebuf&);
  224. #endif
  225. // Members:
  226. /**
  227. * @brief Returns true if the external file is open.
  228. */
  229. bool
  230. is_open() const throw()
  231. { return _M_file.is_open(); }
  232. /**
  233. * @brief Opens an external file.
  234. * @param __s The name of the file.
  235. * @param __mode The open mode flags.
  236. * @return @c this on success, NULL on failure
  237. *
  238. * If a file is already open, this function immediately fails.
  239. * Otherwise it tries to open the file named @a __s using the flags
  240. * given in @a __mode.
  241. *
  242. * Table 92, adapted here, gives the relation between openmode
  243. * combinations and the equivalent @c fopen() flags.
  244. * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
  245. * and binary|in|app per DR 596)
  246. * <pre>
  247. * +---------------------------------------------------------+
  248. * | ios_base Flag combination stdio equivalent |
  249. * |binary in out trunc app |
  250. * +---------------------------------------------------------+
  251. * | + w |
  252. * | + + a |
  253. * | + a |
  254. * | + + w |
  255. * | + r |
  256. * | + + r+ |
  257. * | + + + w+ |
  258. * | + + + a+ |
  259. * | + + a+ |
  260. * +---------------------------------------------------------+
  261. * | + + wb |
  262. * | + + + ab |
  263. * | + + ab |
  264. * | + + + wb |
  265. * | + + rb |
  266. * | + + + r+b |
  267. * | + + + + w+b |
  268. * | + + + + a+b |
  269. * | + + + a+b |
  270. * +---------------------------------------------------------+
  271. * </pre>
  272. */
  273. __filebuf_type*
  274. open(const char* __s, ios_base::openmode __mode);
  275. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  276. /**
  277. * @brief Opens an external file.
  278. * @param __s The name of the file, as a wide character string.
  279. * @param __mode The open mode flags.
  280. * @return @c this on success, NULL on failure
  281. */
  282. __filebuf_type*
  283. open(const wchar_t* __s, ios_base::openmode __mode);
  284. #endif
  285. #if __cplusplus >= 201103L
  286. /**
  287. * @brief Opens an external file.
  288. * @param __s The name of the file.
  289. * @param __mode The open mode flags.
  290. * @return @c this on success, NULL on failure
  291. */
  292. __filebuf_type*
  293. open(const std::string& __s, ios_base::openmode __mode)
  294. { return open(__s.c_str(), __mode); }
  295. #if __cplusplus >= 201703L
  296. /**
  297. * @brief Opens an external file.
  298. * @param __s The name of the file, as a filesystem::path.
  299. * @param __mode The open mode flags.
  300. * @return @c this on success, NULL on failure
  301. */
  302. template<typename _Path>
  303. _If_fs_path<_Path, __filebuf_type*>
  304. open(const _Path& __s, ios_base::openmode __mode)
  305. { return open(__s.c_str(), __mode); }
  306. #endif // C++17
  307. #endif // C++11
  308. /**
  309. * @brief Closes the currently associated file.
  310. * @return @c this on success, NULL on failure
  311. *
  312. * If no file is currently open, this function immediately fails.
  313. *
  314. * If a <em>put buffer area</em> exists, @c overflow(eof) is
  315. * called to flush all the characters. The file is then
  316. * closed.
  317. *
  318. * If any operations fail, this function also fails.
  319. */
  320. __filebuf_type*
  321. close();
  322. protected:
  323. void
  324. _M_allocate_internal_buffer();
  325. void
  326. _M_destroy_internal_buffer() throw();
  327. // [27.8.1.4] overridden virtual functions
  328. virtual streamsize
  329. showmanyc();
  330. // Stroustrup, 1998, p. 628
  331. // underflow() and uflow() functions are called to get the next
  332. // character from the real input source when the buffer is empty.
  333. // Buffered input uses underflow()
  334. virtual int_type
  335. underflow();
  336. virtual int_type
  337. pbackfail(int_type __c = _Traits::eof());
  338. // Stroustrup, 1998, p 648
  339. // The overflow() function is called to transfer characters to the
  340. // real output destination when the buffer is full. A call to
  341. // overflow(c) outputs the contents of the buffer plus the
  342. // character c.
  343. // 27.5.2.4.5
  344. // Consume some sequence of the characters in the pending sequence.
  345. virtual int_type
  346. overflow(int_type __c = _Traits::eof());
  347. // Convert internal byte sequence to external, char-based
  348. // sequence via codecvt.
  349. bool
  350. _M_convert_to_external(char_type*, streamsize);
  351. /**
  352. * @brief Manipulates the buffer.
  353. * @param __s Pointer to a buffer area.
  354. * @param __n Size of @a __s.
  355. * @return @c this
  356. *
  357. * If no file has been opened, and both @a __s and @a __n are zero, then
  358. * the stream becomes unbuffered. Otherwise, @c __s is used as a
  359. * buffer; see
  360. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  361. * for more.
  362. */
  363. virtual __streambuf_type*
  364. setbuf(char_type* __s, streamsize __n);
  365. virtual pos_type
  366. seekoff(off_type __off, ios_base::seekdir __way,
  367. ios_base::openmode __mode = ios_base::in | ios_base::out);
  368. virtual pos_type
  369. seekpos(pos_type __pos,
  370. ios_base::openmode __mode = ios_base::in | ios_base::out);
  371. // Common code for seekoff, seekpos, and overflow
  372. pos_type
  373. _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
  374. int
  375. _M_get_ext_pos(__state_type &__state);
  376. virtual int
  377. sync();
  378. virtual void
  379. imbue(const locale& __loc);
  380. virtual streamsize
  381. xsgetn(char_type* __s, streamsize __n);
  382. virtual streamsize
  383. xsputn(const char_type* __s, streamsize __n);
  384. // Flushes output buffer, then writes unshift sequence.
  385. bool
  386. _M_terminate_output();
  387. /**
  388. * This function sets the pointers of the internal buffer, both get
  389. * and put areas. Typically:
  390. *
  391. * __off == egptr() - eback() upon underflow/uflow (@b read mode);
  392. * __off == 0 upon overflow (@b write mode);
  393. * __off == -1 upon open, setbuf, seekoff/pos (@b uncommitted mode).
  394. *
  395. * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
  396. * reflects the actual allocated memory and the last cell is reserved
  397. * for the overflow char of a full put area.
  398. */
  399. void
  400. _M_set_buffer(streamsize __off)
  401. {
  402. const bool __testin = _M_mode & ios_base::in;
  403. const bool __testout = (_M_mode & ios_base::out
  404. || _M_mode & ios_base::app);
  405. if (__testin && __off > 0)
  406. this->setg(_M_buf, _M_buf, _M_buf + __off);
  407. else
  408. this->setg(_M_buf, _M_buf, _M_buf);
  409. if (__testout && __off == 0 && _M_buf_size > 1 )
  410. this->setp(_M_buf, _M_buf + _M_buf_size - 1);
  411. else
  412. this->setp(0, 0);
  413. }
  414. };
  415. // [27.8.1.5] Template class basic_ifstream
  416. /**
  417. * @brief Controlling input for files.
  418. * @ingroup io
  419. *
  420. * @tparam _CharT Type of character stream.
  421. * @tparam _Traits Traits for character type, defaults to
  422. * char_traits<_CharT>.
  423. *
  424. * This class supports reading from named files, using the inherited
  425. * functions from std::basic_istream. To control the associated
  426. * sequence, an instance of std::basic_filebuf is used, which this page
  427. * refers to as @c sb.
  428. */
  429. template<typename _CharT, typename _Traits>
  430. class basic_ifstream : public basic_istream<_CharT, _Traits>
  431. {
  432. public:
  433. // Types:
  434. typedef _CharT char_type;
  435. typedef _Traits traits_type;
  436. typedef typename traits_type::int_type int_type;
  437. typedef typename traits_type::pos_type pos_type;
  438. typedef typename traits_type::off_type off_type;
  439. // Non-standard types:
  440. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  441. typedef basic_istream<char_type, traits_type> __istream_type;
  442. private:
  443. __filebuf_type _M_filebuf;
  444. public:
  445. // Constructors/Destructors:
  446. /**
  447. * @brief Default constructor.
  448. *
  449. * Initializes @c sb using its default constructor, and passes
  450. * @c &sb to the base class initializer. Does not open any files
  451. * (you haven't given it a filename to open).
  452. */
  453. basic_ifstream() : __istream_type(), _M_filebuf()
  454. { this->init(&_M_filebuf); }
  455. /**
  456. * @brief Create an input file stream.
  457. * @param __s Null terminated string specifying the filename.
  458. * @param __mode Open file in specified mode (see std::ios_base).
  459. *
  460. * @c ios_base::in is automatically included in @a __mode.
  461. */
  462. explicit
  463. basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
  464. : __istream_type(), _M_filebuf()
  465. {
  466. this->init(&_M_filebuf);
  467. this->open(__s, __mode);
  468. }
  469. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  470. /**
  471. * @param Create an input file stream.
  472. * @param __s Wide string specifying the filename.
  473. * @param __mode Open file in specified mode (see std::ios_base).
  474. *
  475. * @c ios_base::in is automatically included in @a __mode.
  476. */
  477. basic_ifstream(const wchar_t* __s,
  478. ios_base::openmode __mode = ios_base::in)
  479. : __istream_type(), _M_filebuf()
  480. {
  481. this->init(&_M_filebuf);
  482. this->open(__s, __mode);
  483. }
  484. #endif
  485. #if __cplusplus >= 201103L
  486. /**
  487. * @brief Create an input file stream.
  488. * @param __s std::string specifying the filename.
  489. * @param __mode Open file in specified mode (see std::ios_base).
  490. *
  491. * @c ios_base::in is automatically included in @a __mode.
  492. */
  493. explicit
  494. basic_ifstream(const std::string& __s,
  495. ios_base::openmode __mode = ios_base::in)
  496. : __istream_type(), _M_filebuf()
  497. {
  498. this->init(&_M_filebuf);
  499. this->open(__s, __mode);
  500. }
  501. #if __cplusplus >= 201703L
  502. /**
  503. * @brief Create an input file stream.
  504. * @param __s filesystem::path specifying the filename.
  505. * @param __mode Open file in specified mode (see std::ios_base).
  506. *
  507. * @c ios_base::in is automatically included in @a __mode.
  508. */
  509. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  510. basic_ifstream(const _Path& __s,
  511. ios_base::openmode __mode = ios_base::in)
  512. : basic_ifstream(__s.c_str(), __mode)
  513. { }
  514. #endif // C++17
  515. basic_ifstream(const basic_ifstream&) = delete;
  516. basic_ifstream(basic_ifstream&& __rhs)
  517. : __istream_type(std::move(__rhs)),
  518. _M_filebuf(std::move(__rhs._M_filebuf))
  519. { __istream_type::set_rdbuf(&_M_filebuf); }
  520. #endif // C++11
  521. /**
  522. * @brief The destructor does nothing.
  523. *
  524. * The file is closed by the filebuf object, not the formatting
  525. * stream.
  526. */
  527. ~basic_ifstream()
  528. { }
  529. #if __cplusplus >= 201103L
  530. // 27.8.3.2 Assign and swap:
  531. basic_ifstream&
  532. operator=(const basic_ifstream&) = delete;
  533. basic_ifstream&
  534. operator=(basic_ifstream&& __rhs)
  535. {
  536. __istream_type::operator=(std::move(__rhs));
  537. _M_filebuf = std::move(__rhs._M_filebuf);
  538. return *this;
  539. }
  540. void
  541. swap(basic_ifstream& __rhs)
  542. {
  543. __istream_type::swap(__rhs);
  544. _M_filebuf.swap(__rhs._M_filebuf);
  545. }
  546. #endif
  547. // Members:
  548. /**
  549. * @brief Accessing the underlying buffer.
  550. * @return The current basic_filebuf buffer.
  551. *
  552. * This hides both signatures of std::basic_ios::rdbuf().
  553. */
  554. __filebuf_type*
  555. rdbuf() const
  556. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  557. /**
  558. * @brief Wrapper to test for an open file.
  559. * @return @c rdbuf()->is_open()
  560. */
  561. bool
  562. is_open()
  563. { return _M_filebuf.is_open(); }
  564. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  565. // 365. Lack of const-qualification in clause 27
  566. bool
  567. is_open() const
  568. { return _M_filebuf.is_open(); }
  569. /**
  570. * @brief Opens an external file.
  571. * @param __s The name of the file.
  572. * @param __mode The open mode flags.
  573. *
  574. * Calls @c std::basic_filebuf::open(s,__mode|in). If that function
  575. * fails, @c failbit is set in the stream's error state.
  576. */
  577. void
  578. open(const char* __s, ios_base::openmode __mode = ios_base::in)
  579. {
  580. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  581. this->setstate(ios_base::failbit);
  582. else
  583. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  584. // 409. Closing an fstream should clear error state
  585. this->clear();
  586. }
  587. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  588. /**
  589. * @brief Opens an external file.
  590. * @param __s The name of the file, as a wide character string.
  591. * @param __mode The open mode flags.
  592. *
  593. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  594. * fails, @c failbit is set in the stream's error state.
  595. */
  596. void
  597. open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in)
  598. {
  599. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  600. this->setstate(ios_base::failbit);
  601. else
  602. this->clear();
  603. }
  604. #endif
  605. #if __cplusplus >= 201103L
  606. /**
  607. * @brief Opens an external file.
  608. * @param __s The name of the file.
  609. * @param __mode The open mode flags.
  610. *
  611. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  612. * fails, @c failbit is set in the stream's error state.
  613. */
  614. void
  615. open(const std::string& __s, ios_base::openmode __mode = ios_base::in)
  616. {
  617. if (!_M_filebuf.open(__s, __mode | ios_base::in))
  618. this->setstate(ios_base::failbit);
  619. else
  620. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  621. // 409. Closing an fstream should clear error state
  622. this->clear();
  623. }
  624. #if __cplusplus >= 201703L
  625. /**
  626. * @brief Opens an external file.
  627. * @param __s The name of the file, as a filesystem::path.
  628. * @param __mode The open mode flags.
  629. *
  630. * Calls @c std::basic_filebuf::open(__s,__mode|in). If that function
  631. * fails, @c failbit is set in the stream's error state.
  632. */
  633. template<typename _Path>
  634. _If_fs_path<_Path, void>
  635. open(const _Path& __s, ios_base::openmode __mode = ios_base::in)
  636. { open(__s.c_str(), __mode); }
  637. #endif // C++17
  638. #endif // C++11
  639. /**
  640. * @brief Close the file.
  641. *
  642. * Calls @c std::basic_filebuf::close(). If that function
  643. * fails, @c failbit is set in the stream's error state.
  644. */
  645. void
  646. close()
  647. {
  648. if (!_M_filebuf.close())
  649. this->setstate(ios_base::failbit);
  650. }
  651. };
  652. // [27.8.1.8] Template class basic_ofstream
  653. /**
  654. * @brief Controlling output for files.
  655. * @ingroup io
  656. *
  657. * @tparam _CharT Type of character stream.
  658. * @tparam _Traits Traits for character type, defaults to
  659. * char_traits<_CharT>.
  660. *
  661. * This class supports reading from named files, using the inherited
  662. * functions from std::basic_ostream. To control the associated
  663. * sequence, an instance of std::basic_filebuf is used, which this page
  664. * refers to as @c sb.
  665. */
  666. template<typename _CharT, typename _Traits>
  667. class basic_ofstream : public basic_ostream<_CharT,_Traits>
  668. {
  669. public:
  670. // Types:
  671. typedef _CharT char_type;
  672. typedef _Traits traits_type;
  673. typedef typename traits_type::int_type int_type;
  674. typedef typename traits_type::pos_type pos_type;
  675. typedef typename traits_type::off_type off_type;
  676. // Non-standard types:
  677. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  678. typedef basic_ostream<char_type, traits_type> __ostream_type;
  679. private:
  680. __filebuf_type _M_filebuf;
  681. public:
  682. // Constructors:
  683. /**
  684. * @brief Default constructor.
  685. *
  686. * Initializes @c sb using its default constructor, and passes
  687. * @c &sb to the base class initializer. Does not open any files
  688. * (you haven't given it a filename to open).
  689. */
  690. basic_ofstream(): __ostream_type(), _M_filebuf()
  691. { this->init(&_M_filebuf); }
  692. /**
  693. * @brief Create an output file stream.
  694. * @param __s Null terminated string specifying the filename.
  695. * @param __mode Open file in specified mode (see std::ios_base).
  696. *
  697. * @c ios_base::out is automatically included in @a __mode.
  698. */
  699. explicit
  700. basic_ofstream(const char* __s,
  701. ios_base::openmode __mode = ios_base::out)
  702. : __ostream_type(), _M_filebuf()
  703. {
  704. this->init(&_M_filebuf);
  705. this->open(__s, __mode);
  706. }
  707. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  708. /**
  709. * @param Create an output file stream.
  710. * @param __s Wide string specifying the filename.
  711. * @param __mode Open file in specified mode (see std::ios_base).
  712. *
  713. * @c ios_base::out | @c ios_base::trunc is automatically included in
  714. * @a __mode.
  715. */
  716. basic_ofstream(const wchar_t* __s,
  717. ios_base::openmode __mode = ios_base::out|ios_base::trunc)
  718. : __ostream_type(), _M_filebuf()
  719. {
  720. this->init(&_M_filebuf);
  721. this->open(__s, __mode);
  722. }
  723. #endif
  724. #if __cplusplus >= 201103L
  725. /**
  726. * @brief Create an output file stream.
  727. * @param __s std::string specifying the filename.
  728. * @param __mode Open file in specified mode (see std::ios_base).
  729. *
  730. * @c ios_base::out is automatically included in @a __mode.
  731. */
  732. explicit
  733. basic_ofstream(const std::string& __s,
  734. ios_base::openmode __mode = ios_base::out)
  735. : __ostream_type(), _M_filebuf()
  736. {
  737. this->init(&_M_filebuf);
  738. this->open(__s, __mode);
  739. }
  740. #if __cplusplus >= 201703L
  741. /**
  742. * @brief Create an output file stream.
  743. * @param __s filesystem::path specifying the filename.
  744. * @param __mode Open file in specified mode (see std::ios_base).
  745. *
  746. * @c ios_base::out is automatically included in @a __mode.
  747. */
  748. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  749. basic_ofstream(const _Path& __s,
  750. ios_base::openmode __mode = ios_base::out)
  751. : basic_ofstream(__s.c_str(), __mode)
  752. { }
  753. #endif // C++17
  754. basic_ofstream(const basic_ofstream&) = delete;
  755. basic_ofstream(basic_ofstream&& __rhs)
  756. : __ostream_type(std::move(__rhs)),
  757. _M_filebuf(std::move(__rhs._M_filebuf))
  758. { __ostream_type::set_rdbuf(&_M_filebuf); }
  759. #endif
  760. /**
  761. * @brief The destructor does nothing.
  762. *
  763. * The file is closed by the filebuf object, not the formatting
  764. * stream.
  765. */
  766. ~basic_ofstream()
  767. { }
  768. #if __cplusplus >= 201103L
  769. // 27.8.3.2 Assign and swap:
  770. basic_ofstream&
  771. operator=(const basic_ofstream&) = delete;
  772. basic_ofstream&
  773. operator=(basic_ofstream&& __rhs)
  774. {
  775. __ostream_type::operator=(std::move(__rhs));
  776. _M_filebuf = std::move(__rhs._M_filebuf);
  777. return *this;
  778. }
  779. void
  780. swap(basic_ofstream& __rhs)
  781. {
  782. __ostream_type::swap(__rhs);
  783. _M_filebuf.swap(__rhs._M_filebuf);
  784. }
  785. #endif
  786. // Members:
  787. /**
  788. * @brief Accessing the underlying buffer.
  789. * @return The current basic_filebuf buffer.
  790. *
  791. * This hides both signatures of std::basic_ios::rdbuf().
  792. */
  793. __filebuf_type*
  794. rdbuf() const
  795. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  796. /**
  797. * @brief Wrapper to test for an open file.
  798. * @return @c rdbuf()->is_open()
  799. */
  800. bool
  801. is_open()
  802. { return _M_filebuf.is_open(); }
  803. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  804. // 365. Lack of const-qualification in clause 27
  805. bool
  806. is_open() const
  807. { return _M_filebuf.is_open(); }
  808. /**
  809. * @brief Opens an external file.
  810. * @param __s The name of the file.
  811. * @param __mode The open mode flags.
  812. *
  813. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  814. * function fails, @c failbit is set in the stream's error state.
  815. */
  816. void
  817. open(const char* __s, ios_base::openmode __mode = ios_base::out)
  818. {
  819. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  820. this->setstate(ios_base::failbit);
  821. else
  822. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  823. // 409. Closing an fstream should clear error state
  824. this->clear();
  825. }
  826. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  827. /**
  828. * @brief Opens an external file.
  829. * @param __s The name of the file.
  830. * @param __mode The open mode flags.
  831. *
  832. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  833. * function fails, @c failbit is set in the stream's error state.
  834. */
  835. void
  836. open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out)
  837. {
  838. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  839. this->setstate(ios_base::failbit);
  840. else
  841. this->clear();
  842. }
  843. #endif
  844. #if __cplusplus >= 201103L
  845. /**
  846. * @brief Opens an external file.
  847. * @param __s The name of the file.
  848. * @param __mode The open mode flags.
  849. *
  850. * Calls @c std::basic_filebuf::open(s,mode|out). If that
  851. * function fails, @c failbit is set in the stream's error state.
  852. */
  853. void
  854. open(const std::string& __s, ios_base::openmode __mode = ios_base::out)
  855. {
  856. if (!_M_filebuf.open(__s, __mode | ios_base::out))
  857. this->setstate(ios_base::failbit);
  858. else
  859. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  860. // 409. Closing an fstream should clear error state
  861. this->clear();
  862. }
  863. #if __cplusplus >= 201703L
  864. /**
  865. * @brief Opens an external file.
  866. * @param __s The name of the file, as a filesystem::path.
  867. * @param __mode The open mode flags.
  868. *
  869. * Calls @c std::basic_filebuf::open(__s,__mode|out). If that
  870. * function fails, @c failbit is set in the stream's error state.
  871. */
  872. template<typename _Path>
  873. _If_fs_path<_Path, void>
  874. open(const _Path& __s, ios_base::openmode __mode = ios_base::out)
  875. { open(__s.c_str(), __mode); }
  876. #endif // C++17
  877. #endif // C++11
  878. /**
  879. * @brief Close the file.
  880. *
  881. * Calls @c std::basic_filebuf::close(). If that function
  882. * fails, @c failbit is set in the stream's error state.
  883. */
  884. void
  885. close()
  886. {
  887. if (!_M_filebuf.close())
  888. this->setstate(ios_base::failbit);
  889. }
  890. };
  891. // [27.8.1.11] Template class basic_fstream
  892. /**
  893. * @brief Controlling input and output for files.
  894. * @ingroup io
  895. *
  896. * @tparam _CharT Type of character stream.
  897. * @tparam _Traits Traits for character type, defaults to
  898. * char_traits<_CharT>.
  899. *
  900. * This class supports reading from and writing to named files, using
  901. * the inherited functions from std::basic_iostream. To control the
  902. * associated sequence, an instance of std::basic_filebuf is used, which
  903. * this page refers to as @c sb.
  904. */
  905. template<typename _CharT, typename _Traits>
  906. class basic_fstream : public basic_iostream<_CharT, _Traits>
  907. {
  908. public:
  909. // Types:
  910. typedef _CharT char_type;
  911. typedef _Traits traits_type;
  912. typedef typename traits_type::int_type int_type;
  913. typedef typename traits_type::pos_type pos_type;
  914. typedef typename traits_type::off_type off_type;
  915. // Non-standard types:
  916. typedef basic_filebuf<char_type, traits_type> __filebuf_type;
  917. typedef basic_ios<char_type, traits_type> __ios_type;
  918. typedef basic_iostream<char_type, traits_type> __iostream_type;
  919. private:
  920. __filebuf_type _M_filebuf;
  921. public:
  922. // Constructors/destructor:
  923. /**
  924. * @brief Default constructor.
  925. *
  926. * Initializes @c sb using its default constructor, and passes
  927. * @c &sb to the base class initializer. Does not open any files
  928. * (you haven't given it a filename to open).
  929. */
  930. basic_fstream()
  931. : __iostream_type(), _M_filebuf()
  932. { this->init(&_M_filebuf); }
  933. /**
  934. * @brief Create an input/output file stream.
  935. * @param __s Null terminated string specifying the filename.
  936. * @param __mode Open file in specified mode (see std::ios_base).
  937. */
  938. explicit
  939. basic_fstream(const char* __s,
  940. ios_base::openmode __mode = ios_base::in | ios_base::out)
  941. : __iostream_type(0), _M_filebuf()
  942. {
  943. this->init(&_M_filebuf);
  944. this->open(__s, __mode);
  945. }
  946. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  947. /**
  948. * @param Create an input/output file stream.
  949. * @param __s Wide string specifying the filename.
  950. * @param __mode Open file in specified mode (see std::ios_base).
  951. */
  952. basic_fstream(const wchar_t* __s,
  953. ios_base::openmode __mode = ios_base::in | ios_base::out)
  954. : __iostream_type(0), _M_filebuf()
  955. {
  956. this->init(&_M_filebuf);
  957. this->open(__s, __mode);
  958. }
  959. #endif
  960. #if __cplusplus >= 201103L
  961. /**
  962. * @brief Create an input/output file stream.
  963. * @param __s Null terminated string specifying the filename.
  964. * @param __mode Open file in specified mode (see std::ios_base).
  965. */
  966. explicit
  967. basic_fstream(const std::string& __s,
  968. ios_base::openmode __mode = ios_base::in | ios_base::out)
  969. : __iostream_type(0), _M_filebuf()
  970. {
  971. this->init(&_M_filebuf);
  972. this->open(__s, __mode);
  973. }
  974. #if __cplusplus >= 201703L
  975. /**
  976. * @brief Create an input/output file stream.
  977. * @param __s filesystem::path specifying the filename.
  978. * @param __mode Open file in specified mode (see std::ios_base).
  979. */
  980. template<typename _Path, typename _Require = _If_fs_path<_Path>>
  981. basic_fstream(const _Path& __s,
  982. ios_base::openmode __mode = ios_base::in | ios_base::out)
  983. : basic_fstream(__s.c_str(), __mode)
  984. { }
  985. #endif // C++17
  986. basic_fstream(const basic_fstream&) = delete;
  987. basic_fstream(basic_fstream&& __rhs)
  988. : __iostream_type(std::move(__rhs)),
  989. _M_filebuf(std::move(__rhs._M_filebuf))
  990. { __iostream_type::set_rdbuf(&_M_filebuf); }
  991. #endif
  992. /**
  993. * @brief The destructor does nothing.
  994. *
  995. * The file is closed by the filebuf object, not the formatting
  996. * stream.
  997. */
  998. ~basic_fstream()
  999. { }
  1000. #if __cplusplus >= 201103L
  1001. // 27.8.3.2 Assign and swap:
  1002. basic_fstream&
  1003. operator=(const basic_fstream&) = delete;
  1004. basic_fstream&
  1005. operator=(basic_fstream&& __rhs)
  1006. {
  1007. __iostream_type::operator=(std::move(__rhs));
  1008. _M_filebuf = std::move(__rhs._M_filebuf);
  1009. return *this;
  1010. }
  1011. void
  1012. swap(basic_fstream& __rhs)
  1013. {
  1014. __iostream_type::swap(__rhs);
  1015. _M_filebuf.swap(__rhs._M_filebuf);
  1016. }
  1017. #endif
  1018. // Members:
  1019. /**
  1020. * @brief Accessing the underlying buffer.
  1021. * @return The current basic_filebuf buffer.
  1022. *
  1023. * This hides both signatures of std::basic_ios::rdbuf().
  1024. */
  1025. __filebuf_type*
  1026. rdbuf() const
  1027. { return const_cast<__filebuf_type*>(&_M_filebuf); }
  1028. /**
  1029. * @brief Wrapper to test for an open file.
  1030. * @return @c rdbuf()->is_open()
  1031. */
  1032. bool
  1033. is_open()
  1034. { return _M_filebuf.is_open(); }
  1035. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1036. // 365. Lack of const-qualification in clause 27
  1037. bool
  1038. is_open() const
  1039. { return _M_filebuf.is_open(); }
  1040. /**
  1041. * @brief Opens an external file.
  1042. * @param __s The name of the file.
  1043. * @param __mode The open mode flags.
  1044. *
  1045. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1046. * function fails, @c failbit is set in the stream's error state.
  1047. */
  1048. void
  1049. open(const char* __s,
  1050. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1051. {
  1052. if (!_M_filebuf.open(__s, __mode))
  1053. this->setstate(ios_base::failbit);
  1054. else
  1055. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1056. // 409. Closing an fstream should clear error state
  1057. this->clear();
  1058. }
  1059. #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
  1060. /**
  1061. * @brief Opens an external file.
  1062. * @param __s The name of the file.
  1063. * @param __mode The open mode flags.
  1064. *
  1065. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1066. * function fails, @c failbit is set in the stream's error state.
  1067. */
  1068. void
  1069. open(const wchar_t* __s,
  1070. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1071. {
  1072. if (!_M_filebuf.open(__s, __mode))
  1073. this->setstate(ios_base::failbit);
  1074. else
  1075. this->clear();
  1076. }
  1077. #endif
  1078. #if __cplusplus >= 201103L
  1079. /**
  1080. * @brief Opens an external file.
  1081. * @param __s The name of the file.
  1082. * @param __mode The open mode flags.
  1083. *
  1084. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1085. * function fails, @c failbit is set in the stream's error state.
  1086. */
  1087. void
  1088. open(const std::string& __s,
  1089. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1090. {
  1091. if (!_M_filebuf.open(__s, __mode))
  1092. this->setstate(ios_base::failbit);
  1093. else
  1094. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1095. // 409. Closing an fstream should clear error state
  1096. this->clear();
  1097. }
  1098. #if __cplusplus >= 201703L
  1099. /**
  1100. * @brief Opens an external file.
  1101. * @param __s The name of the file, as a filesystem::path.
  1102. * @param __mode The open mode flags.
  1103. *
  1104. * Calls @c std::basic_filebuf::open(__s,__mode). If that
  1105. * function fails, @c failbit is set in the stream's error state.
  1106. */
  1107. template<typename _Path>
  1108. _If_fs_path<_Path, void>
  1109. open(const _Path& __s,
  1110. ios_base::openmode __mode = ios_base::in | ios_base::out)
  1111. { open(__s.c_str(), __mode); }
  1112. #endif // C++17
  1113. #endif // C++11
  1114. /**
  1115. * @brief Close the file.
  1116. *
  1117. * Calls @c std::basic_filebuf::close(). If that function
  1118. * fails, @c failbit is set in the stream's error state.
  1119. */
  1120. void
  1121. close()
  1122. {
  1123. if (!_M_filebuf.close())
  1124. this->setstate(ios_base::failbit);
  1125. }
  1126. };
  1127. #if __cplusplus >= 201103L
  1128. /// Swap specialization for filebufs.
  1129. template <class _CharT, class _Traits>
  1130. inline void
  1131. swap(basic_filebuf<_CharT, _Traits>& __x,
  1132. basic_filebuf<_CharT, _Traits>& __y)
  1133. { __x.swap(__y); }
  1134. /// Swap specialization for ifstreams.
  1135. template <class _CharT, class _Traits>
  1136. inline void
  1137. swap(basic_ifstream<_CharT, _Traits>& __x,
  1138. basic_ifstream<_CharT, _Traits>& __y)
  1139. { __x.swap(__y); }
  1140. /// Swap specialization for ofstreams.
  1141. template <class _CharT, class _Traits>
  1142. inline void
  1143. swap(basic_ofstream<_CharT, _Traits>& __x,
  1144. basic_ofstream<_CharT, _Traits>& __y)
  1145. { __x.swap(__y); }
  1146. /// Swap specialization for fstreams.
  1147. template <class _CharT, class _Traits>
  1148. inline void
  1149. swap(basic_fstream<_CharT, _Traits>& __x,
  1150. basic_fstream<_CharT, _Traits>& __y)
  1151. { __x.swap(__y); }
  1152. #endif
  1153. _GLIBCXX_END_NAMESPACE_VERSION
  1154. } // namespace
  1155. #include <bits/fstream.tcc>
  1156. #endif /* _GLIBCXX_FSTREAM */