1// File descriptor layer for filebuf -*- C++ -*- 2 3// Copyright (C) 2002, 2003, 2004, 2005, 2009, 2010 4// Free Software Foundation, Inc. 5// 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) 10// any later version. 11 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. 16 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. 20 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/>. 25 26/** @file ext/stdio_filebuf.h 27 * This file is a GNU extension to the Standard C++ Library. 28 */ 29 30#ifndef _STDIO_FILEBUF_H 31#define _STDIO_FILEBUF_H 1 32 33#pragma GCC system_header 34 35#include <fstream> 36 37_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 38 39 /** 40 * @brief Provides a layer of compatibility for C/POSIX. 41 * @ingroup io 42 * 43 * This GNU extension provides extensions for working with standard C 44 * FILE*'s and POSIX file descriptors. It must be instantiated by the 45 * user with the type of character used in the file stream, e.g., 46 * stdio_filebuf<char>. 47 */ 48 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 49 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 50 { 51 public: 52 // Types: 53 typedef _CharT char_type; 54 typedef _Traits traits_type; 55 typedef typename traits_type::int_type int_type; 56 typedef typename traits_type::pos_type pos_type; 57 typedef typename traits_type::off_type off_type; 58 typedef std::size_t size_t; 59 60 public: 61 /** 62 * deferred initialization 63 */ 64 stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} 65 66 /** 67 * @param fd An open file descriptor. 68 * @param mode Same meaning as in a standard filebuf. 69 * @param size Optimal or preferred size of internal buffer, in chars. 70 * 71 * This constructor associates a file stream buffer with an open 72 * POSIX file descriptor. The file descriptor will be automatically 73 * closed when the stdio_filebuf is closed/destroyed. 74 */ 75 stdio_filebuf(int __fd, std::ios_base::openmode __mode, 76 size_t __size = static_cast<size_t>(BUFSIZ)); 77 78 /** 79 * @param f An open @c FILE*. 80 * @param mode Same meaning as in a standard filebuf. 81 * @param size Optimal or preferred size of internal buffer, in chars. 82 * Defaults to system's @c BUFSIZ. 83 * 84 * This constructor associates a file stream buffer with an open 85 * C @c FILE*. The @c FILE* will not be automatically closed when the 86 * stdio_filebuf is closed/destroyed. 87 */ 88 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 89 size_t __size = static_cast<size_t>(BUFSIZ)); 90 91 /** 92 * Closes the external data stream if the file descriptor constructor 93 * was used. 94 */ 95 virtual 96 ~stdio_filebuf(); 97 98 /** 99 * @return The underlying file descriptor. 100 * 101 * Once associated with an external data stream, this function can be 102 * used to access the underlying POSIX file descriptor. Note that 103 * there is no way for the library to track what you do with the 104 * descriptor, so be careful. 105 */ 106 int 107 fd() { return this->_M_file.fd(); } 108 109 /** 110 * @return The underlying FILE*. 111 * 112 * This function can be used to access the underlying "C" file pointer. 113 * Note that there is no way for the library to track what you do 114 * with the file, so be careful. 115 */ 116 std::__c_file* 117 file() { return this->_M_file.file(); } 118 }; 119 120 template<typename _CharT, typename _Traits> 121 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 122 { } 123 124 template<typename _CharT, typename _Traits> 125 stdio_filebuf<_CharT, _Traits>:: 126 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) 127 { 128 this->_M_file.sys_open(__fd, __mode); 129 if (this->is_open()) 130 { 131 this->_M_mode = __mode; 132 this->_M_buf_size = __size; 133 this->_M_allocate_internal_buffer(); 134 this->_M_reading = false; 135 this->_M_writing = false; 136 this->_M_set_buffer(-1); 137 } 138 } 139 140 template<typename _CharT, typename _Traits> 141 stdio_filebuf<_CharT, _Traits>:: 142 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 143 size_t __size) 144 { 145 this->_M_file.sys_open(__f, __mode); 146 if (this->is_open()) 147 { 148 this->_M_mode = __mode; 149 this->_M_buf_size = __size; 150 this->_M_allocate_internal_buffer(); 151 this->_M_reading = false; 152 this->_M_writing = false; 153 this->_M_set_buffer(-1); 154 } 155 } 156 157_GLIBCXX_END_NAMESPACE 158 159#endif 160