1*404b540aSrobert // File descriptor layer for filebuf -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction. Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License. This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert /** @file ext/stdio_filebuf.h
31*404b540aSrobert * This file is a GNU extension to the Standard C++ Library.
32*404b540aSrobert */
33*404b540aSrobert
34*404b540aSrobert #ifndef _STDIO_FILEBUF_H
35*404b540aSrobert #define _STDIO_FILEBUF_H 1
36*404b540aSrobert
37*404b540aSrobert #pragma GCC system_header
38*404b540aSrobert
39*404b540aSrobert #include <fstream>
40*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)41*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
42*404b540aSrobert
43*404b540aSrobert /**
44*404b540aSrobert * @brief Provides a layer of compatibility for C/POSIX.
45*404b540aSrobert *
46*404b540aSrobert * This GNU extension provides extensions for working with standard C
47*404b540aSrobert * FILE*'s and POSIX file descriptors. It must be instantiated by the
48*404b540aSrobert * user with the type of character used in the file stream, e.g.,
49*404b540aSrobert * stdio_filebuf<char>.
50*404b540aSrobert */
51*404b540aSrobert template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
52*404b540aSrobert class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
53*404b540aSrobert {
54*404b540aSrobert public:
55*404b540aSrobert // Types:
56*404b540aSrobert typedef _CharT char_type;
57*404b540aSrobert typedef _Traits traits_type;
58*404b540aSrobert typedef typename traits_type::int_type int_type;
59*404b540aSrobert typedef typename traits_type::pos_type pos_type;
60*404b540aSrobert typedef typename traits_type::off_type off_type;
61*404b540aSrobert typedef std::size_t size_t;
62*404b540aSrobert
63*404b540aSrobert public:
64*404b540aSrobert /**
65*404b540aSrobert * deferred initialization
66*404b540aSrobert */
67*404b540aSrobert stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
68*404b540aSrobert
69*404b540aSrobert /**
70*404b540aSrobert * @param fd An open file descriptor.
71*404b540aSrobert * @param mode Same meaning as in a standard filebuf.
72*404b540aSrobert * @param size Optimal or preferred size of internal buffer, in chars.
73*404b540aSrobert *
74*404b540aSrobert * This constructor associates a file stream buffer with an open
75*404b540aSrobert * POSIX file descriptor. The file descriptor will be automatically
76*404b540aSrobert * closed when the stdio_filebuf is closed/destroyed.
77*404b540aSrobert */
78*404b540aSrobert stdio_filebuf(int __fd, std::ios_base::openmode __mode,
79*404b540aSrobert size_t __size = static_cast<size_t>(BUFSIZ));
80*404b540aSrobert
81*404b540aSrobert /**
82*404b540aSrobert * @param f An open @c FILE*.
83*404b540aSrobert * @param mode Same meaning as in a standard filebuf.
84*404b540aSrobert * @param size Optimal or preferred size of internal buffer, in chars.
85*404b540aSrobert * Defaults to system's @c BUFSIZ.
86*404b540aSrobert *
87*404b540aSrobert * This constructor associates a file stream buffer with an open
88*404b540aSrobert * C @c FILE*. The @c FILE* will not be automatically closed when the
89*404b540aSrobert * stdio_filebuf is closed/destroyed.
90*404b540aSrobert */
91*404b540aSrobert stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
92*404b540aSrobert size_t __size = static_cast<size_t>(BUFSIZ));
93*404b540aSrobert
94*404b540aSrobert /**
95*404b540aSrobert * Closes the external data stream if the file descriptor constructor
96*404b540aSrobert * was used.
97*404b540aSrobert */
98*404b540aSrobert virtual
99*404b540aSrobert ~stdio_filebuf();
100*404b540aSrobert
101*404b540aSrobert /**
102*404b540aSrobert * @return The underlying file descriptor.
103*404b540aSrobert *
104*404b540aSrobert * Once associated with an external data stream, this function can be
105*404b540aSrobert * used to access the underlying POSIX file descriptor. Note that
106*404b540aSrobert * there is no way for the library to track what you do with the
107*404b540aSrobert * descriptor, so be careful.
108*404b540aSrobert */
109*404b540aSrobert int
110*404b540aSrobert fd() { return this->_M_file.fd(); }
111*404b540aSrobert
112*404b540aSrobert /**
113*404b540aSrobert * @return The underlying FILE*.
114*404b540aSrobert *
115*404b540aSrobert * This function can be used to access the underlying "C" file pointer.
116*404b540aSrobert * Note that there is no way for the library to track what you do
117*404b540aSrobert * with the file, so be careful.
118*404b540aSrobert */
119*404b540aSrobert std::__c_file*
120*404b540aSrobert file() { return this->_M_file.file(); }
121*404b540aSrobert };
122*404b540aSrobert
123*404b540aSrobert template<typename _CharT, typename _Traits>
~stdio_filebuf()124*404b540aSrobert stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
125*404b540aSrobert { }
126*404b540aSrobert
127*404b540aSrobert template<typename _CharT, typename _Traits>
128*404b540aSrobert stdio_filebuf<_CharT, _Traits>::
stdio_filebuf(int __fd,std::ios_base::openmode __mode,size_t __size)129*404b540aSrobert stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
130*404b540aSrobert {
131*404b540aSrobert this->_M_file.sys_open(__fd, __mode);
132*404b540aSrobert if (this->is_open())
133*404b540aSrobert {
134*404b540aSrobert this->_M_mode = __mode;
135*404b540aSrobert this->_M_buf_size = __size;
136*404b540aSrobert this->_M_allocate_internal_buffer();
137*404b540aSrobert this->_M_reading = false;
138*404b540aSrobert this->_M_writing = false;
139*404b540aSrobert this->_M_set_buffer(-1);
140*404b540aSrobert }
141*404b540aSrobert }
142*404b540aSrobert
143*404b540aSrobert template<typename _CharT, typename _Traits>
144*404b540aSrobert stdio_filebuf<_CharT, _Traits>::
stdio_filebuf(std::__c_file * __f,std::ios_base::openmode __mode,size_t __size)145*404b540aSrobert stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
146*404b540aSrobert size_t __size)
147*404b540aSrobert {
148*404b540aSrobert this->_M_file.sys_open(__f, __mode);
149*404b540aSrobert if (this->is_open())
150*404b540aSrobert {
151*404b540aSrobert this->_M_mode = __mode;
152*404b540aSrobert this->_M_buf_size = __size;
153*404b540aSrobert this->_M_allocate_internal_buffer();
154*404b540aSrobert this->_M_reading = false;
155*404b540aSrobert this->_M_writing = false;
156*404b540aSrobert this->_M_set_buffer(-1);
157*404b540aSrobert }
158*404b540aSrobert }
159*404b540aSrobert
160*404b540aSrobert _GLIBCXX_END_NAMESPACE
161*404b540aSrobert
162*404b540aSrobert #endif
163