stdio_filebuf.h revision 303975
11839Swollman// File descriptor layer for filebuf -*- C++ -*-
21839Swollman
31839Swollman// Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
41839Swollman//
51839Swollman// This file is part of the GNU ISO C++ Library.  This library is free
61839Swollman// software; you can redistribute it and/or modify it under the
71839Swollman// terms of the GNU General Public License as published by the
88858Srgrimes// Free Software Foundation; either version 2, or (at your option)
91839Swollman// any later version.
1013771Smpp
111839Swollman// This library is distributed in the hope that it will be useful,
128858Srgrimes// but WITHOUT ANY WARRANTY; without even the implied warranty of
131839Swollman// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141839Swollman// GNU General Public License for more details.
151839Swollman
161839Swollman// You should have received a copy of the GNU General Public License along
171839Swollman// with this library; see the file COPYING.  If not, write to the Free
181839Swollman// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
191839Swollman// USA.
201839Swollman
211839Swollman// As a special exception, you may use this file as part of a free software
221839Swollman// library without restriction.  Specifically, if other files instantiate
231839Swollman// templates or use macros or inline functions from this file, or you compile
241839Swollman// this file and link it with other files to produce an executable, this
251839Swollman// file does not by itself cause the resulting executable to be covered by
261839Swollman// the GNU General Public License.  This exception does not however
271839Swollman// invalidate any other reasons why the executable file might be covered by
281903Swollman// the GNU General Public License.
298858Srgrimes
301903Swollman/** @file ext/stdio_filebuf.h
3121059Speter *  This file is a GNU extension to the Standard C++ Library.
321839Swollman */
331839Swollman
341839Swollman#ifndef _STDIO_FILEBUF_H
351839Swollman#define _STDIO_FILEBUF_H 1
361839Swollman
371839Swollman#pragma GCC system_header
381839Swollman
391839Swollman#include <fstream>
401903Swollman
411903Swollman_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
421839Swollman
431839Swollman  /**
441839Swollman   *  @brief Provides a layer of compatibility for C/POSIX.
451839Swollman   *
461839Swollman   *  This GNU extension provides extensions for working with standard C
471839Swollman   *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
481839Swollman   *  user with the type of character used in the file stream, e.g.,
491839Swollman   *  stdio_filebuf<char>.
501839Swollman  */
511839Swollman  template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
521839Swollman    class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
531839Swollman    {
541839Swollman    public:
551839Swollman      // Types:
561839Swollman      typedef _CharT				        char_type;
571839Swollman      typedef _Traits				        traits_type;
581839Swollman      typedef typename traits_type::int_type		int_type;
598858Srgrimes      typedef typename traits_type::pos_type		pos_type;
601839Swollman      typedef typename traits_type::off_type		off_type;
611839Swollman      typedef std::size_t                               size_t;
626685Sphk
636685Sphk    public:
646685Sphk      /**
651839Swollman       * deferred initialization
661839Swollman      */
671839Swollman      stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
681839Swollman
691839Swollman      /**
701839Swollman       *  @param  fd  An open file descriptor.
711839Swollman       *  @param  mode  Same meaning as in a standard filebuf.
721839Swollman       *  @param  size  Optimal or preferred size of internal buffer, in chars.
731839Swollman       *
741839Swollman       *  This constructor associates a file stream buffer with an open
751839Swollman       *  POSIX file descriptor. The file descriptor will be automatically
761839Swollman       *  closed when the stdio_filebuf is closed/destroyed.
771839Swollman      */
781839Swollman      stdio_filebuf(int __fd, std::ios_base::openmode __mode,
791839Swollman		    size_t __size = static_cast<size_t>(BUFSIZ));
801839Swollman
811839Swollman      /**
821839Swollman       *  @param  f  An open @c FILE*.
831903Swollman       *  @param  mode  Same meaning as in a standard filebuf.
841903Swollman       *  @param  size  Optimal or preferred size of internal buffer, in chars.
851903Swollman       *                Defaults to system's @c BUFSIZ.
861903Swollman       *
8721059Speter       *  This constructor associates a file stream buffer with an open
881903Swollman       *  C @c FILE*.  The @c FILE* will not be automatically closed when the
891903Swollman       *  stdio_filebuf is closed/destroyed.
9021059Speter      */
9121059Speter      stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
9221059Speter		    size_t __size = static_cast<size_t>(BUFSIZ));
931903Swollman
941839Swollman      /**
951903Swollman       *  Closes the external data stream if the file descriptor constructor
96       *  was used.
97      */
98      virtual
99      ~stdio_filebuf();
100
101      /**
102       *  @return  The underlying file descriptor.
103       *
104       *  Once associated with an external data stream, this function can be
105       *  used to access the underlying POSIX file descriptor.  Note that
106       *  there is no way for the library to track what you do with the
107       *  descriptor, so be careful.
108      */
109      int
110      fd() { return this->_M_file.fd(); }
111
112      /**
113       *  @return  The underlying FILE*.
114       *
115       *  This function can be used to access the underlying "C" file pointer.
116       *  Note that there is no way for the library to track what you do
117       *  with the file, so be careful.
118       */
119      std::__c_file*
120      file() { return this->_M_file.file(); }
121    };
122
123  template<typename _CharT, typename _Traits>
124    stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
125    { }
126
127  template<typename _CharT, typename _Traits>
128    stdio_filebuf<_CharT, _Traits>::
129    stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
130    {
131      this->_M_file.sys_open(__fd, __mode);
132      if (this->is_open())
133	{
134	  this->_M_mode = __mode;
135	  this->_M_buf_size = __size;
136	  this->_M_allocate_internal_buffer();
137	  this->_M_reading = false;
138	  this->_M_writing = false;
139	  this->_M_set_buffer(-1);
140	}
141    }
142
143  template<typename _CharT, typename _Traits>
144    stdio_filebuf<_CharT, _Traits>::
145    stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
146		  size_t __size)
147    {
148      this->_M_file.sys_open(__f, __mode);
149      if (this->is_open())
150	{
151	  this->_M_mode = __mode;
152	  this->_M_buf_size = __size;
153	  this->_M_allocate_internal_buffer();
154	  this->_M_reading = false;
155	  this->_M_writing = false;
156	  this->_M_set_buffer(-1);
157	}
158    }
159
160_GLIBCXX_END_NAMESPACE
161
162#endif
163