libstdc++
|
00001 // File descriptor layer for filebuf -*- C++ -*- 00002 00003 // Copyright (C) 2002-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file ext/stdio_filebuf.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _STDIO_FILEBUF_H 00030 #define _STDIO_FILEBUF_H 1 00031 00032 #pragma GCC system_header 00033 00034 #include <fstream> 00035 00036 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00037 { 00038 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00039 00040 /** 00041 * @brief Provides a layer of compatibility for C/POSIX. 00042 * @ingroup io 00043 * 00044 * This GNU extension provides extensions for working with standard C 00045 * FILE*'s and POSIX file descriptors. It must be instantiated by the 00046 * user with the type of character used in the file stream, e.g., 00047 * stdio_filebuf<char>. 00048 */ 00049 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 00050 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 00051 { 00052 public: 00053 // Types: 00054 typedef _CharT char_type; 00055 typedef _Traits traits_type; 00056 typedef typename traits_type::int_type int_type; 00057 typedef typename traits_type::pos_type pos_type; 00058 typedef typename traits_type::off_type off_type; 00059 typedef std::size_t size_t; 00060 00061 public: 00062 /** 00063 * deferred initialization 00064 */ 00065 stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} 00066 00067 /** 00068 * @param __fd An open file descriptor. 00069 * @param __mode Same meaning as in a standard filebuf. 00070 * @param __size Optimal or preferred size of internal buffer, 00071 * in chars. 00072 * 00073 * This constructor associates a file stream buffer with an open 00074 * POSIX file descriptor. The file descriptor will be automatically 00075 * closed when the stdio_filebuf is closed/destroyed. 00076 */ 00077 stdio_filebuf(int __fd, std::ios_base::openmode __mode, 00078 size_t __size = static_cast<size_t>(BUFSIZ)); 00079 00080 /** 00081 * @param __f An open @c FILE*. 00082 * @param __mode Same meaning as in a standard filebuf. 00083 * @param __size Optimal or preferred size of internal buffer, 00084 * in chars. Defaults to system's @c BUFSIZ. 00085 * 00086 * This constructor associates a file stream buffer with an open 00087 * C @c FILE*. The @c FILE* will not be automatically closed when the 00088 * stdio_filebuf is closed/destroyed. 00089 */ 00090 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 00091 size_t __size = static_cast<size_t>(BUFSIZ)); 00092 00093 /** 00094 * Closes the external data stream if the file descriptor constructor 00095 * was used. 00096 */ 00097 virtual 00098 ~stdio_filebuf(); 00099 00100 #if __cplusplus >= 201103L 00101 stdio_filebuf(stdio_filebuf&&) = default; 00102 stdio_filebuf& operator=(stdio_filebuf&&) = default; 00103 00104 void 00105 swap(stdio_filebuf& __fb) 00106 { std::basic_filebuf<_CharT, _Traits>::swap(__fb); } 00107 #endif 00108 00109 /** 00110 * @return The underlying file descriptor. 00111 * 00112 * Once associated with an external data stream, this function can be 00113 * used to access the underlying POSIX file descriptor. Note that 00114 * there is no way for the library to track what you do with the 00115 * descriptor, so be careful. 00116 */ 00117 int 00118 fd() { return this->_M_file.fd(); } 00119 00120 /** 00121 * @return The underlying FILE*. 00122 * 00123 * This function can be used to access the underlying "C" file pointer. 00124 * Note that there is no way for the library to track what you do 00125 * with the file, so be careful. 00126 */ 00127 std::__c_file* 00128 file() { return this->_M_file.file(); } 00129 }; 00130 00131 template<typename _CharT, typename _Traits> 00132 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 00133 { } 00134 00135 template<typename _CharT, typename _Traits> 00136 stdio_filebuf<_CharT, _Traits>:: 00137 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) 00138 { 00139 this->_M_file.sys_open(__fd, __mode); 00140 if (this->is_open()) 00141 { 00142 this->_M_mode = __mode; 00143 this->_M_buf_size = __size; 00144 this->_M_allocate_internal_buffer(); 00145 this->_M_reading = false; 00146 this->_M_writing = false; 00147 this->_M_set_buffer(-1); 00148 } 00149 } 00150 00151 template<typename _CharT, typename _Traits> 00152 stdio_filebuf<_CharT, _Traits>:: 00153 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 00154 size_t __size) 00155 { 00156 this->_M_file.sys_open(__f, __mode); 00157 if (this->is_open()) 00158 { 00159 this->_M_mode = __mode; 00160 this->_M_buf_size = __size; 00161 this->_M_allocate_internal_buffer(); 00162 this->_M_reading = false; 00163 this->_M_writing = false; 00164 this->_M_set_buffer(-1); 00165 } 00166 } 00167 00168 _GLIBCXX_END_NAMESPACE_VERSION 00169 } // namespace 00170 00171 #endif