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