100db7afdSDavid E. O'Brien // File descriptor layer for filebuf -*- C++ -*-
200db7afdSDavid E. O'Brien 
3*f8a1b7d9SAlexander Kabaev // Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
400db7afdSDavid E. O'Brien //
500db7afdSDavid E. O'Brien // This file is part of the GNU ISO C++ Library.  This library is free
600db7afdSDavid E. O'Brien // software; you can redistribute it and/or modify it under the
700db7afdSDavid E. O'Brien // terms of the GNU General Public License as published by the
800db7afdSDavid E. O'Brien // Free Software Foundation; either version 2, or (at your option)
900db7afdSDavid E. O'Brien // any later version.
1000db7afdSDavid E. O'Brien 
1100db7afdSDavid E. O'Brien // This library is distributed in the hope that it will be useful,
1200db7afdSDavid E. O'Brien // but WITHOUT ANY WARRANTY; without even the implied warranty of
1300db7afdSDavid E. O'Brien // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1400db7afdSDavid E. O'Brien // GNU General Public License for more details.
1500db7afdSDavid E. O'Brien 
1600db7afdSDavid E. O'Brien // You should have received a copy of the GNU General Public License along
1700db7afdSDavid E. O'Brien // with this library; see the file COPYING.  If not, write to the Free
18*f8a1b7d9SAlexander Kabaev // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
1900db7afdSDavid E. O'Brien // USA.
2000db7afdSDavid E. O'Brien 
2100db7afdSDavid E. O'Brien // As a special exception, you may use this file as part of a free software
2200db7afdSDavid E. O'Brien // library without restriction.  Specifically, if other files instantiate
2300db7afdSDavid E. O'Brien // templates or use macros or inline functions from this file, or you compile
2400db7afdSDavid E. O'Brien // this file and link it with other files to produce an executable, this
2500db7afdSDavid E. O'Brien // file does not by itself cause the resulting executable to be covered by
2600db7afdSDavid E. O'Brien // the GNU General Public License.  This exception does not however
2700db7afdSDavid E. O'Brien // invalidate any other reasons why the executable file might be covered by
2800db7afdSDavid E. O'Brien // the GNU General Public License.
2900db7afdSDavid E. O'Brien 
30ca6500fcSAlexander Kabaev /** @file ext/stdio_filebuf.h
31ca6500fcSAlexander Kabaev  *  This file is a GNU extension to the Standard C++ Library.
32ca6500fcSAlexander Kabaev  */
33ca6500fcSAlexander Kabaev 
34ffeaf689SAlexander Kabaev #ifndef _STDIO_FILEBUF_H
35ffeaf689SAlexander Kabaev #define _STDIO_FILEBUF_H 1
36ca6500fcSAlexander Kabaev 
37ca6500fcSAlexander Kabaev #pragma GCC system_header
38ffeaf689SAlexander Kabaev 
3900db7afdSDavid E. O'Brien #include <fstream>
4000db7afdSDavid E. O'Brien 
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)41*f8a1b7d9SAlexander Kabaev _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
42*f8a1b7d9SAlexander Kabaev 
43ca6500fcSAlexander Kabaev   /**
44ca6500fcSAlexander Kabaev    *  @brief Provides a layer of compatibility for C/POSIX.
45ca6500fcSAlexander Kabaev    *
46ca6500fcSAlexander Kabaev    *  This GNU extension provides extensions for working with standard C
47ca6500fcSAlexander Kabaev    *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
48ca6500fcSAlexander Kabaev    *  user with the type of character used in the file stream, e.g.,
49ca6500fcSAlexander Kabaev    *  stdio_filebuf<char>.
50ca6500fcSAlexander Kabaev   */
5100db7afdSDavid E. O'Brien   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
5200db7afdSDavid E. O'Brien     class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
5300db7afdSDavid E. O'Brien     {
5400db7afdSDavid E. O'Brien     public:
5500db7afdSDavid E. O'Brien       // Types:
5600db7afdSDavid E. O'Brien       typedef _CharT				        char_type;
5700db7afdSDavid E. O'Brien       typedef _Traits				        traits_type;
5800db7afdSDavid E. O'Brien       typedef typename traits_type::int_type		int_type;
5900db7afdSDavid E. O'Brien       typedef typename traits_type::pos_type		pos_type;
6000db7afdSDavid E. O'Brien       typedef typename traits_type::off_type		off_type;
611b86b14eSAlexander Kabaev       typedef std::size_t                               size_t;
6200db7afdSDavid E. O'Brien 
6300db7afdSDavid E. O'Brien     public:
64ca6500fcSAlexander Kabaev       /**
65ffeaf689SAlexander Kabaev        * deferred initialization
66ffeaf689SAlexander Kabaev       */
67ffeaf689SAlexander Kabaev       stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
68ffeaf689SAlexander Kabaev 
69ffeaf689SAlexander Kabaev       /**
70ca6500fcSAlexander Kabaev        *  @param  fd  An open file descriptor.
71ca6500fcSAlexander Kabaev        *  @param  mode  Same meaning as in a standard filebuf.
72ffeaf689SAlexander Kabaev        *  @param  size  Optimal or preferred size of internal buffer, in chars.
73ca6500fcSAlexander Kabaev        *
74ca6500fcSAlexander Kabaev        *  This constructor associates a file stream buffer with an open
75ffeaf689SAlexander Kabaev        *  POSIX file descriptor. The file descriptor will be automatically
76ffeaf689SAlexander Kabaev        *  closed when the stdio_filebuf is closed/destroyed.
77ca6500fcSAlexander Kabaev       */
78ffeaf689SAlexander Kabaev       stdio_filebuf(int __fd, std::ios_base::openmode __mode,
79ffeaf689SAlexander Kabaev 		    size_t __size = static_cast<size_t>(BUFSIZ));
8000db7afdSDavid E. O'Brien 
81ca6500fcSAlexander Kabaev       /**
82ca6500fcSAlexander Kabaev        *  @param  f  An open @c FILE*.
83ca6500fcSAlexander Kabaev        *  @param  mode  Same meaning as in a standard filebuf.
84ffeaf689SAlexander Kabaev        *  @param  size  Optimal or preferred size of internal buffer, in chars.
85ca6500fcSAlexander Kabaev        *                Defaults to system's @c BUFSIZ.
86ca6500fcSAlexander Kabaev        *
87ca6500fcSAlexander Kabaev        *  This constructor associates a file stream buffer with an open
88ca6500fcSAlexander Kabaev        *  C @c FILE*.  The @c FILE* will not be automatically closed when the
89ca6500fcSAlexander Kabaev        *  stdio_filebuf is closed/destroyed.
90ca6500fcSAlexander Kabaev       */
9100db7afdSDavid E. O'Brien       stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
921b86b14eSAlexander Kabaev 		    size_t __size = static_cast<size_t>(BUFSIZ));
9300db7afdSDavid E. O'Brien 
94ca6500fcSAlexander Kabaev       /**
95ffeaf689SAlexander Kabaev        *  Closes the external data stream if the file descriptor constructor
96ffeaf689SAlexander Kabaev        *  was used.
97ca6500fcSAlexander Kabaev       */
9800db7afdSDavid E. O'Brien       virtual
9900db7afdSDavid E. O'Brien       ~stdio_filebuf();
10000db7afdSDavid E. O'Brien 
101ca6500fcSAlexander Kabaev       /**
102ca6500fcSAlexander Kabaev        *  @return  The underlying file descriptor.
103ca6500fcSAlexander Kabaev        *
104ca6500fcSAlexander Kabaev        *  Once associated with an external data stream, this function can be
105ca6500fcSAlexander Kabaev        *  used to access the underlying POSIX file descriptor.  Note that
106ca6500fcSAlexander Kabaev        *  there is no way for the library to track what you do with the
107ca6500fcSAlexander Kabaev        *  descriptor, so be careful.
108ca6500fcSAlexander Kabaev       */
10900db7afdSDavid E. O'Brien       int
110ffeaf689SAlexander Kabaev       fd() { return this->_M_file.fd(); }
111ffeaf689SAlexander Kabaev 
112ffeaf689SAlexander Kabaev       /**
113ffeaf689SAlexander Kabaev        *  @return  The underlying FILE*.
114ffeaf689SAlexander Kabaev        *
115ffeaf689SAlexander Kabaev        *  This function can be used to access the underlying "C" file pointer.
116ffeaf689SAlexander Kabaev        *  Note that there is no way for the library to track what you do
117ffeaf689SAlexander Kabaev        *  with the file, so be careful.
118ffeaf689SAlexander Kabaev        */
119ffeaf689SAlexander Kabaev       std::__c_file*
120ffeaf689SAlexander Kabaev       file() { return this->_M_file.file(); }
12100db7afdSDavid E. O'Brien     };
12200db7afdSDavid E. O'Brien 
12300db7afdSDavid E. O'Brien   template<typename _CharT, typename _Traits>
~stdio_filebuf()12400db7afdSDavid E. O'Brien     stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
12500db7afdSDavid E. O'Brien     { }
12600db7afdSDavid E. O'Brien 
12700db7afdSDavid E. O'Brien   template<typename _CharT, typename _Traits>
12800db7afdSDavid E. O'Brien     stdio_filebuf<_CharT, _Traits>::
stdio_filebuf(int __fd,std::ios_base::openmode __mode,size_t __size)129ffeaf689SAlexander Kabaev     stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
13000db7afdSDavid E. O'Brien     {
131ffeaf689SAlexander Kabaev       this->_M_file.sys_open(__fd, __mode);
13200db7afdSDavid E. O'Brien       if (this->is_open())
13300db7afdSDavid E. O'Brien 	{
134ffeaf689SAlexander Kabaev 	  this->_M_mode = __mode;
135ffeaf689SAlexander Kabaev 	  this->_M_buf_size = __size;
136ffeaf689SAlexander Kabaev 	  this->_M_allocate_internal_buffer();
137ffeaf689SAlexander Kabaev 	  this->_M_reading = false;
138ffeaf689SAlexander Kabaev 	  this->_M_writing = false;
139ffeaf689SAlexander Kabaev 	  this->_M_set_buffer(-1);
14000db7afdSDavid E. O'Brien 	}
14100db7afdSDavid E. O'Brien     }
14200db7afdSDavid E. O'Brien 
14300db7afdSDavid E. O'Brien   template<typename _CharT, typename _Traits>
14400db7afdSDavid E. O'Brien     stdio_filebuf<_CharT, _Traits>::
stdio_filebuf(std::__c_file * __f,std::ios_base::openmode __mode,size_t __size)14500db7afdSDavid E. O'Brien     stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
1461b86b14eSAlexander Kabaev 		  size_t __size)
14700db7afdSDavid E. O'Brien     {
148ffeaf689SAlexander Kabaev       this->_M_file.sys_open(__f, __mode);
14900db7afdSDavid E. O'Brien       if (this->is_open())
15000db7afdSDavid E. O'Brien 	{
151ffeaf689SAlexander Kabaev 	  this->_M_mode = __mode;
152ffeaf689SAlexander Kabaev 	  this->_M_buf_size = __size;
153ffeaf689SAlexander Kabaev 	  this->_M_allocate_internal_buffer();
154ffeaf689SAlexander Kabaev 	  this->_M_reading = false;
155ffeaf689SAlexander Kabaev 	  this->_M_writing = false;
156ffeaf689SAlexander Kabaev 	  this->_M_set_buffer(-1);
15700db7afdSDavid E. O'Brien 	}
15800db7afdSDavid E. O'Brien     }
159*f8a1b7d9SAlexander Kabaev 
160*f8a1b7d9SAlexander Kabaev _GLIBCXX_END_NAMESPACE
161ca6500fcSAlexander Kabaev 
162ffeaf689SAlexander Kabaev #endif
163