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