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,
72e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *                 in chars.
73e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *
74e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  This constructor associates a file stream buffer with an open
75e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  POSIX file descriptor. The file descriptor will be automatically
76e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  closed when the stdio_filebuf is closed/destroyed.
77e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      */
78e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      stdio_filebuf(int __fd, std::ios_base::openmode __mode,
79e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh		    size_t __size = static_cast<size_t>(BUFSIZ));
80e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
81e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      /**
82e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  @param  __f  An open @c FILE*.
83e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  @param  __mode  Same meaning as in a standard filebuf.
84e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  @param  __size Optimal or preferred size of internal buffer,
85e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *                 in chars.  Defaults to system's @c BUFSIZ.
86e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *
87e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  This constructor associates a file stream buffer with an open
88e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  C @c FILE*.  The @c FILE* will not be automatically closed when the
89e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  stdio_filebuf is closed/destroyed.
90e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      */
91e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
92e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh		    size_t __size = static_cast<size_t>(BUFSIZ));
93e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
94e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      /**
95e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  Closes the external data stream if the file descriptor constructor
96e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  was used.
97e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      */
98e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      virtual
99e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      ~stdio_filebuf();
100e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
101e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      /**
102e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  @return  The underlying file descriptor.
103e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *
104e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  Once associated with an external data stream, this function can be
105e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  used to access the underlying POSIX file descriptor.  Note that
106e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  there is no way for the library to track what you do with the
107e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  descriptor, so be careful.
108e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      */
109e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      int
110e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      fd() { return this->_M_file.fd(); }
111e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
112e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      /**
113e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  @return  The underlying FILE*.
114e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *
115e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  This function can be used to access the underlying "C" file pointer.
116e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  Note that there is no way for the library to track what you do
117e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       *  with the file, so be careful.
118e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh       */
119e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      std::__c_file*
120e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      file() { return this->_M_file.file(); }
121e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    };
122e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
123e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh  template<typename _CharT, typename _Traits>
124e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
125e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    { }
126e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
127e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh  template<typename _CharT, typename _Traits>
128e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    stdio_filebuf<_CharT, _Traits>::
129e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
130e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    {
131e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      this->_M_file.sys_open(__fd, __mode);
132e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      if (this->is_open())
133e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	{
134e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_mode = __mode;
135e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_buf_size = __size;
136e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_allocate_internal_buffer();
137e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_reading = false;
138e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_writing = false;
139e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_set_buffer(-1);
140e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	}
141e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    }
142e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
143e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh  template<typename _CharT, typename _Traits>
144e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    stdio_filebuf<_CharT, _Traits>::
145e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
146e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh		  size_t __size)
147e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    {
148e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      this->_M_file.sys_open(__f, __mode);
149e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh      if (this->is_open())
150e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	{
151e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_mode = __mode;
152e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_buf_size = __size;
153e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_allocate_internal_buffer();
154e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_reading = false;
155e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_writing = false;
156e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	  this->_M_set_buffer(-1);
157e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh	}
158e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh    }
159e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
160e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh_GLIBCXX_END_NAMESPACE_VERSION
161e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh} // namespace
162e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh
163e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh#endif
164