libstdc++
|
00001 // Iostreams wrapper for stdio FILE* -*- C++ -*- 00002 00003 // Copyright (C) 2003-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_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 #ifdef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL 00081 ~stdio_sync_filebuf() {} 00082 #endif 00083 /** 00084 * @return The underlying FILE*. 00085 * 00086 * This function can be used to access the underlying C file pointer. 00087 * Note that there is no way for the library to track what you do 00088 * with the file, so be careful. 00089 */ 00090 std::__c_file* const 00091 file() { return this->_M_file; } 00092 00093 protected: 00094 int_type 00095 syncgetc(); 00096 00097 int_type 00098 syncungetc(int_type __c); 00099 00100 int_type 00101 syncputc(int_type __c); 00102 00103 virtual int_type 00104 underflow() 00105 { 00106 int_type __c = this->syncgetc(); 00107 return this->syncungetc(__c); 00108 } 00109 00110 virtual int_type 00111 uflow() 00112 { 00113 // Store the gotten character in case we need to unget it. 00114 _M_unget_buf = this->syncgetc(); 00115 return _M_unget_buf; 00116 } 00117 00118 virtual int_type 00119 pbackfail(int_type __c = traits_type::eof()) 00120 { 00121 int_type __ret; 00122 const int_type __eof = traits_type::eof(); 00123 00124 // Check if the unget or putback was requested 00125 if (traits_type::eq_int_type(__c, __eof)) // unget 00126 { 00127 if (!traits_type::eq_int_type(_M_unget_buf, __eof)) 00128 __ret = this->syncungetc(_M_unget_buf); 00129 else // buffer invalid, fail. 00130 __ret = __eof; 00131 } 00132 else // putback 00133 __ret = this->syncungetc(__c); 00134 00135 // The buffered character is no longer valid, discard it. 00136 _M_unget_buf = __eof; 00137 return __ret; 00138 } 00139 00140 virtual std::streamsize 00141 xsgetn(char_type* __s, std::streamsize __n); 00142 00143 virtual int_type 00144 overflow(int_type __c = traits_type::eof()) 00145 { 00146 int_type __ret; 00147 if (traits_type::eq_int_type(__c, traits_type::eof())) 00148 { 00149 if (std::fflush(_M_file)) 00150 __ret = traits_type::eof(); 00151 else 00152 __ret = traits_type::not_eof(__c); 00153 } 00154 else 00155 __ret = this->syncputc(__c); 00156 return __ret; 00157 } 00158 00159 virtual std::streamsize 00160 xsputn(const char_type* __s, std::streamsize __n); 00161 00162 virtual int 00163 sync() 00164 { return std::fflush(_M_file); } 00165 00166 virtual std::streampos 00167 seekoff(std::streamoff __off, std::ios_base::seekdir __dir, 00168 std::ios_base::openmode = std::ios_base::in | std::ios_base::out) 00169 { 00170 std::streampos __ret(std::streamoff(-1)); 00171 int __whence; 00172 if (__dir == std::ios_base::beg) 00173 __whence = SEEK_SET; 00174 else if (__dir == std::ios_base::cur) 00175 __whence = SEEK_CUR; 00176 else 00177 __whence = SEEK_END; 00178 #ifdef _GLIBCXX_USE_LFS 00179 if (!fseeko64(_M_file, __off, __whence)) 00180 __ret = std::streampos(ftello64(_M_file)); 00181 #else 00182 if (!fseek(_M_file, __off, __whence)) 00183 __ret = std::streampos(std::ftell(_M_file)); 00184 #endif 00185 return __ret; 00186 } 00187 00188 virtual std::streampos 00189 seekpos(std::streampos __pos, 00190 std::ios_base::openmode __mode = 00191 std::ios_base::in | std::ios_base::out) 00192 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); } 00193 }; 00194 00195 template<> 00196 inline stdio_sync_filebuf<char>::int_type 00197 stdio_sync_filebuf<char>::syncgetc() 00198 { return std::getc(_M_file); } 00199 00200 template<> 00201 inline stdio_sync_filebuf<char>::int_type 00202 stdio_sync_filebuf<char>::syncungetc(int_type __c) 00203 { return std::ungetc(__c, _M_file); } 00204 00205 template<> 00206 inline stdio_sync_filebuf<char>::int_type 00207 stdio_sync_filebuf<char>::syncputc(int_type __c) 00208 { return std::putc(__c, _M_file); } 00209 00210 template<> 00211 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL 00212 inline 00213 #endif 00214 std::streamsize 00215 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n) 00216 { 00217 std::streamsize __ret = std::fread(__s, 1, __n, _M_file); 00218 if (__ret > 0) 00219 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]); 00220 else 00221 _M_unget_buf = traits_type::eof(); 00222 return __ret; 00223 } 00224 00225 template<> 00226 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL 00227 inline 00228 #endif 00229 std::streamsize 00230 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n) 00231 { return std::fwrite(__s, 1, __n, _M_file); } 00232 00233 #ifdef _GLIBCXX_USE_WCHAR_T 00234 template<> 00235 inline stdio_sync_filebuf<wchar_t>::int_type 00236 stdio_sync_filebuf<wchar_t>::syncgetc() 00237 { return std::getwc(_M_file); } 00238 00239 template<> 00240 inline stdio_sync_filebuf<wchar_t>::int_type 00241 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c) 00242 { return std::ungetwc(__c, _M_file); } 00243 00244 template<> 00245 inline stdio_sync_filebuf<wchar_t>::int_type 00246 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c) 00247 { return std::putwc(__c, _M_file); } 00248 00249 template<> 00250 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL 00251 inline 00252 #endif 00253 std::streamsize 00254 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n) 00255 { 00256 std::streamsize __ret = 0; 00257 const int_type __eof = traits_type::eof(); 00258 while (__n--) 00259 { 00260 int_type __c = this->syncgetc(); 00261 if (traits_type::eq_int_type(__c, __eof)) 00262 break; 00263 __s[__ret] = traits_type::to_char_type(__c); 00264 ++__ret; 00265 } 00266 00267 if (__ret > 0) 00268 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]); 00269 else 00270 _M_unget_buf = traits_type::eof(); 00271 return __ret; 00272 } 00273 00274 template<> 00275 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL 00276 inline 00277 #endif 00278 std::streamsize 00279 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s, 00280 std::streamsize __n) 00281 { 00282 std::streamsize __ret = 0; 00283 const int_type __eof = traits_type::eof(); 00284 while (__n--) 00285 { 00286 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof)) 00287 break; 00288 ++__ret; 00289 } 00290 return __ret; 00291 } 00292 #endif 00293 00294 #if _GLIBCXX_EXTERN_TEMPLATE 00295 extern template class stdio_sync_filebuf<char>; 00296 #ifdef _GLIBCXX_USE_WCHAR_T 00297 extern template class stdio_sync_filebuf<wchar_t>; 00298 #endif 00299 #endif 00300 00301 _GLIBCXX_END_NAMESPACE_VERSION 00302 } // namespace 00303 00304 #endif