1 // File descriptor layer for filebuf -*- C++ -*-
3 // Copyright (C) 2002, 2003 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 * @class stdio_filebuf ext/stdio_filebuf.h <ext/stdio_filebuf.h>
45 * @brief Provides a layer of compatibility for C/POSIX.
47 * This GNU extension provides extensions for working with standard C
48 * FILE*'s and POSIX file descriptors. It must be instantiated by the
49 * user with the type of character used in the file stream, e.g.,
50 * stdio_filebuf<char>.
52 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
> >
53 class stdio_filebuf
: public std::basic_filebuf
<_CharT
, _Traits
>
57 typedef _CharT char_type
;
58 typedef _Traits traits_type
;
59 typedef typename
traits_type::int_type int_type
;
60 typedef typename
traits_type::pos_type pos_type
;
61 typedef typename
traits_type::off_type off_type
;
62 typedef std::size_t size_t;
66 * @param fd An open file descriptor.
67 * @param mode Same meaning as in a standard filebuf.
68 * @param size Optimal or preferred size of internal buffer, in chars.
70 * This constructor associates a file stream buffer with an open
71 * POSIX file descriptor.
73 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
,
74 size_t __size
= static_cast<size_t>(BUFSIZ
));
77 * @param f An open @c FILE*.
78 * @param mode Same meaning as in a standard filebuf.
79 * @param size Optimal or preferred size of internal buffer, in chars.
80 * Defaults to system's @c BUFSIZ.
82 * This constructor associates a file stream buffer with an open
83 * C @c FILE*. The @c FILE* will not be automatically closed when the
84 * stdio_filebuf is closed/destroyed.
86 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
87 size_t __size
= static_cast<size_t>(BUFSIZ
));
90 * Possibly closes the external data stream, in the case of the file
91 * descriptor constructor and @c del @c == @c true.
97 * @return The underlying file descriptor.
99 * Once associated with an external data stream, this function can be
100 * used to access the underlying POSIX file descriptor. Note that
101 * there is no way for the library to track what you do with the
102 * descriptor, so be careful.
106 { return this->_M_file
.fd(); }
109 template<typename _CharT
, typename _Traits
>
110 stdio_filebuf
<_CharT
, _Traits
>::~stdio_filebuf()
113 template<typename _CharT
, typename _Traits
>
114 stdio_filebuf
<_CharT
, _Traits
>::
115 stdio_filebuf(int __fd
, std::ios_base::openmode __mode
, size_t __size
)
117 this->_M_file
.sys_open(__fd
, __mode
);
120 this->_M_mode
= __mode
;
121 this->_M_buf_size
= __size
;
122 this->_M_allocate_internal_buffer();
123 this->_M_reading
= false;
124 this->_M_writing
= false;
125 this->_M_set_buffer(-1);
129 template<typename _CharT
, typename _Traits
>
130 stdio_filebuf
<_CharT
, _Traits
>::
131 stdio_filebuf(std::__c_file
* __f
, std::ios_base::openmode __mode
,
134 this->_M_file
.sys_open(__f
, __mode
);
137 this->_M_mode
= __mode
;
138 this->_M_buf_size
= __size
;
139 this->_M_allocate_internal_buffer();
140 this->_M_reading
= false;
141 this->_M_writing
= false;
142 this->_M_set_buffer(-1);
145 } // namespace __gnu_cxx