# stdio_filebuf.h

Go to the documentation of this file.

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