libstdc++
|
00001 // File descriptor layer for filebuf -*- C++ -*- 00002 00003 // Copyright (C) 2002, 2003, 2004, 2005, 2009 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 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 00037 00038 /** 00039 * @brief Provides a layer of compatibility for C/POSIX. 00040 * 00041 * This GNU extension provides extensions for working with standard C 00042 * FILE*'s and POSIX file descriptors. It must be instantiated by the 00043 * user with the type of character used in the file stream, e.g., 00044 * stdio_filebuf<char>. 00045 */ 00046 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 00047 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 00048 { 00049 public: 00050 // Types: 00051 typedef _CharT char_type; 00052 typedef _Traits traits_type; 00053 typedef typename traits_type::int_type int_type; 00054 typedef typename traits_type::pos_type pos_type; 00055 typedef typename traits_type::off_type off_type; 00056 typedef std::size_t size_t; 00057 00058 public: 00059 /** 00060 * deferred initialization 00061 */ 00062 stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} 00063 00064 /** 00065 * @param fd An open file descriptor. 00066 * @param mode Same meaning as in a standard filebuf. 00067 * @param size Optimal or preferred size of internal buffer, in chars. 00068 * 00069 * This constructor associates a file stream buffer with an open 00070 * POSIX file descriptor. The file descriptor will be automatically 00071 * closed when the stdio_filebuf is closed/destroyed. 00072 */ 00073 stdio_filebuf(int __fd, std::ios_base::openmode __mode, 00074 size_t __size = static_cast<size_t>(BUFSIZ)); 00075 00076 /** 00077 * @param f An open @c FILE*. 00078 * @param mode Same meaning as in a standard filebuf. 00079 * @param size Optimal or preferred size of internal buffer, in chars. 00080 * Defaults to system's @c BUFSIZ. 00081 * 00082 * This constructor associates a file stream buffer with an open 00083 * C @c FILE*. The @c FILE* will not be automatically closed when the 00084 * stdio_filebuf is closed/destroyed. 00085 */ 00086 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 00087 size_t __size = static_cast<size_t>(BUFSIZ)); 00088 00089 /** 00090 * Closes the external data stream if the file descriptor constructor 00091 * was used. 00092 */ 00093 virtual 00094 ~stdio_filebuf(); 00095 00096 /** 00097 * @return The underlying file descriptor. 00098 * 00099 * Once associated with an external data stream, this function can be 00100 * used to access the underlying POSIX file descriptor. Note that 00101 * there is no way for the library to track what you do with the 00102 * descriptor, so be careful. 00103 */ 00104 int 00105 fd() { return this->_M_file.fd(); } 00106 00107 /** 00108 * @return The underlying FILE*. 00109 * 00110 * This function can be used to access the underlying "C" file pointer. 00111 * Note that there is no way for the library to track what you do 00112 * with the file, so be careful. 00113 */ 00114 std::__c_file* 00115 file() { return this->_M_file.file(); } 00116 }; 00117 00118 template<typename _CharT, typename _Traits> 00119 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 00120 { } 00121 00122 template<typename _CharT, typename _Traits> 00123 stdio_filebuf<_CharT, _Traits>:: 00124 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) 00125 { 00126 this->_M_file.sys_open(__fd, __mode); 00127 if (this->is_open()) 00128 { 00129 this->_M_mode = __mode; 00130 this->_M_buf_size = __size; 00131 this->_M_allocate_internal_buffer(); 00132 this->_M_reading = false; 00133 this->_M_writing = false; 00134 this->_M_set_buffer(-1); 00135 } 00136 } 00137 00138 template<typename _CharT, typename _Traits> 00139 stdio_filebuf<_CharT, _Traits>:: 00140 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 00141 size_t __size) 00142 { 00143 this->_M_file.sys_open(__f, __mode); 00144 if (this->is_open()) 00145 { 00146 this->_M_mode = __mode; 00147 this->_M_buf_size = __size; 00148 this->_M_allocate_internal_buffer(); 00149 this->_M_reading = false; 00150 this->_M_writing = false; 00151 this->_M_set_buffer(-1); 00152 } 00153 } 00154 00155 _GLIBCXX_END_NAMESPACE 00156 00157 #endif