LCOV - code coverage report
Current view: directory - usr/include/c++/4.2.1 - fstream (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 27 0 0.0 %
Date: 2012-02-16 Functions: 0 0 -

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

Generated by: LCOV version 1.7