1 // File descriptor layer for filebuf -*- C++ -*- 2 3 // Copyright (C) 2002 Free Software Foundation, Inc. 4 // 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) 9 // any later version. 10 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. 15 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, 19 // USA. 20 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. 29 30 /** @file ext/stdio_filebuf.h 31 * This file is a GNU extension to the Standard C++ Library. 32 */ 33 34 #ifndef _EXT_STDIO_FILEBUF 35 #define _EXT_STDIO_FILEBUF 36 37 #pragma GCC system_header 38 #include <fstream> 39 40 namespace __gnu_cxx 41 { 42 /** 43 * @class stdio_filebuf ext/stdio_filebuf.h <ext/stdio_filebuf.h> 44 * @brief Provides a layer of compatibility for C/POSIX. 45 * 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>. 50 */ 51 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 52 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 53 { 54 public: 55 // Types: 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; 62 63 protected: 64 // Stack-based buffer for unbuffered input. 65 char_type _M_unbuf[4]; 66 67 public: 68 /** 69 * @param fd An open file descriptor. 70 * @param mode Same meaning as in a standard filebuf. 71 * @param del Whether to close the file on destruction. 72 * @param size Optimal or preferred size of internal buffer, in bytes. 73 * 74 * This constructor associates a file stream buffer with an open 75 * POSIX file descriptor. Iff @a del is true, then the associated 76 * file will be closed when the stdio_filebuf is closed/destroyed. 77 */ 78 stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 79 size_t __size); 80 81 /** 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 bytes. 85 * Defaults to system's @c BUFSIZ. 86 * 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. 90 */ 91 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 92 size_t __size = static_cast<size_t>(BUFSIZ)); 93 94 /** 95 * Possibly closes the external data stream, in the case of the file 96 * descriptor constructor and @c del @c == @c true. 97 */ 98 virtual 99 ~stdio_filebuf(); 100 101 /** 102 * @return The underlying file descriptor. 103 * 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. 108 */ 109 int 110 fd() 111 { return _M_file.fd(); } 112 }; 113 114 template<typename _CharT, typename _Traits> 115 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 116 { } 117 118 template<typename _CharT, typename _Traits> 119 stdio_filebuf<_CharT, _Traits>:: 120 stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 121 size_t __size) 122 { 123 _M_file.sys_open(__fd, __mode, __del); 124 if (this->is_open()) 125 { 126 _M_mode = __mode; 127 if (__size > 0 && __size < 4) 128 { 129 // Specify not to use an allocated buffer. 130 _M_buf = _M_unbuf; 131 _M_buf_size = __size; 132 _M_buf_size_opt = 0; 133 } 134 else 135 { 136 _M_buf_size_opt = __size; 137 _M_allocate_internal_buffer(); 138 } 139 _M_set_indeterminate(); 140 } 141 } 142 143 template<typename _CharT, typename _Traits> 144 stdio_filebuf<_CharT, _Traits>:: 145 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 146 size_t __size) 147 { 148 _M_file.sys_open(__f, __mode); 149 if (this->is_open()) 150 { 151 _M_mode = __mode; 152 if (__size > 0 && __size < 4) 153 { 154 // Specify not to use an allocated buffer. 155 _M_buf = _M_unbuf; 156 _M_buf_size = __size; 157 _M_buf_size_opt = 0; 158 } 159 else 160 { 161 _M_buf_size_opt = __size; 162 _M_allocate_internal_buffer(); 163 } 164 _M_set_indeterminate(); 165 } 166 } 167 } // namespace __gnu_cxx 168 169 #endif /* _EXT_STDIO_FILEBUF */ 170