1 // File descriptor layer for filebuf -*- C++ -*-
3 // Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file ext/stdio_filebuf.h
31 * This file is a GNU extension to the Standard C++ Library.
34 #ifndef _STDIO_FILEBUF_H
35 #define _STDIO_FILEBUF_H 1
37 #pragma GCC system_header
44 * @brief Provides a layer of compatibility for C/POSIX.
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>.
51 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
> >
52 class stdio_filebuf
: public std::basic_filebuf
<_CharT
, _Traits
>
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;
65 * deferred initialization
67 stdio_filebuf() : std::basic_filebuf
<_CharT
, _Traits
>() {}
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, in chars.
74 * This constructor associates a file stream buffer with an open
75 * POSIX file descriptor. The file descriptor will be automatically
76 * closed when the stdio_filebuf is closed/destroyed.
78 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
,
79 size_t __size
= static_cast<size_t>(BUFSIZ
));
82 * @param f An open @c FILE*.
83 * @param mode Same meaning as in a standard filebuf.
84 * @param size Optimal or preferred size of internal buffer, in chars.
85 * Defaults to system's @c BUFSIZ.
87 * This constructor associates a file stream buffer with an open
88 * C @c FILE*. The @c FILE* will not be automatically closed when the
89 * stdio_filebuf is closed/destroyed.
91 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
92 size_t __size
= static_cast<size_t>(BUFSIZ
));
95 * Closes the external data stream if the file descriptor constructor
102 * @return The underlying file descriptor.
104 * Once associated with an external data stream, this function can be
105 * used to access the underlying POSIX file descriptor. Note that
106 * there is no way for the library to track what you do with the
107 * descriptor, so be careful.
110 fd() { return this->_M_file
.fd(); }
113 * @return The underlying FILE*.
115 * This function can be used to access the underlying "C" file pointer.
116 * Note that there is no way for the library to track what you do
117 * with the file, so be careful.
120 file() { return this->_M_file
.file(); }
123 template<typename _CharT
, typename _Traits
>
124 stdio_filebuf
<_CharT
, _Traits
>::~stdio_filebuf()
127 template<typename _CharT
, typename _Traits
>
128 stdio_filebuf
<_CharT
, _Traits
>::
129 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
, size_t __size
)
131 this->_M_file
.sys_open(__fd
, __mode
);
134 this->_M_mode
= __mode
;
135 this->_M_buf_size
= __size
;
136 this->_M_allocate_internal_buffer();
137 this->_M_reading
= false;
138 this->_M_writing
= false;
139 this->_M_set_buffer(-1);
143 template<typename _CharT
, typename _Traits
>
144 stdio_filebuf
<_CharT
, _Traits
>::
145 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
148 this->_M_file
.sys_open(__f
, __mode
);
151 this->_M_mode
= __mode
;
152 this->_M_buf_size
= __size
;
153 this->_M_allocate_internal_buffer();
154 this->_M_reading
= false;
155 this->_M_writing
= false;
156 this->_M_set_buffer(-1);
159 } // namespace __gnu_cxx