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