1e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// File descriptor layer for filebuf -*- C++ -*- 2e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 3e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// Copyright (C) 2002-2013 Free Software Foundation, Inc. 4e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// 5e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// This file is part of the GNU ISO C++ Library. This library is free 6e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// software; you can redistribute it and/or modify it under the 7e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// terms of the GNU General Public License as published by the 8e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// Free Software Foundation; either version 3, or (at your option) 9e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// any later version. 10e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 11e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// This library is distributed in the hope that it will be useful, 12e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// but WITHOUT ANY WARRANTY; without even the implied warranty of 13e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// GNU General Public License for more details. 15e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 16e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// Under Section 7 of GPL version 3, you are granted additional 17e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// permissions described in the GCC Runtime Library Exception, version 18e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// 3.1, as published by the Free Software Foundation. 19e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 20e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// You should have received a copy of the GNU General Public License and 21e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// a copy of the GCC Runtime Library Exception along with this program; 22e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh// <http://www.gnu.org/licenses/>. 24e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 25e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh/** @file ext/stdio_filebuf.h 26e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * This file is a GNU extension to the Standard C++ Library. 27e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh */ 28e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 29e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh#ifndef _STDIO_FILEBUF_H 30e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh#define _STDIO_FILEBUF_H 1 31e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 32e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh#pragma GCC system_header 33e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 34e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh#include <fstream> 35e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 36e7de7d971409d955ca138406d5062499bc554451Andrew Hsiehnamespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 37e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh{ 38e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh_GLIBCXX_BEGIN_NAMESPACE_VERSION 39e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 40e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh /** 41e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * @brief Provides a layer of compatibility for C/POSIX. 42e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * @ingroup io 43e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * 44e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * This GNU extension provides extensions for working with standard C 45e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * FILE*'s and POSIX file descriptors. It must be instantiated by the 46e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * user with the type of character used in the file stream, e.g., 47e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * stdio_filebuf<char>. 48e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh */ 49e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 50e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 51e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh { 52e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh public: 53e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh // Types: 54e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef _CharT char_type; 55e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef _Traits traits_type; 56e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef typename traits_type::int_type int_type; 57e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef typename traits_type::pos_type pos_type; 58e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef typename traits_type::off_type off_type; 59e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh typedef std::size_t size_t; 60e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 61e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh public: 62e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh /** 63e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * deferred initialization 64e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh */ 65e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} 66e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh 67e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh /** 68e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * @param __fd An open file descriptor. 69e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * @param __mode Same meaning as in a standard filebuf. 70e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * @param __size Optimal or preferred size of internal buffer, 71e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * 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, 84e7de7d971409d955ca138406d5062499bc554451Andrew Hsieh * in chars. 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