1 // File descriptor layer for filebuf -*- C++ -*-
3 // Copyright (C) 2002, 2003, 2004, 2005, 2009, 2010
4 // Free Software Foundation, Inc.
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 3, or (at your option)
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.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/stdio_filebuf.h
27 * This file is a GNU extension to the Standard C++ Library.
30 #ifndef _STDIO_FILEBUF_H
31 #define _STDIO_FILEBUF_H 1
33 #pragma GCC system_header
37 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 * @brief Provides a layer of compatibility for C/POSIX.
45 * This GNU extension provides extensions for working with standard C
46 * FILE*'s and POSIX file descriptors. It must be instantiated by the
47 * user with the type of character used in the file stream, e.g.,
48 * stdio_filebuf<char>.
50 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
> >
51 class stdio_filebuf
: public std::basic_filebuf
<_CharT
, _Traits
>
55 typedef _CharT char_type
;
56 typedef _Traits traits_type
;
57 typedef typename
traits_type::int_type int_type
;
58 typedef typename
traits_type::pos_type pos_type
;
59 typedef typename
traits_type::off_type off_type
;
60 typedef std::size_t size_t;
64 * deferred initialization
66 stdio_filebuf() : std::basic_filebuf
<_CharT
, _Traits
>() {}
69 * @param fd An open file descriptor.
70 * @param mode Same meaning as in a standard filebuf.
71 * @param size Optimal or preferred size of internal buffer, in chars.
73 * This constructor associates a file stream buffer with an open
74 * POSIX file descriptor. The file descriptor will be automatically
75 * closed when the stdio_filebuf is closed/destroyed.
77 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
,
78 size_t __size
= static_cast<size_t>(BUFSIZ
));
81 * @param f An open @c FILE*.
82 * @param mode Same meaning as in a standard filebuf.
83 * @param size Optimal or preferred size of internal buffer, in chars.
84 * Defaults to system's @c BUFSIZ.
86 * This constructor associates a file stream buffer with an open
87 * C @c FILE*. The @c FILE* will not be automatically closed when the
88 * stdio_filebuf is closed/destroyed.
90 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
91 size_t __size
= static_cast<size_t>(BUFSIZ
));
94 * Closes the external data stream if the file descriptor constructor
101 * @return The underlying file descriptor.
103 * Once associated with an external data stream, this function can be
104 * used to access the underlying POSIX file descriptor. Note that
105 * there is no way for the library to track what you do with the
106 * descriptor, so be careful.
109 fd() { return this->_M_file
.fd(); }
112 * @return The underlying FILE*.
114 * This function can be used to access the underlying "C" file pointer.
115 * Note that there is no way for the library to track what you do
116 * with the file, so be careful.
119 file() { return this->_M_file
.file(); }
122 template<typename _CharT
, typename _Traits
>
123 stdio_filebuf
<_CharT
, _Traits
>::~stdio_filebuf()
126 template<typename _CharT
, typename _Traits
>
127 stdio_filebuf
<_CharT
, _Traits
>::
128 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
, size_t __size
)
130 this->_M_file
.sys_open(__fd
, __mode
);
133 this->_M_mode
= __mode
;
134 this->_M_buf_size
= __size
;
135 this->_M_allocate_internal_buffer();
136 this->_M_reading
= false;
137 this->_M_writing
= false;
138 this->_M_set_buffer(-1);
142 template<typename _CharT
, typename _Traits
>
143 stdio_filebuf
<_CharT
, _Traits
>::
144 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
147 this->_M_file
.sys_open(__f
, __mode
);
150 this->_M_mode
= __mode
;
151 this->_M_buf_size
= __size
;
152 this->_M_allocate_internal_buffer();
153 this->_M_reading
= false;
154 this->_M_writing
= false;
155 this->_M_set_buffer(-1);
159 _GLIBCXX_END_NAMESPACE_VERSION