# stdio_filebuf.h

Go to the documentation of this file.

    00001 // File descriptor layer for filebuf -*- C++ -*-
    00002
    00003 // Copyright (C) 2002 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 2, 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 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00034 #ifndef _EXT_STDIO_FILEBUF
    00035 #define _EXT_STDIO_FILEBUF
    00036
    00037 #pragma GCC system_header
    00038 #include <fstream>
    00039
    00040 namespace __gnu_cxx
    00041 {
    00051   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
    00052     class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
    00053     {
    00054     public:
    00055       // Types:
    00056       typedef _CharT                                char_type;
    00057       typedef _Traits                               traits_type;
    00058       typedef typename traits_type::int_type        int_type;
    00059       typedef typename traits_type::pos_type        pos_type;
    00060       typedef typename traits_type::off_type        off_type;
    00061
    00062     protected:
    00063       // Stack-based buffer for unbuffered input.
    00064       char_type         _M_unbuf[4];
    00065
    00066     public:
    00077       stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del,
    00078             int_type __size);
    00079
    00090       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
    00091             int_type __size = static_cast<int_type>(BUFSIZ));
    00092
    00097       virtual
    00098       ~stdio_filebuf();
    00099
    00108       int
    00109       fd()
    00110       { return _M_file.fd(); }
    00111     };
    00112
    00113   template<typename _CharT, typename _Traits>
    00114     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
    00115     { }
    00116
    00117   template<typename _CharT, typename _Traits>
    00118     stdio_filebuf<_CharT, _Traits>::
    00119     stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del,
    00120           int_type __size)
    00121     {
    00122       _M_file.sys_open(__fd, __mode, __del);
    00123       if (this->is_open())
    00124     {
    00125       _M_mode = __mode;
    00126       if (__size > 0 && __size < 4)
    00127         {
    00128           // Specify unbuffered.
    00129           _M_buf = _M_unbuf;
    00130           _M_buf_size = __size;
    00131           _M_buf_size_opt = 0;
    00132         }
    00133       else
    00134         {
    00135           _M_buf_size_opt = __size;
    00136           _M_allocate_internal_buffer();
    00137         }
    00138       _M_set_indeterminate();
    00139     }
    00140     }
    00141
    00142   template<typename _CharT, typename _Traits>
    00143     stdio_filebuf<_CharT, _Traits>::
    00144     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
    00145           int_type __size)
    00146     {
    00147       _M_file.sys_open(__f, __mode);
    00148       if (this->is_open())
    00149     {
    00150       _M_mode = __mode;
    00151       if (__size > 0 && __size < 4)
    00152         {
    00153           // Specify unbuffered.
    00154           _M_buf = _M_unbuf;
    00155           _M_buf_size = __size;
    00156           _M_buf_size_opt = 0;
    00157         }
    00158       else
    00159         {
    00160           _M_buf_size_opt = __size;
    00161           _M_allocate_internal_buffer();
    00162         }
    00163       _M_set_indeterminate();
    00164     }
    00165     }
    00166 } // namespace __gnu_cxx
    00167
    00168 #endif /* _EXT_STDIO_FILEBUF */