xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/stdio_filebuf.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg // File descriptor layer for filebuf -*- C++ -*-
236ac495dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2002-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg 
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg 
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg 
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg 
2536ac495dSmrg /** @file ext/stdio_filebuf.h
2636ac495dSmrg  *  This file is a GNU extension to the Standard C++ Library.
2736ac495dSmrg  */
2836ac495dSmrg 
2936ac495dSmrg #ifndef _STDIO_FILEBUF_H
3036ac495dSmrg #define _STDIO_FILEBUF_H 1
3136ac495dSmrg 
3236ac495dSmrg #pragma GCC system_header
3336ac495dSmrg 
3436ac495dSmrg #include <fstream>
3536ac495dSmrg 
_GLIBCXX_VISIBILITY(default)3636ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
3736ac495dSmrg {
3836ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
3936ac495dSmrg 
4036ac495dSmrg   /**
4136ac495dSmrg    *  @brief Provides a layer of compatibility for C/POSIX.
4236ac495dSmrg    *  @ingroup io
4336ac495dSmrg    *
4436ac495dSmrg    *  This GNU extension provides extensions for working with standard C
4536ac495dSmrg    *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
4636ac495dSmrg    *  user with the type of character used in the file stream, e.g.,
4736ac495dSmrg    *  stdio_filebuf<char>.
4836ac495dSmrg   */
4936ac495dSmrg   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
5036ac495dSmrg     class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
5136ac495dSmrg     {
5236ac495dSmrg     public:
5336ac495dSmrg       // Types:
5436ac495dSmrg       typedef _CharT				        char_type;
5536ac495dSmrg       typedef _Traits				        traits_type;
5636ac495dSmrg       typedef typename traits_type::int_type		int_type;
5736ac495dSmrg       typedef typename traits_type::pos_type		pos_type;
5836ac495dSmrg       typedef typename traits_type::off_type		off_type;
5936ac495dSmrg       typedef std::size_t                               size_t;
6036ac495dSmrg 
6136ac495dSmrg     public:
6236ac495dSmrg       /**
6336ac495dSmrg        * deferred initialization
6436ac495dSmrg       */
6536ac495dSmrg       stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
6636ac495dSmrg 
6736ac495dSmrg       /**
6836ac495dSmrg        *  @param  __fd  An open file descriptor.
6936ac495dSmrg        *  @param  __mode  Same meaning as in a standard filebuf.
7036ac495dSmrg        *  @param  __size Optimal or preferred size of internal buffer,
7136ac495dSmrg        *                 in chars.
7236ac495dSmrg        *
7336ac495dSmrg        *  This constructor associates a file stream buffer with an open
7436ac495dSmrg        *  POSIX file descriptor. The file descriptor will be automatically
7536ac495dSmrg        *  closed when the stdio_filebuf is closed/destroyed.
7636ac495dSmrg       */
7736ac495dSmrg       stdio_filebuf(int __fd, std::ios_base::openmode __mode,
7836ac495dSmrg 		    size_t __size = static_cast<size_t>(BUFSIZ));
7936ac495dSmrg 
8036ac495dSmrg       /**
8136ac495dSmrg        *  @param  __f  An open @c FILE*.
8236ac495dSmrg        *  @param  __mode  Same meaning as in a standard filebuf.
8336ac495dSmrg        *  @param  __size Optimal or preferred size of internal buffer,
8436ac495dSmrg        *                 in chars.  Defaults to system's @c BUFSIZ.
8536ac495dSmrg        *
8636ac495dSmrg        *  This constructor associates a file stream buffer with an open
8736ac495dSmrg        *  C @c FILE*.  The @c FILE* will not be automatically closed when the
8836ac495dSmrg        *  stdio_filebuf is closed/destroyed.
8936ac495dSmrg       */
9036ac495dSmrg       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
9136ac495dSmrg 		    size_t __size = static_cast<size_t>(BUFSIZ));
9236ac495dSmrg 
9336ac495dSmrg       /**
9436ac495dSmrg        *  Closes the external data stream if the file descriptor constructor
9536ac495dSmrg        *  was used.
9636ac495dSmrg       */
9736ac495dSmrg       virtual
9836ac495dSmrg       ~stdio_filebuf();
9936ac495dSmrg 
10036ac495dSmrg #if __cplusplus >= 201103L
10136ac495dSmrg       stdio_filebuf(stdio_filebuf&&) = default;
10236ac495dSmrg       stdio_filebuf& operator=(stdio_filebuf&&) = default;
10336ac495dSmrg 
10436ac495dSmrg       void
10536ac495dSmrg       swap(stdio_filebuf& __fb)
10636ac495dSmrg       { std::basic_filebuf<_CharT, _Traits>::swap(__fb); }
10736ac495dSmrg #endif
10836ac495dSmrg 
10936ac495dSmrg       /**
11036ac495dSmrg        *  @return  The underlying file descriptor.
11136ac495dSmrg        *
11236ac495dSmrg        *  Once associated with an external data stream, this function can be
11336ac495dSmrg        *  used to access the underlying POSIX file descriptor.  Note that
11436ac495dSmrg        *  there is no way for the library to track what you do with the
11536ac495dSmrg        *  descriptor, so be careful.
11636ac495dSmrg       */
11736ac495dSmrg       int
11836ac495dSmrg       fd() { return this->_M_file.fd(); }
11936ac495dSmrg 
12036ac495dSmrg       /**
12136ac495dSmrg        *  @return  The underlying FILE*.
12236ac495dSmrg        *
12336ac495dSmrg        *  This function can be used to access the underlying "C" file pointer.
12436ac495dSmrg        *  Note that there is no way for the library to track what you do
12536ac495dSmrg        *  with the file, so be careful.
12636ac495dSmrg        */
12736ac495dSmrg       std::__c_file*
12836ac495dSmrg       file() { return this->_M_file.file(); }
12936ac495dSmrg     };
13036ac495dSmrg 
13136ac495dSmrg   template<typename _CharT, typename _Traits>
13236ac495dSmrg     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
13336ac495dSmrg     { }
13436ac495dSmrg 
13536ac495dSmrg   template<typename _CharT, typename _Traits>
13636ac495dSmrg     stdio_filebuf<_CharT, _Traits>::
13736ac495dSmrg     stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
13836ac495dSmrg     {
13936ac495dSmrg       this->_M_file.sys_open(__fd, __mode);
14036ac495dSmrg       if (this->is_open())
14136ac495dSmrg 	{
14236ac495dSmrg 	  this->_M_mode = __mode;
14336ac495dSmrg 	  this->_M_buf_size = __size;
14436ac495dSmrg 	  this->_M_allocate_internal_buffer();
14536ac495dSmrg 	  this->_M_reading = false;
14636ac495dSmrg 	  this->_M_writing = false;
14736ac495dSmrg 	  this->_M_set_buffer(-1);
14836ac495dSmrg 	}
14936ac495dSmrg     }
15036ac495dSmrg 
15136ac495dSmrg   template<typename _CharT, typename _Traits>
15236ac495dSmrg     stdio_filebuf<_CharT, _Traits>::
15336ac495dSmrg     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
15436ac495dSmrg 		  size_t __size)
15536ac495dSmrg     {
15636ac495dSmrg       this->_M_file.sys_open(__f, __mode);
15736ac495dSmrg       if (this->is_open())
15836ac495dSmrg 	{
15936ac495dSmrg 	  this->_M_mode = __mode;
16036ac495dSmrg 	  this->_M_buf_size = __size;
16136ac495dSmrg 	  this->_M_allocate_internal_buffer();
16236ac495dSmrg 	  this->_M_reading = false;
16336ac495dSmrg 	  this->_M_writing = false;
16436ac495dSmrg 	  this->_M_set_buffer(-1);
16536ac495dSmrg 	}
16636ac495dSmrg     }
16736ac495dSmrg 
16836ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
16936ac495dSmrg } // namespace
17036ac495dSmrg 
17136ac495dSmrg #endif
172