137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// File descriptor layer for filebuf -*- C++ -*- 237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// Copyright (C) 2002-2013 Free Software Foundation, Inc. 437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// 537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// This file is part of the GNU ISO C++ Library. This library is free 637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// software; you can redistribute it and/or modify it under the 737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// terms of the GNU General Public License as published by the 837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// Free Software Foundation; either version 3, or (at your option) 937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// any later version. 1037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 1137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// This library is distributed in the hope that it will be useful, 1237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// but WITHOUT ANY WARRANTY; without even the implied warranty of 1337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// GNU General Public License for more details. 1537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 1637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// Under Section 7 of GPL version 3, you are granted additional 1737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// permissions described in the GCC Runtime Library Exception, version 1837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// 3.1, as published by the Free Software Foundation. 1937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 2037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// You should have received a copy of the GNU General Public License and 2137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// a copy of the GCC Runtime Library Exception along with this program; 2237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh// <http://www.gnu.org/licenses/>. 2437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 2537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh/** @file ext/stdio_filebuf.h 2637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * This file is a GNU extension to the Standard C++ Library. 2737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 2837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 2937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh#ifndef _STDIO_FILEBUF_H 3037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh#define _STDIO_FILEBUF_H 1 3137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 3237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh#pragma GCC system_header 3337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 3437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh#include <fstream> 3537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 3637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsiehnamespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 3737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh{ 3837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh_GLIBCXX_BEGIN_NAMESPACE_VERSION 3937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 4037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 4137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @brief Provides a layer of compatibility for C/POSIX. 4237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @ingroup io 4337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * 4437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * This GNU extension provides extensions for working with standard C 4537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * FILE*'s and POSIX file descriptors. It must be instantiated by the 4637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * user with the type of character used in the file stream, e.g., 4737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * stdio_filebuf<char>. 4837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 4937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 5037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 5137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { 5237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh public: 5337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh // Types: 5437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef _CharT char_type; 5537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef _Traits traits_type; 5637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef typename traits_type::int_type int_type; 5737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef typename traits_type::pos_type pos_type; 5837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef typename traits_type::off_type off_type; 5937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh typedef std::size_t size_t; 6037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 6137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh public: 6237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 6337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * deferred initialization 6437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 6537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} 6637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 6737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 6837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __fd An open file descriptor. 6937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __mode Same meaning as in a standard filebuf. 7037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __size Optimal or preferred size of internal buffer, 7137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * in chars. 7237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * 7337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * This constructor associates a file stream buffer with an open 7437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * POSIX file descriptor. The file descriptor will be automatically 7537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * closed when the stdio_filebuf is closed/destroyed. 7637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 7737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf(int __fd, std::ios_base::openmode __mode, 7837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh size_t __size = static_cast<size_t>(BUFSIZ)); 7937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 8037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 8137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __f An open @c FILE*. 8237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __mode Same meaning as in a standard filebuf. 8337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @param __size Optimal or preferred size of internal buffer, 8437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * in chars. Defaults to system's @c BUFSIZ. 8537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * 8637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * This constructor associates a file stream buffer with an open 8737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * C @c FILE*. The @c FILE* will not be automatically closed when the 8837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * stdio_filebuf is closed/destroyed. 8937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 9037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 9137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh size_t __size = static_cast<size_t>(BUFSIZ)); 9237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 9337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 9437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * Closes the external data stream if the file descriptor constructor 9537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * was used. 9637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 9737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh virtual 9837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh ~stdio_filebuf(); 9937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 10037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 10137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @return The underlying file descriptor. 10237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * 10337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * Once associated with an external data stream, this function can be 10437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * used to access the underlying POSIX file descriptor. Note that 10537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * there is no way for the library to track what you do with the 10637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * descriptor, so be careful. 10737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 10837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh int 10937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh fd() { return this->_M_file.fd(); } 11037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 11137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh /** 11237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * @return The underlying FILE*. 11337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * 11437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * This function can be used to access the underlying "C" file pointer. 11537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * Note that there is no way for the library to track what you do 11637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh * with the file, so be careful. 11737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh */ 11837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh std::__c_file* 11937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh file() { return this->_M_file.file(); } 12037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh }; 12137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 12237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh template<typename _CharT, typename _Traits> 12337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 12437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { } 12537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 12637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh template<typename _CharT, typename _Traits> 12737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf<_CharT, _Traits>:: 12837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) 12937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { 13037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_file.sys_open(__fd, __mode); 13137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh if (this->is_open()) 13237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { 13337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_mode = __mode; 13437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_buf_size = __size; 13537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_allocate_internal_buffer(); 13637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_reading = false; 13737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_writing = false; 13837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_set_buffer(-1); 13937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh } 14037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh } 14137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 14237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh template<typename _CharT, typename _Traits> 14337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf<_CharT, _Traits>:: 14437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 14537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh size_t __size) 14637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { 14737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_file.sys_open(__f, __mode); 14837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh if (this->is_open()) 14937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh { 15037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_mode = __mode; 15137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_buf_size = __size; 15237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_allocate_internal_buffer(); 15337f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_reading = false; 15437f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_writing = false; 15537f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh this->_M_set_buffer(-1); 15637f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh } 15737f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh } 15837f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 15937f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh_GLIBCXX_END_NAMESPACE_VERSION 16037f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh} // namespace 16137f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh 16237f12739251d2637c9405c75951962b5e27bbceeAndrew Hsieh#endif 163