Branch data Line data Source code
1 : : // File based streams -*- C++ -*-
2 : :
3 : : // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 : : // 2006, 2007, 2008
5 : : // Free Software Foundation, Inc.
6 : : //
7 : : // This file is part of the GNU ISO C++ Library. This library is free
8 : : // software; you can redistribute it and/or modify it under the
9 : : // terms of the GNU General Public License as published by the
10 : : // Free Software Foundation; either version 2, or (at your option)
11 : : // any later version.
12 : :
13 : : // This library is distributed in the hope that it will be useful,
14 : : // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : // GNU General Public License for more details.
17 : :
18 : : // You should have received a copy of the GNU General Public License
19 : : // along with this library; see the file COPYING. If not, write to
20 : : // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 : : // Boston, MA 02110-1301, USA.
22 : :
23 : : // As a special exception, you may use this file as part of a free software
24 : : // library without restriction. Specifically, if other files instantiate
25 : : // templates or use macros or inline functions from this file, or you compile
26 : : // this file and link it with other files to produce an executable, this
27 : : // file does not by itself cause the resulting executable to be covered by
28 : : // the GNU General Public License. This exception does not however
29 : : // invalidate any other reasons why the executable file might be covered by
30 : : // the GNU General Public License.
31 : :
32 : : /** @file fstream
33 : : * This is a Standard C++ Library header.
34 : : */
35 : :
36 : : //
37 : : // ISO C++ 14882: 27.8 File-based streams
38 : : //
39 : :
40 : : #ifndef _GLIBCXX_FSTREAM
41 : : #define _GLIBCXX_FSTREAM 1
42 : :
43 : : #pragma GCC system_header
44 : :
45 : : #include <istream>
46 : : #include <ostream>
47 : : #include <bits/codecvt.h>
48 : : #include <cstdio> // For BUFSIZ
49 : : #include <bits/basic_file.h> // For __basic_file, __c_lock
50 : :
51 : : _GLIBCXX_BEGIN_NAMESPACE(std)
52 : :
53 : : // [27.8.1.1] template class basic_filebuf
54 : : /**
55 : : * @brief The actual work of input and output (for files).
56 : : *
57 : : * This class associates both its input and output sequence with an
58 : : * external disk file, and maintains a joint file position for both
59 : : * sequences. Many of its semantics are described in terms of similar
60 : : * behavior in the Standard C Library's @c FILE streams.
61 : : */
62 : : // Requirements on traits_type, specific to this class:
63 : : // traits_type::pos_type must be fpos<traits_type::state_type>
64 : : // traits_type::off_type must be streamoff
65 : : // traits_type::state_type must be Assignable and DefaultConstructible,
66 : : // and traits_type::state_type() must be the initial state for codecvt.
67 : : template<typename _CharT, typename _Traits>
68 : : class basic_filebuf : public basic_streambuf<_CharT, _Traits>
69 : : {
70 : : public:
71 : : // Types:
72 : : typedef _CharT char_type;
73 : : typedef _Traits traits_type;
74 : : typedef typename traits_type::int_type int_type;
75 : : typedef typename traits_type::pos_type pos_type;
76 : : typedef typename traits_type::off_type off_type;
77 : :
78 : : typedef basic_streambuf<char_type, traits_type> __streambuf_type;
79 : : typedef basic_filebuf<char_type, traits_type> __filebuf_type;
80 : : typedef __basic_file<char> __file_type;
81 : : typedef typename traits_type::state_type __state_type;
82 : : typedef codecvt<char_type, char, __state_type> __codecvt_type;
83 : :
84 : : friend class ios_base; // For sync_with_stdio.
85 : :
86 : : protected:
87 : : // Data Members:
88 : : // MT lock inherited from libio or other low-level io library.
89 : : __c_lock _M_lock;
90 : :
91 : : // External buffer.
92 : : __file_type _M_file;
93 : :
94 : : /// Place to stash in || out || in | out settings for current filebuf.
95 : : ios_base::openmode _M_mode;
96 : :
97 : : // Beginning state type for codecvt.
98 : : __state_type _M_state_beg;
99 : :
100 : : // During output, the state that corresponds to pptr(),
101 : : // during input, the state that corresponds to egptr() and
102 : : // _M_ext_next.
103 : : __state_type _M_state_cur;
104 : :
105 : : // Not used for output. During input, the state that corresponds
106 : : // to eback() and _M_ext_buf.
107 : : __state_type _M_state_last;
108 : :
109 : : /// Pointer to the beginning of internal buffer.
110 : : char_type* _M_buf;
111 : :
112 : : /**
113 : : * Actual size of internal buffer. This number is equal to the size
114 : : * of the put area + 1 position, reserved for the overflow char of
115 : : * a full area.
116 : : */
117 : : size_t _M_buf_size;
118 : :
119 : : // Set iff _M_buf is allocated memory from _M_allocate_internal_buffer.
120 : : bool _M_buf_allocated;
121 : :
122 : : /**
123 : : * _M_reading == false && _M_writing == false for 'uncommitted' mode;
124 : : * _M_reading == true for 'read' mode;
125 : : * _M_writing == true for 'write' mode;
126 : : *
127 : : * NB: _M_reading == true && _M_writing == true is unused.
128 : : */
129 : : bool _M_reading;
130 : : bool _M_writing;
131 : :
132 : : //@{
133 : : /**
134 : : * Necessary bits for putback buffer management.
135 : : *
136 : : * @note pbacks of over one character are not currently supported.
137 : : */
138 : : char_type _M_pback;
139 : : char_type* _M_pback_cur_save;
140 : : char_type* _M_pback_end_save;
141 : : bool _M_pback_init;
142 : : //@}
143 : :
144 : : // Cached codecvt facet.
145 : : const __codecvt_type* _M_codecvt;
146 : :
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 : : /**
155 : : * Size of buffer held by _M_ext_buf.
156 : : */
157 : : streamsize _M_ext_buf_size;
158 : :
159 : : /**
160 : : * Pointers into the buffer held by _M_ext_buf that delimit a
161 : : * subsequence of bytes that have been read but not yet converted.
162 : : * When valid, _M_ext_next corresponds to egptr().
163 : : */
164 : : const char* _M_ext_next;
165 : : char* _M_ext_end;
166 : :
167 : : /**
168 : : * Initializes pback buffers, and moves normal buffers to safety.
169 : : * Assumptions:
170 : : * _M_in_cur has already been moved back
171 : : */
172 : : void
173 : : _M_create_pback()
174 : : {
175 : : if (!_M_pback_init)
176 : : {
177 : : _M_pback_cur_save = this->gptr();
178 : : _M_pback_end_save = this->egptr();
179 : : this->setg(&_M_pback, &_M_pback, &_M_pback + 1);
180 : : _M_pback_init = true;
181 : : }
182 : : }
183 : :
184 : : /**
185 : : * Deactivates pback buffer contents, and restores normal buffer.
186 : : * Assumptions:
187 : : * The pback buffer has only moved forward.
188 : : */
189 : : void
190 : : _M_destroy_pback() throw()
191 : : {
192 : : if (_M_pback_init)
193 : : {
194 : : // Length _M_in_cur moved in the pback buffer.
195 : : _M_pback_cur_save += this->gptr() != this->eback();
196 : : this->setg(_M_buf, _M_pback_cur_save, _M_pback_end_save);
197 : : _M_pback_init = false;
198 : : }
199 : : }
200 : :
201 : : public:
202 : : // Constructors/destructor:
203 : : /**
204 : : * @brief Does not open any files.
205 : : *
206 : : * The default constructor initializes the parent class using its
207 : : * own default ctor.
208 : : */
209 : : basic_filebuf();
210 : :
211 : : /**
212 : : * @brief The destructor closes the file first.
213 : : */
214 : : virtual
215 : 60 : ~basic_filebuf()
216 [ - + ]: 60 : { this->close(); }
217 : :
218 : : // Members:
219 : : /**
220 : : * @brief Returns true if the external file is open.
221 : : */
222 : : bool
223 : 0 : is_open() const throw()
224 : 4964 : { return _M_file.is_open(); }
225 : :
226 : : /**
227 : : * @brief Opens an external file.
228 : : * @param s The name of the file.
229 : : * @param mode The open mode flags.
230 : : * @return @c this on success, NULL on failure
231 : : *
232 : : * If a file is already open, this function immediately fails.
233 : : * Otherwise it tries to open the file named @a s using the flags
234 : : * given in @a mode.
235 : : *
236 : : * Table 92, adapted here, gives the relation between openmode
237 : : * combinations and the equivalent fopen() flags.
238 : : * (NB: lines app, in|out|app, in|app, binary|app, binary|in|out|app,
239 : : * and binary|in|app per DR 596)
240 : : * +---------------------------------------------------------+
241 : : * | ios_base Flag combination stdio equivalent |
242 : : * |binary in out trunc app |
243 : : * +---------------------------------------------------------+
244 : : * | + "w" |
245 : : * | + + "a" |
246 : : * | + "a" |
247 : : * | + + "w" |
248 : : * | + "r" |
249 : : * | + + "r+" |
250 : : * | + + + "w+" |
251 : : * | + + + "a+" |
252 : : * | + + "a+" |
253 : : * +---------------------------------------------------------+
254 : : * | + + "wb" |
255 : : * | + + + "ab" |
256 : : * | + + "ab" |
257 : : * | + + + "wb" |
258 : : * | + + "rb" |
259 : : * | + + + "r+b" |
260 : : * | + + + + "w+b" |
261 : : * | + + + + "a+b" |
262 : : * | + + + "a+b" |
263 : : * +---------------------------------------------------------+
264 : : */
265 : : __filebuf_type*
266 : : open(const char* __s, ios_base::openmode __mode);
267 : :
268 : : /**
269 : : * @brief Closes the currently associated file.
270 : : * @return @c this on success, NULL on failure
271 : : *
272 : : * If no file is currently open, this function immediately fails.
273 : : *
274 : : * If a "put buffer area" exists, @c overflow(eof) is called to flush
275 : : * all the characters. The file is then closed.
276 : : *
277 : : * If any operations fail, this function also fails.
278 : : */
279 : : __filebuf_type*
280 : : close();
281 : :
282 : : protected:
283 : : void
284 : : _M_allocate_internal_buffer();
285 : :
286 : : void
287 : : _M_destroy_internal_buffer() throw();
288 : :
289 : : // [27.8.1.4] overridden virtual functions
290 : : virtual streamsize
291 : : showmanyc();
292 : :
293 : : // Stroustrup, 1998, p. 628
294 : : // underflow() and uflow() functions are called to get the next
295 : : // character from the real input source when the buffer is empty.
296 : : // Buffered input uses underflow()
297 : :
298 : : virtual int_type
299 : : underflow();
300 : :
301 : : virtual int_type
302 : : pbackfail(int_type __c = _Traits::eof());
303 : :
304 : : // Stroustrup, 1998, p 648
305 : : // The overflow() function is called to transfer characters to the
306 : : // real output destination when the buffer is full. A call to
307 : : // overflow(c) outputs the contents of the buffer plus the
308 : : // character c.
309 : : // 27.5.2.4.5
310 : : // Consume some sequence of the characters in the pending sequence.
311 : : virtual int_type
312 : : overflow(int_type __c = _Traits::eof());
313 : :
314 : : // Convert internal byte sequence to external, char-based
315 : : // sequence via codecvt.
316 : : bool
317 : : _M_convert_to_external(char_type*, streamsize);
318 : :
319 : : /**
320 : : * @brief Manipulates the buffer.
321 : : * @param s Pointer to a buffer area.
322 : : * @param n Size of @a s.
323 : : * @return @c this
324 : : *
325 : : * If no file has been opened, and both @a s and @a n are zero, then
326 : : * the stream becomes unbuffered. Otherwise, @c s is used as a
327 : : * buffer; see
328 : : * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
329 : : * for more.
330 : : */
331 : : virtual __streambuf_type*
332 : : setbuf(char_type* __s, streamsize __n);
333 : :
334 : : virtual pos_type
335 : : seekoff(off_type __off, ios_base::seekdir __way,
336 : : ios_base::openmode __mode = ios_base::in | ios_base::out);
337 : :
338 : : virtual pos_type
339 : : seekpos(pos_type __pos,
340 : : ios_base::openmode __mode = ios_base::in | ios_base::out);
341 : :
342 : : // Common code for seekoff and seekpos
343 : : pos_type
344 : : _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state);
345 : :
346 : : virtual int
347 : : sync();
348 : :
349 : : virtual void
350 : : imbue(const locale& __loc);
351 : :
352 : : virtual streamsize
353 : : xsgetn(char_type* __s, streamsize __n);
354 : :
355 : : virtual streamsize
356 : : xsputn(const char_type* __s, streamsize __n);
357 : :
358 : : // Flushes output buffer, then writes unshift sequence.
359 : : bool
360 : : _M_terminate_output();
361 : :
362 : : /**
363 : : * This function sets the pointers of the internal buffer, both get
364 : : * and put areas. Typically:
365 : : *
366 : : * __off == egptr() - eback() upon underflow/uflow ('read' mode);
367 : : * __off == 0 upon overflow ('write' mode);
368 : : * __off == -1 upon open, setbuf, seekoff/pos ('uncommitted' mode).
369 : : *
370 : : * NB: epptr() - pbase() == _M_buf_size - 1, since _M_buf_size
371 : : * reflects the actual allocated memory and the last cell is reserved
372 : : * for the overflow char of a full put area.
373 : : */
374 : : void
375 : 0 : _M_set_buffer(streamsize __off)
376 : : {
377 : 0 : const bool __testin = _M_mode & ios_base::in;
378 : 0 : const bool __testout = _M_mode & ios_base::out;
379 : :
380 [ # # ][ # # ]: 0 : if (__testin && __off > 0)
381 : 0 : this->setg(_M_buf, _M_buf, _M_buf + __off);
382 : : else
383 : 0 : this->setg(_M_buf, _M_buf, _M_buf);
384 : :
385 [ # # ][ # # ]: 0 : if (__testout && __off == 0 && _M_buf_size > 1 )
[ # # ]
386 : 0 : this->setp(_M_buf, _M_buf + _M_buf_size - 1);
387 : : else
388 : 0 : this->setp(NULL, NULL);
389 : 0 : }
390 : : };
391 : :
392 : : // [27.8.1.5] Template class basic_ifstream
393 : : /**
394 : : * @brief Controlling input for files.
395 : : *
396 : : * This class supports reading from named files, using the inherited
397 : : * functions from std::basic_istream. To control the associated
398 : : * sequence, an instance of std::basic_filebuf is used, which this page
399 : : * refers to as @c sb.
400 : : */
401 : : template<typename _CharT, typename _Traits>
402 : : class basic_ifstream : public basic_istream<_CharT, _Traits>
403 : : {
404 : : public:
405 : : // Types:
406 : : typedef _CharT char_type;
407 : : typedef _Traits traits_type;
408 : : typedef typename traits_type::int_type int_type;
409 : : typedef typename traits_type::pos_type pos_type;
410 : : typedef typename traits_type::off_type off_type;
411 : :
412 : : // Non-standard types:
413 : : typedef basic_filebuf<char_type, traits_type> __filebuf_type;
414 : : typedef basic_istream<char_type, traits_type> __istream_type;
415 : :
416 : : private:
417 : : __filebuf_type _M_filebuf;
418 : :
419 : : public:
420 : : // Constructors/Destructors:
421 : : /**
422 : : * @brief Default constructor.
423 : : *
424 : : * Initializes @c sb using its default constructor, and passes
425 : : * @c &sb to the base class initializer. Does not open any files
426 : : * (you haven't given it a filename to open).
427 : : */
428 : 35 : basic_ifstream() : __istream_type(), _M_filebuf()
429 : 35 : { this->init(&_M_filebuf); }
430 : :
431 : : /**
432 : : * @brief Create an input file stream.
433 : : * @param s Null terminated string specifying the filename.
434 : : * @param mode Open file in specified mode (see std::ios_base).
435 : : *
436 : : * @c ios_base::in is automatically included in @a mode.
437 : : *
438 : : * Tip: When using std::string to hold the filename, you must use
439 : : * .c_str() before passing it to this constructor.
440 : : */
441 : : explicit
442 : 0 : basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
443 : 0 : : __istream_type(), _M_filebuf()
444 : : {
445 : 0 : this->init(&_M_filebuf);
446 : 0 : this->open(__s, __mode);
447 : 0 : }
448 : :
449 : : /**
450 : : * @brief The destructor does nothing.
451 : : *
452 : : * The file is closed by the filebuf object, not the formatting
453 : : * stream.
454 : : */
455 : 59 : ~basic_ifstream()
456 [ # # ][ + - ]: 59 : { }
[ - + ]
457 : :
458 : : // Members:
459 : : /**
460 : : * @brief Accessing the underlying buffer.
461 : : * @return The current basic_filebuf buffer.
462 : : *
463 : : * This hides both signatures of std::basic_ios::rdbuf().
464 : : */
465 : : __filebuf_type*
466 : : rdbuf() const
467 : : { return const_cast<__filebuf_type*>(&_M_filebuf); }
468 : :
469 : : /**
470 : : * @brief Wrapper to test for an open file.
471 : : * @return @c rdbuf()->is_open()
472 : : */
473 : : bool
474 : : is_open()
475 : 59 : { return _M_filebuf.is_open(); }
476 : :
477 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
478 : : // 365. Lack of const-qualification in clause 27
479 : : bool
480 : : is_open() const
481 : : { return _M_filebuf.is_open(); }
482 : :
483 : : /**
484 : : * @brief Opens an external file.
485 : : * @param s The name of the file.
486 : : * @param mode The open mode flags.
487 : : *
488 : : * Calls @c std::basic_filebuf::open(s,mode|in). If that function
489 : : * fails, @c failbit is set in the stream's error state.
490 : : *
491 : : * Tip: When using std::string to hold the filename, you must use
492 : : * .c_str() before passing it to this constructor.
493 : : */
494 : : void
495 : 55 : open(const char* __s, ios_base::openmode __mode = ios_base::in)
496 : : {
497 [ - + ]: 55 : if (!_M_filebuf.open(__s, __mode | ios_base::in))
498 : 0 : this->setstate(ios_base::failbit);
499 : : else
500 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
501 : : // 409. Closing an fstream should clear error state
502 : 55 : this->clear();
503 : 55 : }
504 : :
505 : : /**
506 : : * @brief Close the file.
507 : : *
508 : : * Calls @c std::basic_filebuf::close(). If that function
509 : : * fails, @c failbit is set in the stream's error state.
510 : : */
511 : : void
512 : 27 : close()
513 : : {
514 [ - + ]: 27 : if (!_M_filebuf.close())
515 : 0 : this->setstate(ios_base::failbit);
516 : 27 : }
517 : : };
518 : :
519 : :
520 : : // [27.8.1.8] Template class basic_ofstream
521 : : /**
522 : : * @brief Controlling output for files.
523 : : *
524 : : * This class supports reading from named files, using the inherited
525 : : * functions from std::basic_ostream. To control the associated
526 : : * sequence, an instance of std::basic_filebuf is used, which this page
527 : : * refers to as @c sb.
528 : : */
529 : : template<typename _CharT, typename _Traits>
530 : : class basic_ofstream : public basic_ostream<_CharT,_Traits>
531 : : {
532 : : public:
533 : : // Types:
534 : : typedef _CharT char_type;
535 : : typedef _Traits traits_type;
536 : : typedef typename traits_type::int_type int_type;
537 : : typedef typename traits_type::pos_type pos_type;
538 : : typedef typename traits_type::off_type off_type;
539 : :
540 : : // Non-standard types:
541 : : typedef basic_filebuf<char_type, traits_type> __filebuf_type;
542 : : typedef basic_ostream<char_type, traits_type> __ostream_type;
543 : :
544 : : private:
545 : : __filebuf_type _M_filebuf;
546 : :
547 : : public:
548 : : // Constructors:
549 : : /**
550 : : * @brief Default constructor.
551 : : *
552 : : * Initializes @c sb using its default constructor, and passes
553 : : * @c &sb to the base class initializer. Does not open any files
554 : : * (you haven't given it a filename to open).
555 : : */
556 : 1 : basic_ofstream(): __ostream_type(), _M_filebuf()
557 : 1 : { this->init(&_M_filebuf); }
558 : :
559 : : /**
560 : : * @brief Create an output file stream.
561 : : * @param s Null terminated string specifying the filename.
562 : : * @param mode Open file in specified mode (see std::ios_base).
563 : : *
564 : : * @c ios_base::out|ios_base::trunc is automatically included in
565 : : * @a mode.
566 : : *
567 : : * Tip: When using std::string to hold the filename, you must use
568 : : * .c_str() before passing it to this constructor.
569 : : */
570 : : explicit
571 : : basic_ofstream(const char* __s,
572 : 0 : ios_base::openmode __mode = ios_base::out|ios_base::trunc)
573 : 0 : : __ostream_type(), _M_filebuf()
574 : : {
575 : 0 : this->init(&_M_filebuf);
576 : 0 : this->open(__s, __mode);
577 : 0 : }
578 : :
579 : : /**
580 : : * @brief The destructor does nothing.
581 : : *
582 : : * The file is closed by the filebuf object, not the formatting
583 : : * stream.
584 : : */
585 : 1 : ~basic_ofstream()
586 [ # # ][ + - ]: 1 : { }
[ - + ]
587 : :
588 : : // Members:
589 : : /**
590 : : * @brief Accessing the underlying buffer.
591 : : * @return The current basic_filebuf buffer.
592 : : *
593 : : * This hides both signatures of std::basic_ios::rdbuf().
594 : : */
595 : : __filebuf_type*
596 : : rdbuf() const
597 : 4909 : { return const_cast<__filebuf_type*>(&_M_filebuf); }
598 : :
599 : : /**
600 : : * @brief Wrapper to test for an open file.
601 : : * @return @c rdbuf()->is_open()
602 : : */
603 : : bool
604 : : is_open()
605 : 4909 : { return _M_filebuf.is_open(); }
606 : :
607 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
608 : : // 365. Lack of const-qualification in clause 27
609 : : bool
610 : : is_open() const
611 : : { return _M_filebuf.is_open(); }
612 : :
613 : : /**
614 : : * @brief Opens an external file.
615 : : * @param s The name of the file.
616 : : * @param mode The open mode flags.
617 : : *
618 : : * Calls @c std::basic_filebuf::open(s,mode|out|trunc). If that
619 : : * function fails, @c failbit is set in the stream's error state.
620 : : *
621 : : * Tip: When using std::string to hold the filename, you must use
622 : : * .c_str() before passing it to this constructor.
623 : : */
624 : : void
625 : : open(const char* __s,
626 : 0 : ios_base::openmode __mode = ios_base::out | ios_base::trunc)
627 : : {
628 [ # # ]: 0 : if (!_M_filebuf.open(__s, __mode | ios_base::out))
629 : 0 : this->setstate(ios_base::failbit);
630 : : else
631 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
632 : : // 409. Closing an fstream should clear error state
633 : 0 : this->clear();
634 : 0 : }
635 : :
636 : : /**
637 : : * @brief Close the file.
638 : : *
639 : : * Calls @c std::basic_filebuf::close(). If that function
640 : : * fails, @c failbit is set in the stream's error state.
641 : : */
642 : : void
643 : 0 : close()
644 : : {
645 [ # # ]: 0 : if (!_M_filebuf.close())
646 : 0 : this->setstate(ios_base::failbit);
647 : 0 : }
648 : : };
649 : :
650 : :
651 : : // [27.8.1.11] Template class basic_fstream
652 : : /**
653 : : * @brief Controlling input and output for files.
654 : : *
655 : : * This class supports reading from and writing to named files, using
656 : : * the inherited functions from std::basic_iostream. To control the
657 : : * associated sequence, an instance of std::basic_filebuf is used, which
658 : : * this page refers to as @c sb.
659 : : */
660 : : template<typename _CharT, typename _Traits>
661 : : class basic_fstream : public basic_iostream<_CharT, _Traits>
662 : : {
663 : : public:
664 : : // Types:
665 : : typedef _CharT char_type;
666 : : typedef _Traits traits_type;
667 : : typedef typename traits_type::int_type int_type;
668 : : typedef typename traits_type::pos_type pos_type;
669 : : typedef typename traits_type::off_type off_type;
670 : :
671 : : // Non-standard types:
672 : : typedef basic_filebuf<char_type, traits_type> __filebuf_type;
673 : : typedef basic_ios<char_type, traits_type> __ios_type;
674 : : typedef basic_iostream<char_type, traits_type> __iostream_type;
675 : :
676 : : private:
677 : : __filebuf_type _M_filebuf;
678 : :
679 : : public:
680 : : // Constructors/destructor:
681 : : /**
682 : : * @brief Default constructor.
683 : : *
684 : : * Initializes @c sb using its default constructor, and passes
685 : : * @c &sb to the base class initializer. Does not open any files
686 : : * (you haven't given it a filename to open).
687 : : */
688 : : basic_fstream()
689 : : : __iostream_type(), _M_filebuf()
690 : : { this->init(&_M_filebuf); }
691 : :
692 : : /**
693 : : * @brief Create an input/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 : : * Tip: When using std::string to hold the filename, you must use
698 : : * .c_str() before passing it to this constructor.
699 : : */
700 : : explicit
701 : : basic_fstream(const char* __s,
702 : : ios_base::openmode __mode = ios_base::in | ios_base::out)
703 : : : __iostream_type(NULL), _M_filebuf()
704 : : {
705 : : this->init(&_M_filebuf);
706 : : this->open(__s, __mode);
707 : : }
708 : :
709 : : /**
710 : : * @brief The destructor does nothing.
711 : : *
712 : : * The file is closed by the filebuf object, not the formatting
713 : : * stream.
714 : : */
715 : : ~basic_fstream()
716 : : { }
717 : :
718 : : // Members:
719 : : /**
720 : : * @brief Accessing the underlying buffer.
721 : : * @return The current basic_filebuf buffer.
722 : : *
723 : : * This hides both signatures of std::basic_ios::rdbuf().
724 : : */
725 : : __filebuf_type*
726 : : rdbuf() const
727 : : { return const_cast<__filebuf_type*>(&_M_filebuf); }
728 : :
729 : : /**
730 : : * @brief Wrapper to test for an open file.
731 : : * @return @c rdbuf()->is_open()
732 : : */
733 : : bool
734 : : is_open()
735 : : { return _M_filebuf.is_open(); }
736 : :
737 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
738 : : // 365. Lack of const-qualification in clause 27
739 : : bool
740 : : is_open() const
741 : : { return _M_filebuf.is_open(); }
742 : :
743 : : /**
744 : : * @brief Opens an external file.
745 : : * @param s The name of the file.
746 : : * @param mode The open mode flags.
747 : : *
748 : : * Calls @c std::basic_filebuf::open(s,mode). If that
749 : : * function fails, @c failbit is set in the stream's error state.
750 : : *
751 : : * Tip: When using std::string to hold the filename, you must use
752 : : * .c_str() before passing it to this constructor.
753 : : */
754 : : void
755 : : open(const char* __s,
756 : : ios_base::openmode __mode = ios_base::in | ios_base::out)
757 : : {
758 : : if (!_M_filebuf.open(__s, __mode))
759 : : this->setstate(ios_base::failbit);
760 : : else
761 : : // _GLIBCXX_RESOLVE_LIB_DEFECTS
762 : : // 409. Closing an fstream should clear error state
763 : : this->clear();
764 : : }
765 : :
766 : : /**
767 : : * @brief Close the file.
768 : : *
769 : : * Calls @c std::basic_filebuf::close(). If that function
770 : : * fails, @c failbit is set in the stream's error state.
771 : : */
772 : : void
773 : : close()
774 : : {
775 : : if (!_M_filebuf.close())
776 : : this->setstate(ios_base::failbit);
777 : : }
778 : : };
779 : :
780 : : _GLIBCXX_END_NAMESPACE
781 : :
782 : : #ifndef _GLIBCXX_EXPORT_TEMPLATE
783 : : # include <bits/fstream.tcc>
784 : : #endif
785 : :
786 : : #endif /* _GLIBCXX_FSTREAM */
|