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