1 // File descriptor layer for filebuf -*- C++ -*- 2 3 // Copyright (C) 2002 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 2, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // You should have received a copy of the GNU General Public License along 17 // with this library; see the file COPYING. If not, write to the Free 18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 19 // USA. 20 21 // As a special exception, you may use this file as part of a free software 22 // library without restriction. Specifically, if other files instantiate 23 // templates or use macros or inline functions from this file, or you compile 24 // this file and link it with other files to produce an executable, this 25 // file does not by itself cause the resulting executable to be covered by 26 // the GNU General Public License. This exception does not however 27 // invalidate any other reasons why the executable file might be covered by 28 // the GNU General Public License. 29 30 #include <fstream> 31 32 namespace __gnu_cxx 33 { 34 template<typename _CharT, typename _Traits = std::char_traits<_CharT> > 35 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> 36 { 37 public: 38 // Types: 39 typedef _CharT char_type; 40 typedef _Traits traits_type; 41 typedef typename traits_type::int_type int_type; 42 typedef typename traits_type::pos_type pos_type; 43 typedef typename traits_type::off_type off_type; 44 45 protected: 46 // Stack-based buffer for unbuffered input. 47 char_type _M_unbuf[4]; 48 49 public: 50 stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 51 int_type __size); 52 53 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 54 int_type __size = static_cast<int_type>(BUFSIZ)); 55 56 virtual 57 ~stdio_filebuf(); 58 59 int 60 fd() 61 { return _M_file.fd(); } 62 }; 63 64 template<typename _CharT, typename _Traits> 65 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() 66 { } 67 68 template<typename _CharT, typename _Traits> 69 stdio_filebuf<_CharT, _Traits>:: 70 stdio_filebuf(int __fd, std::ios_base::openmode __mode, bool __del, 71 int_type __size) 72 { 73 _M_file.sys_open(__fd, __mode, __del); 74 if (this->is_open()) 75 { 76 _M_mode = __mode; 77 _M_buf_size_opt = __size; 78 79 if (__size > 0 && __size < 4) 80 { 81 _M_buf = _M_unbuf; 82 _M_buf_size = __size; 83 } 84 else 85 _M_allocate_internal_buffer(); 86 87 _M_set_indeterminate(); 88 } 89 } 90 91 template<typename _CharT, typename _Traits> 92 stdio_filebuf<_CharT, _Traits>:: 93 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, 94 int_type __size) 95 { 96 _M_file.sys_open(__f, __mode); 97 if (this->is_open()) 98 { 99 _M_mode = __mode; 100 _M_buf_size_opt = __size; 101 102 if (__size > 0 && __size < 4) 103 { 104 _M_buf = _M_unbuf; 105 _M_buf_size = __size; 106 } 107 else 108 _M_allocate_internal_buffer(); 109 110 _M_set_indeterminate(); 111 } 112 } 113 } // namespace __gnu_cxx 114