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