libstdc++
stdio_sync_filebuf.h
Go to the documentation of this file.
00001 // Iostreams wrapper for stdio FILE* -*- C++ -*-
00002 
00003 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file ext/stdio_sync_filebuf.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _STDIO_SYNC_FILEBUF_H
00031 #define _STDIO_SYNC_FILEBUF_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <streambuf>
00036 #include <unistd.h>
00037 #include <cstdio>
00038 #include <bits/c++io.h>  // For __c_file
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       // Underlying stdio FILE
00069       std::__c_file* const _M_file;
00070 
00071       // Last character gotten. This is used when pbackfail is
00072       // called from basic_streambuf::sungetc()
00073       int_type _M_unget_buf;
00074 
00075     public:
00076       explicit
00077       stdio_sync_filebuf(std::__c_file* __f)
00078       : _M_file(__f), _M_unget_buf(traits_type::eof())
00079       { }
00080 
00081 #ifdef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
00082       ~stdio_sync_filebuf() {}
00083 #endif
00084       /**
00085        *  @return  The underlying FILE*.
00086        *
00087        *  This function can be used to access the underlying C file pointer.
00088        *  Note that there is no way for the library to track what you do
00089        *  with the file, so be careful.
00090        */
00091       std::__c_file* const
00092       file() { return this->_M_file; }
00093 
00094     protected:
00095       int_type
00096       syncgetc();
00097 
00098       int_type
00099       syncungetc(int_type __c);
00100 
00101       int_type
00102       syncputc(int_type __c);
00103 
00104       virtual int_type
00105       underflow()
00106       {
00107     int_type __c = this->syncgetc();
00108     return this->syncungetc(__c);
00109       }
00110 
00111       virtual int_type
00112       uflow()
00113       {
00114     // Store the gotten character in case we need to unget it.
00115     _M_unget_buf = this->syncgetc();
00116     return _M_unget_buf;
00117       }
00118 
00119       virtual int_type
00120       pbackfail(int_type __c = traits_type::eof())
00121       {
00122     int_type __ret;
00123     const int_type __eof = traits_type::eof();
00124 
00125     // Check if the unget or putback was requested
00126     if (traits_type::eq_int_type(__c, __eof)) // unget
00127       {
00128         if (!traits_type::eq_int_type(_M_unget_buf, __eof))
00129           __ret = this->syncungetc(_M_unget_buf);
00130         else // buffer invalid, fail.
00131           __ret = __eof;
00132       }
00133     else // putback
00134       __ret = this->syncungetc(__c);
00135 
00136     // The buffered character is no longer valid, discard it.
00137     _M_unget_buf = __eof;
00138     return __ret;
00139       }
00140 
00141       virtual std::streamsize
00142       xsgetn(char_type* __s, std::streamsize __n);
00143 
00144       virtual int_type
00145       overflow(int_type __c = traits_type::eof())
00146       {
00147     int_type __ret;
00148     if (traits_type::eq_int_type(__c, traits_type::eof()))
00149       {
00150         if (std::fflush(_M_file))
00151           __ret = traits_type::eof();
00152         else
00153           __ret = traits_type::not_eof(__c);
00154       }
00155     else
00156       __ret = this->syncputc(__c);
00157     return __ret;
00158       }
00159 
00160       virtual std::streamsize
00161       xsputn(const char_type* __s, std::streamsize __n);
00162 
00163       virtual int
00164       sync()
00165       { return std::fflush(_M_file); }
00166 
00167       virtual std::streampos
00168       seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
00169           std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
00170       {
00171     std::streampos __ret(std::streamoff(-1));
00172     int __whence;
00173     if (__dir == std::ios_base::beg)
00174       __whence = SEEK_SET;
00175     else if (__dir == std::ios_base::cur)
00176       __whence = SEEK_CUR;
00177     else
00178       __whence = SEEK_END;
00179 #ifdef _GLIBCXX_USE_LFS
00180     if (!fseeko64(_M_file, __off, __whence))
00181       __ret = std::streampos(ftello64(_M_file));
00182 #else
00183     if (!fseek(_M_file, __off, __whence))
00184       __ret = std::streampos(std::ftell(_M_file));
00185 #endif
00186     return __ret;
00187       }
00188 
00189       virtual std::streampos
00190       seekpos(std::streampos __pos,
00191           std::ios_base::openmode __mode =
00192           std::ios_base::in | std::ios_base::out)
00193       { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
00194     };
00195 
00196   template<>
00197     inline stdio_sync_filebuf<char>::int_type
00198     stdio_sync_filebuf<char>::syncgetc()
00199     { return std::getc(_M_file); }
00200 
00201   template<>
00202     inline stdio_sync_filebuf<char>::int_type
00203     stdio_sync_filebuf<char>::syncungetc(int_type __c)
00204     { return std::ungetc(__c, _M_file); }
00205 
00206   template<>
00207     inline stdio_sync_filebuf<char>::int_type
00208     stdio_sync_filebuf<char>::syncputc(int_type __c)
00209     { return std::putc(__c, _M_file); }
00210 
00211   template<>
00212 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
00213     inline
00214 #endif
00215     std::streamsize
00216     stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
00217     {
00218       std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
00219       if (__ret > 0)
00220     _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
00221       else
00222     _M_unget_buf = traits_type::eof();
00223       return __ret;
00224     }
00225 
00226   template<>
00227 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
00228     inline
00229 #endif
00230     std::streamsize
00231     stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
00232     { return std::fwrite(__s, 1, __n, _M_file); }
00233 
00234 #ifdef _GLIBCXX_USE_WCHAR_T
00235   template<>
00236     inline stdio_sync_filebuf<wchar_t>::int_type
00237     stdio_sync_filebuf<wchar_t>::syncgetc()
00238     { return std::getwc(_M_file); }
00239 
00240   template<>
00241     inline stdio_sync_filebuf<wchar_t>::int_type
00242     stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
00243     { return std::ungetwc(__c, _M_file); }
00244 
00245   template<>
00246     inline stdio_sync_filebuf<wchar_t>::int_type
00247     stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
00248     { return std::putwc(__c, _M_file); }
00249 
00250   template<>
00251 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
00252     inline
00253 #endif
00254     std::streamsize
00255     stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
00256     {
00257       std::streamsize __ret = 0;
00258       const int_type __eof = traits_type::eof();
00259       while (__n--)
00260     {
00261       int_type __c = this->syncgetc();
00262       if (traits_type::eq_int_type(__c, __eof))
00263         break;
00264       __s[__ret] = traits_type::to_char_type(__c);
00265       ++__ret;
00266     }
00267 
00268       if (__ret > 0)
00269     _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
00270       else
00271     _M_unget_buf = traits_type::eof();
00272       return __ret;
00273     }
00274 
00275   template<>
00276 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
00277     inline
00278 #endif
00279     std::streamsize
00280     stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
00281                     std::streamsize __n)
00282     {
00283       std::streamsize __ret = 0;
00284       const int_type __eof = traits_type::eof();
00285       while (__n--)
00286     {
00287       if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
00288         break;
00289       ++__ret;
00290     }
00291       return __ret;
00292     }
00293 #endif
00294 
00295 #if _GLIBCXX_EXTERN_TEMPLATE
00296   extern template class stdio_sync_filebuf<char>;
00297 #ifdef _GLIBCXX_USE_WCHAR_T
00298   extern template class stdio_sync_filebuf<wchar_t>;
00299 #endif
00300 #endif
00301 
00302 _GLIBCXX_END_NAMESPACE_VERSION
00303 } // namespace
00304 
00305 #endif