Home | History | Annotate | Download | only in ext
      1 // File descriptor layer for filebuf -*- C++ -*-
      2 
      3 // Copyright (C) 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file ext/stdio_filebuf.h
     26  *  This file is a GNU extension to the Standard C++ Library.
     27  */
     28 
     29 #ifndef _STDIO_FILEBUF_H
     30 #define _STDIO_FILEBUF_H 1
     31 
     32 #pragma GCC system_header
     33 
     34 #include <fstream>
     35 
     36 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
     37 
     38   /**
     39    *  @brief Provides a layer of compatibility for C/POSIX.
     40    *
     41    *  This GNU extension provides extensions for working with standard C
     42    *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
     43    *  user with the type of character used in the file stream, e.g.,
     44    *  stdio_filebuf<char>.
     45   */
     46   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
     47     class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
     48     {
     49     public:
     50       // Types:
     51       typedef _CharT				        char_type;
     52       typedef _Traits				        traits_type;
     53       typedef typename traits_type::int_type		int_type;
     54       typedef typename traits_type::pos_type		pos_type;
     55       typedef typename traits_type::off_type		off_type;
     56       typedef std::size_t                               size_t;
     57 
     58     public:
     59       /**
     60        * deferred initialization
     61       */
     62       stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
     63 
     64       /**
     65        *  @param  fd  An open file descriptor.
     66        *  @param  mode  Same meaning as in a standard filebuf.
     67        *  @param  size  Optimal or preferred size of internal buffer, in chars.
     68        *
     69        *  This constructor associates a file stream buffer with an open
     70        *  POSIX file descriptor. The file descriptor will be automatically
     71        *  closed when the stdio_filebuf is closed/destroyed.
     72       */
     73       stdio_filebuf(int __fd, std::ios_base::openmode __mode,
     74 		    size_t __size = static_cast<size_t>(BUFSIZ));
     75 
     76       /**
     77        *  @param  f  An open @c FILE*.
     78        *  @param  mode  Same meaning as in a standard filebuf.
     79        *  @param  size  Optimal or preferred size of internal buffer, in chars.
     80        *                Defaults to system's @c BUFSIZ.
     81        *
     82        *  This constructor associates a file stream buffer with an open
     83        *  C @c FILE*.  The @c FILE* will not be automatically closed when the
     84        *  stdio_filebuf is closed/destroyed.
     85       */
     86       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
     87 		    size_t __size = static_cast<size_t>(BUFSIZ));
     88 
     89       /**
     90        *  Closes the external data stream if the file descriptor constructor
     91        *  was used.
     92       */
     93       virtual
     94       ~stdio_filebuf();
     95 
     96       /**
     97        *  @return  The underlying file descriptor.
     98        *
     99        *  Once associated with an external data stream, this function can be
    100        *  used to access the underlying POSIX file descriptor.  Note that
    101        *  there is no way for the library to track what you do with the
    102        *  descriptor, so be careful.
    103       */
    104       int
    105       fd() { return this->_M_file.fd(); }
    106 
    107       /**
    108        *  @return  The underlying FILE*.
    109        *
    110        *  This function can be used to access the underlying "C" file pointer.
    111        *  Note that there is no way for the library to track what you do
    112        *  with the file, so be careful.
    113        */
    114       std::__c_file*
    115       file() { return this->_M_file.file(); }
    116     };
    117 
    118   template<typename _CharT, typename _Traits>
    119     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
    120     { }
    121 
    122   template<typename _CharT, typename _Traits>
    123     stdio_filebuf<_CharT, _Traits>::
    124     stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
    125     {
    126       this->_M_file.sys_open(__fd, __mode);
    127       if (this->is_open())
    128 	{
    129 	  this->_M_mode = __mode;
    130 	  this->_M_buf_size = __size;
    131 	  this->_M_allocate_internal_buffer();
    132 	  this->_M_reading = false;
    133 	  this->_M_writing = false;
    134 	  this->_M_set_buffer(-1);
    135 	}
    136     }
    137 
    138   template<typename _CharT, typename _Traits>
    139     stdio_filebuf<_CharT, _Traits>::
    140     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
    141 		  size_t __size)
    142     {
    143       this->_M_file.sys_open(__f, __mode);
    144       if (this->is_open())
    145 	{
    146 	  this->_M_mode = __mode;
    147 	  this->_M_buf_size = __size;
    148 	  this->_M_allocate_internal_buffer();
    149 	  this->_M_reading = false;
    150 	  this->_M_writing = false;
    151 	  this->_M_set_buffer(-1);
    152 	}
    153     }
    154 
    155 _GLIBCXX_END_NAMESPACE
    156 
    157 #endif
    158