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