libstdc++
|
00001 // File descriptor layer for filebuf -*- C++ -*- 00002 00003 // Copyright (C) 2002-2013 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 /** 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