libstdc++
|
00001 // Iostreams wrapper for stdio FILE* -*- C++ -*- 00002 00003 // Copyright (C) 2003-2014 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_sync_filebuf.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _STDIO_SYNC_FILEBUF_H 00030 #define _STDIO_SYNC_FILEBUF_H 1 00031 00032 #pragma GCC system_header 00033 00034 #include <streambuf> 00035 #include <unistd.h> 00036 #include <cstdio> 00037 #include <bits/c++io.h> // For __c_file 00038 00039 #ifdef _GLIBCXX_USE_WCHAR_T 00040 #include <cwchar> 00041 #endif 00042 00043 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * @brief Provides a layer of compatibility for C. 00049 * @ingroup io 00050 * 00051 * This GNU extension provides extensions for working with standard 00052 * C FILE*'s. It must be instantiated by the user with the type of 00053 * character used in the file stream, e.g., stdio_filebuf<char>. 00054 */ 00055 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 00056 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits> 00057 { 00058 public: 00059 // Types: 00060 typedef _CharT char_type; 00061 typedef _Traits traits_type; 00062 typedef typename traits_type::int_type int_type; 00063 typedef typename traits_type::pos_type pos_type; 00064 typedef typename traits_type::off_type off_type; 00065 00066 private: 00067 // Underlying stdio FILE 00068 std::__c_file* const _M_file; 00069 00070 // Last character gotten. This is used when pbackfail is 00071 // called from basic_streambuf::sungetc() 00072 int_type _M_unget_buf; 00073 00074 public: 00075 explicit 00076 stdio_sync_filebuf(std::__c_file* __f) 00077 : _M_file(__f), _M_unget_buf(traits_type::eof()) 00078 { } 00079 00080 /** 00081 * @return The underlying FILE*. 00082 * 00083 * This function can be used to access the underlying C file pointer. 00084 * Note that there is no way for the library to track what you do 00085 * with the file, so be careful. 00086 */ 00087 std::__c_file* const 00088 file() { return this->_M_file; } 00089 00090 protected: 00091 int_type 00092 syncgetc(); 00093 00094 int_type 00095 syncungetc(int_type __c); 00096 00097 int_type 00098 syncputc(int_type __c); 00099 00100 virtual int_type 00101 underflow() 00102 { 00103 int_type __c = this->syncgetc(); 00104 return this->syncungetc(__c); 00105 } 00106 00107 virtual int_type 00108 uflow() 00109 { 00110 // Store the gotten character in case we need to unget it. 00111 _M_unget_buf = this->syncgetc(); 00112 return _M_unget_buf; 00113 } 00114 00115 virtual int_type 00116 pbackfail(int_type __c = traits_type::eof()) 00117 { 00118 int_type __ret; 00119 const int_type __eof = traits_type::eof(); 00120 00121 // Check if the unget or putback was requested 00122 if (traits_type::eq_int_type(__c, __eof)) // unget 00123 { 00124 if (!traits_type::eq_int_type(_M_unget_buf, __eof)) 00125 __ret = this->syncungetc(_M_unget_buf); 00126 else // buffer invalid, fail. 00127 __ret = __eof; 00128 } 00129 else // putback 00130 __ret = this->syncungetc(__c); 00131 00132 // The buffered character is no longer valid, discard it. 00133 _M_unget_buf = __eof; 00134 return __ret; 00135 } 00136 00137 virtual std::streamsize 00138 xsgetn(char_type* __s, std::streamsize __n); 00139 00140 virtual int_type 00141 overflow(int_type __c = traits_type::eof()) 00142 { 00143 int_type __ret; 00144 if (traits_type::eq_int_type(__c, traits_type::eof())) 00145 { 00146 if (std::fflush(_M_file)) 00147 __ret = traits_type::eof(); 00148 else 00149 __ret = traits_type::not_eof(__c); 00150 } 00151 else 00152 __ret = this->syncputc(__c); 00153 return __ret; 00154 } 00155 00156 virtual std::streamsize 00157 xsputn(const char_type* __s, std::streamsize __n); 00158 00159 virtual int 00160 sync() 00161 { return std::fflush(_M_file); } 00162 00163 virtual std::streampos 00164 seekoff(std::streamoff __off, std::ios_base::seekdir __dir, 00165 std::ios_base::openmode = std::ios_base::in | std::ios_base::out) 00166 { 00167 std::streampos __ret(std::streamoff(-1)); 00168 int __whence; 00169 if (__dir == std::ios_base::beg) 00170 __whence = SEEK_SET; 00171 else if (__dir == std::ios_base::cur) 00172 __whence = SEEK_CUR; 00173 else 00174 __whence = SEEK_END; 00175 #ifdef _GLIBCXX_USE_LFS 00176 if (!fseeko64(_M_file, __off, __whence)) 00177 __ret = std::streampos(ftello64(_M_file)); 00178 #else 00179 if (!fseek(_M_file, __off, __whence)) 00180 __ret = std::streampos(std::ftell(_M_file)); 00181 #endif 00182 return __ret; 00183 } 00184 00185 virtual std::streampos 00186 seekpos(std::streampos __pos, 00187 std::ios_base::openmode __mode = 00188 std::ios_base::in | std::ios_base::out) 00189 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); } 00190 }; 00191 00192 template<> 00193 inline stdio_sync_filebuf<char>::int_type 00194 stdio_sync_filebuf<char>::syncgetc() 00195 { return std::getc(_M_file); } 00196 00197 template<> 00198 inline stdio_sync_filebuf<char>::int_type 00199 stdio_sync_filebuf<char>::syncungetc(int_type __c) 00200 { return std::ungetc(__c, _M_file); } 00201 00202 template<> 00203 inline stdio_sync_filebuf<char>::int_type 00204 stdio_sync_filebuf<char>::syncputc(int_type __c) 00205 { return std::putc(__c, _M_file); } 00206 00207 template<> 00208 inline std::streamsize 00209 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n) 00210 { 00211 std::streamsize __ret = std::fread(__s, 1, __n, _M_file); 00212 if (__ret > 0) 00213 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]); 00214 else 00215 _M_unget_buf = traits_type::eof(); 00216 return __ret; 00217 } 00218 00219 template<> 00220 inline std::streamsize 00221 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n) 00222 { return std::fwrite(__s, 1, __n, _M_file); } 00223 00224 #ifdef _GLIBCXX_USE_WCHAR_T 00225 template<> 00226 inline stdio_sync_filebuf<wchar_t>::int_type 00227 stdio_sync_filebuf<wchar_t>::syncgetc() 00228 { return std::getwc(_M_file); } 00229 00230 template<> 00231 inline stdio_sync_filebuf<wchar_t>::int_type 00232 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c) 00233 { return std::ungetwc(__c, _M_file); } 00234 00235 template<> 00236 inline stdio_sync_filebuf<wchar_t>::int_type 00237 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c) 00238 { return std::putwc(__c, _M_file); } 00239 00240 template<> 00241 inline std::streamsize 00242 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n) 00243 { 00244 std::streamsize __ret = 0; 00245 const int_type __eof = traits_type::eof(); 00246 while (__n--) 00247 { 00248 int_type __c = this->syncgetc(); 00249 if (traits_type::eq_int_type(__c, __eof)) 00250 break; 00251 __s[__ret] = traits_type::to_char_type(__c); 00252 ++__ret; 00253 } 00254 00255 if (__ret > 0) 00256 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]); 00257 else 00258 _M_unget_buf = traits_type::eof(); 00259 return __ret; 00260 } 00261 00262 template<> 00263 inline std::streamsize 00264 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s, 00265 std::streamsize __n) 00266 { 00267 std::streamsize __ret = 0; 00268 const int_type __eof = traits_type::eof(); 00269 while (__n--) 00270 { 00271 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof)) 00272 break; 00273 ++__ret; 00274 } 00275 return __ret; 00276 } 00277 #endif 00278 00279 #if _GLIBCXX_EXTERN_TEMPLATE 00280 extern template class stdio_sync_filebuf<char>; 00281 #ifdef _GLIBCXX_USE_WCHAR_T 00282 extern template class stdio_sync_filebuf<wchar_t>; 00283 #endif 00284 #endif 00285 00286 _GLIBCXX_END_NAMESPACE_VERSION 00287 } // namespace 00288 00289 #endif