annotate libstdc++-v3/include/std/fstream @ 158:494b0b89df80 default tip

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