1  // File descriptor layer for filebuf -*- C++ -*-
       2  
       3  // Copyright (C) 2002-2023 Free Software Foundation, Inc.
       4  //
       5  // This file is part of the GNU ISO C++ Library.  This library is free
       6  // software; you can redistribute it and/or modify it under the
       7  // terms of the GNU General Public License as published by the
       8  // Free Software Foundation; either version 3, or (at your option)
       9  // any later version.
      10  
      11  // This library is distributed in the hope that it will be useful,
      12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14  // GNU General Public License for more details.
      15  
      16  // Under Section 7 of GPL version 3, you are granted additional
      17  // permissions described in the GCC Runtime Library Exception, version
      18  // 3.1, as published by the Free Software Foundation.
      19  
      20  // You should have received a copy of the GNU General Public License and
      21  // a copy of the GCC Runtime Library Exception along with this program;
      22  // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      23  // <http://www.gnu.org/licenses/>.
      24  
      25  /** @file ext/stdio_filebuf.h
      26   *  This file is a GNU extension to the Standard C++ Library.
      27   */
      28  
      29  #ifndef _STDIO_FILEBUF_H
      30  #define _STDIO_FILEBUF_H 1
      31  
      32  #pragma GCC system_header
      33  
      34  #include <bits/requires_hosted.h> // GNU extensions are currently omitted
      35  
      36  #include <fstream>
      37  
      38  namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
      39  {
      40  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      41  
      42    /**
      43     *  @brief Provides a layer of compatibility for C/POSIX.
      44     *  @ingroup io
      45     *
      46     *  This GNU extension provides extensions for working with standard C
      47     *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
      48     *  user with the type of character used in the file stream, e.g.,
      49     *  stdio_filebuf<char>.
      50    */
      51    template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
      52      class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
      53      {
      54      public:
      55        // Types:
      56        typedef _CharT				        char_type;
      57        typedef _Traits				        traits_type;
      58        typedef typename traits_type::int_type		int_type;
      59        typedef typename traits_type::pos_type		pos_type;
      60        typedef typename traits_type::off_type		off_type;
      61        typedef std::size_t                               size_t;
      62  
      63      public:
      64        /**
      65         * deferred initialization
      66        */
      67        stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
      68  
      69        /**
      70         *  @param  __fd  An open file descriptor.
      71         *  @param  __mode  Same meaning as in a standard filebuf.
      72         *  @param  __size Optimal or preferred size of internal buffer,
      73         *                 in chars.
      74         *
      75         *  This constructor associates a file stream buffer with an open
      76         *  POSIX file descriptor. The file descriptor will be automatically
      77         *  closed when the stdio_filebuf is closed/destroyed.
      78        */
      79        stdio_filebuf(int __fd, std::ios_base::openmode __mode,
      80  		    size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
      81  
      82        /**
      83         *  @param  __f  An open @c FILE*.
      84         *  @param  __mode  Same meaning as in a standard filebuf.
      85         *  @param  __size Optimal or preferred size of internal buffer,
      86         *                 in chars.  Defaults to system's @c BUFSIZ.
      87         *
      88         *  This constructor associates a file stream buffer with an open
      89         *  C @c FILE*.  The @c FILE* will not be automatically closed when the
      90         *  stdio_filebuf is closed/destroyed.
      91        */
      92        stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
      93  		    size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ));
      94  
      95        /**
      96         *  Closes the external data stream if the file descriptor constructor
      97         *  was used.
      98        */
      99        virtual
     100        ~stdio_filebuf();
     101  
     102  #if __cplusplus >= 201103L
     103        stdio_filebuf(stdio_filebuf&&) = default;
     104        stdio_filebuf& operator=(stdio_filebuf&&) = default;
     105  
     106        void
     107        swap(stdio_filebuf& __fb)
     108        { std::basic_filebuf<_CharT, _Traits>::swap(__fb); }
     109  #endif
     110  
     111        /**
     112         *  @return  The underlying file descriptor.
     113         *
     114         *  Once associated with an external data stream, this function can be
     115         *  used to access the underlying POSIX file descriptor.  Note that
     116         *  there is no way for the library to track what you do with the
     117         *  descriptor, so be careful.
     118        */
     119        int
     120        fd() { return this->_M_file.fd(); }
     121  
     122        /**
     123         *  @return  The underlying FILE*.
     124         *
     125         *  This function can be used to access the underlying "C" file pointer.
     126         *  Note that there is no way for the library to track what you do
     127         *  with the file, so be careful.
     128         */
     129        std::__c_file*
     130        file() { return this->_M_file.file(); }
     131      };
     132  
     133    template<typename _CharT, typename _Traits>
     134      stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
     135      { }
     136  
     137    template<typename _CharT, typename _Traits>
     138      stdio_filebuf<_CharT, _Traits>::
     139      stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
     140      {
     141        this->_M_file.sys_open(__fd, __mode);
     142        if (this->is_open())
     143  	{
     144  	  this->_M_mode = __mode;
     145  	  this->_M_buf_size = __size;
     146  	  this->_M_allocate_internal_buffer();
     147  	  this->_M_reading = false;
     148  	  this->_M_writing = false;
     149  	  this->_M_set_buffer(-1);
     150  	}
     151      }
     152  
     153    template<typename _CharT, typename _Traits>
     154      stdio_filebuf<_CharT, _Traits>::
     155      stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
     156  		  size_t __size)
     157      {
     158        this->_M_file.sys_open(__f, __mode);
     159        if (this->is_open())
     160  	{
     161  	  this->_M_mode = __mode;
     162  	  this->_M_buf_size = __size;
     163  	  this->_M_allocate_internal_buffer();
     164  	  this->_M_reading = false;
     165  	  this->_M_writing = false;
     166  	  this->_M_set_buffer(-1);
     167  	}
     168      }
     169  
     170  _GLIBCXX_END_NAMESPACE_VERSION
     171  } // namespace
     172  
     173  #endif