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, 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, in chars. 00084 * 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 /** 00101 * @return The underlying file descriptor. 00102 * 00103 * Once associated with an external data stream, this function can be 00104 * used to access the underlying POSIX file descriptor. Note that 00105 * there is no way for the library to track what you do with the 00106 * descriptor, so be careful. 00107 */ 00108 int 00109 fd() { return this->_M_file.fd(); } 00110 00111 /** 00112 * @return The underlying FILE*. 00113 * 00114 * This function can be used to access the underlying "C" file pointer. 00115 * Note that there is no way for the library to track what you do 00116 * with the file, so be careful. 00117 */ 00118 std::__c_file* 00119 file() { return this->_M_file.file(); } 00120 }; 00121 00122 template<typename _CharT, typename _Traits> 00123 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 00124 { } 00125 00126 template<typename _CharT, typename _Traits> 00127 stdio_filebuf<_CharT, _Traits>:: 00128 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) 00129 { 00130 this->_M_file.sys_open(__fd, __mode); 00131 if (this->is_open()) 00132 { 00133 this->_M_mode = __mode; 00134 this->_M_buf_size = __size; 00135 this->_M_allocate_internal_buffer(); 00136 this->_M_reading = false; 00137 this->_M_writing = false; 00138 this->_M_set_buffer(-1); 00139 } 00140 } 00141 00142 template<typename _CharT, typename _Traits> 00143 stdio_filebuf<_CharT, _Traits>:: 00144 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 00145 size_t __size) 00146 { 00147 this->_M_file.sys_open(__f, __mode); 00148 if (this->is_open()) 00149 { 00150 this->_M_mode = __mode; 00151 this->_M_buf_size = __size; 00152 this->_M_allocate_internal_buffer(); 00153 this->_M_reading = false; 00154 this->_M_writing = false; 00155 this->_M_set_buffer(-1); 00156 } 00157 } 00158 00159 _GLIBCXX_END_NAMESPACE_VERSION 00160 } // namespace 00161 00162 #endif