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 
00030 /** @file ext/stdio_filebuf.h
00031  *  This file is a GNU extension to the Standard C++ Library.
00032  */
00033 
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 {
00042   /**
00043    *  @class stdio_filebuf ext/stdio_filebuf.h <ext/stdio_filebuf.h>
00044    *  @brief Provides a layer of compatibility for C/POSIX.
00045    *
00046    *  This GNU extension provides extensions for working with standard C
00047    *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
00048    *  user with the type of character used in the file stream, e.g.,
00049    *  stdio_filebuf<char>.
00050   */
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       typedef std::size_t                               size_t;
00062       
00063     protected:
00064       // Stack-based buffer for unbuffered input.
00065       char_type         _M_unbuf[4];
00066       
00067     public:
00068       /**
00069        *  @param  fd  An open file descriptor.
00070        *  @param  mode  Same meaning as in a standard filebuf.
00071        *  @param  del  Whether to close the file on destruction.
00072        *  @param  size  Optimal or preferred size of internal buffer, in bytes.
00073        *
00074        *  This constructor associates a file stream buffer with an open
00075        *  POSIX file descriptor.  Iff @a del is true, then the associated
00076        *  file will be closed when the stdio_filebuf is closed/destroyed.
00077       */
00078       stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 
00079             size_t __size);
00080 
00081       /**
00082        *  @param  f  An open @c FILE*.
00083        *  @param  mode  Same meaning as in a standard filebuf.
00084        *  @param  size  Optimal or preferred size of internal buffer, in bytes.
00085        *                Defaults to system's @c BUFSIZ.
00086        *
00087        *  This constructor associates a file stream buffer with an open
00088        *  C @c FILE*.  The @c FILE* will not be automatically closed when the
00089        *  stdio_filebuf is closed/destroyed.
00090       */
00091       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 
00092             size_t __size = static_cast<size_t>(BUFSIZ));
00093 
00094       /**
00095        *  Possibly closes the external data stream, in the case of the file
00096        *  descriptor constructor and @c del @c == @c true.
00097       */
00098       virtual
00099       ~stdio_filebuf();
00100 
00101       /**
00102        *  @return  The underlying file descriptor.
00103        *
00104        *  Once associated with an external data stream, this function can be
00105        *  used to access the underlying POSIX file descriptor.  Note that
00106        *  there is no way for the library to track what you do with the
00107        *  descriptor, so be careful.
00108       */
00109       int
00110       fd()
00111       { return _M_file.fd(); }
00112     };
00113 
00114   template<typename _CharT, typename _Traits>
00115     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
00116     { }
00117 
00118   template<typename _CharT, typename _Traits>
00119     stdio_filebuf<_CharT, _Traits>::
00120     stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 
00121           size_t __size)
00122     {
00123       _M_file.sys_open(__fd, __mode, __del);
00124       if (this->is_open())
00125     {
00126       _M_mode = __mode;
00127       if (__size > 0 && __size < 4)
00128         {
00129           // Specify not to use an allocated buffer.
00130           _M_buf = _M_unbuf;
00131           _M_buf_size = __size;
00132           _M_buf_size_opt = 0;
00133         }
00134       else
00135         {
00136           _M_buf_size_opt = __size;
00137           _M_allocate_internal_buffer();
00138         }
00139       _M_set_indeterminate();
00140     }
00141     }
00142 
00143   template<typename _CharT, typename _Traits>
00144     stdio_filebuf<_CharT, _Traits>::
00145     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 
00146           size_t __size)
00147     {
00148       _M_file.sys_open(__f, __mode);
00149       if (this->is_open())
00150     {
00151       _M_mode = __mode;
00152       if (__size > 0 && __size < 4)
00153         {
00154           // Specify not to use an allocated buffer.
00155           _M_buf = _M_unbuf;
00156           _M_buf_size = __size;
00157           _M_buf_size_opt = 0;
00158         }
00159       else
00160         {
00161           _M_buf_size_opt = __size;
00162           _M_allocate_internal_buffer();
00163         }
00164       _M_set_indeterminate();
00165     }
00166     }
00167 } // namespace __gnu_cxx
00168 
00169 #endif /* _EXT_STDIO_FILEBUF */

Generated on Thu Feb 10 23:22:57 2005 for libstdc++-v3 Source by  doxygen 1.4.0