# stdio_filebuf.h

Go to the documentation of this file.

    00001 // File descriptor layer for filebuf -*- C++ -*-
    00002
    00003 // Copyright (C) 2002, 2003, 2004 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
    00030 /** @file ext/stdio_filebuf.h
    00031  *  This file is a GNU extension to the Standard C++ Library.
    00032  */
    00033
    00034 #ifndef _STDIO_FILEBUF_H
    00035 #define _STDIO_FILEBUF_H 1
    00036
    00037 #pragma GCC system_header
    00038
    00039 #include <fstream>
    00040
    00041 namespace __gnu_cxx
    00042 {
    00043   /**
    00044    *  @class stdio_filebuf ext/stdio_filebuf.h <ext/stdio_filebuf.h>
    00045    *  @brief Provides a layer of compatibility for C/POSIX.
    00046    *
    00047    *  This GNU extension provides extensions for working with standard C
    00048    *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
    00049    *  user with the type of character used in the file stream, e.g.,
    00050    *  stdio_filebuf<char>.
    00051   */
    00052   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
    00053     class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
    00054     {
    00055     public:
    00056       // Types:
    00057       typedef _CharT                        char_type;
    00058       typedef _Traits                       traits_type;
    00059       typedef typename traits_type::int_type        int_type;
    00060       typedef typename traits_type::pos_type        pos_type;
    00061       typedef typename traits_type::off_type        off_type;
    00062       typedef std::size_t                               size_t;
    00063
    00064     public:
    00065       /**
    00066        * deferred initialization
    00067       */
    00068       stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
    00069
    00070       /**
    00071        *  @param  fd  An open file descriptor.
    00072        *  @param  mode  Same meaning as in a standard filebuf.
    00073        *  @param  size  Optimal or preferred size of internal buffer, in chars.
    00074        *
    00075        *  This constructor associates a file stream buffer with an open
    00076        *  POSIX file descriptor. The file descriptor will be automatically
    00077        *  closed when the stdio_filebuf is closed/destroyed.
    00078       */
    00079       stdio_filebuf(int __fd, std::ios_base::openmode __mode,
    00080             size_t __size = static_cast<size_t>(BUFSIZ));
    00081
    00082       /**
    00083        *  @param  f  An open @c FILE*.
    00084        *  @param  mode  Same meaning as in a standard filebuf.
    00085        *  @param  size  Optimal or preferred size of internal buffer, in chars.
    00086        *                Defaults to system's @c BUFSIZ.
    00087        *
    00088        *  This constructor associates a file stream buffer with an open
    00089        *  C @c FILE*.  The @c FILE* will not be automatically closed when the
    00090        *  stdio_filebuf is closed/destroyed.
    00091       */
    00092       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
    00093             size_t __size = static_cast<size_t>(BUFSIZ));
    00094
    00095       /**
    00096        *  Closes the external data stream if the file descriptor constructor
    00097        *  was used.
    00098       */
    00099       virtual
    00100       ~stdio_filebuf();
    00101
    00102       /**
    00103        *  @return  The underlying file descriptor.
    00104        *
    00105        *  Once associated with an external data stream, this function can be
    00106        *  used to access the underlying POSIX file descriptor.  Note that
    00107        *  there is no way for the library to track what you do with the
    00108        *  descriptor, so be careful.
    00109       */
    00110       int
    00111       fd() { return this->_M_file.fd(); }
    00112
    00113       /**
    00114        *  @return  The underlying FILE*.
    00115        *
    00116        *  This function can be used to access the underlying "C" file pointer.
    00117        *  Note that there is no way for the library to track what you do
    00118        *  with the file, so be careful.
    00119        */
    00120       std::__c_file*
    00121       file() { return this->_M_file.file(); }
    00122     };
    00123
    00124   template<typename _CharT, typename _Traits>
    00125     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
    00126     { }
    00127
    00128   template<typename _CharT, typename _Traits>
    00129     stdio_filebuf<_CharT, _Traits>::
    00130     stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
    00131     {
    00132       this->_M_file.sys_open(__fd, __mode);
    00133       if (this->is_open())
    00134     {
    00135       this->_M_mode = __mode;
    00136       this->_M_buf_size = __size;
    00137       this->_M_allocate_internal_buffer();
    00138       this->_M_reading = false;
    00139       this->_M_writing = false;
    00140       this->_M_set_buffer(-1);
    00141     }
    00142     }
    00143
    00144   template<typename _CharT, typename _Traits>
    00145     stdio_filebuf<_CharT, _Traits>::
    00146     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
    00147           size_t __size)
    00148     {
    00149       this->_M_file.sys_open(__f, __mode);
    00150       if (this->is_open())
    00151     {
    00152       this->_M_mode = __mode;
    00153       this->_M_buf_size = __size;
    00154       this->_M_allocate_internal_buffer();
    00155       this->_M_reading = false;
    00156       this->_M_writing = false;
    00157       this->_M_set_buffer(-1);
    00158     }
    00159     }
    00160 } // namespace __gnu_cxx
    00161
    00162 #endif