| 1 | // File descriptor layer for filebuf -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2002-2025 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 3, 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 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file ext/stdio_filebuf.h |
| 26 | * This file is a GNU extension to the Standard C++ Library. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _STDIO_FILEBUF_H |
| 30 | #define _STDIO_FILEBUF_H 1 |
| 31 | |
| 32 | #ifdef _GLIBCXX_SYSHDR |
| 33 | #pragma GCC system_header |
| 34 | #endif |
| 35 | |
| 36 | #include <bits/requires_hosted.h> // GNU extensions are currently omitted |
| 37 | |
| 38 | #include <fstream> |
| 39 | |
| 40 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
| 41 | { |
| 42 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 43 | |
| 44 | /** |
| 45 | * @brief Provides a layer of compatibility for C/POSIX. |
| 46 | * @ingroup io |
| 47 | * |
| 48 | * This GNU extension provides extensions for working with standard C |
| 49 | * FILE*'s and POSIX file descriptors. It must be instantiated by the |
| 50 | * user with the type of character used in the file stream, e.g., |
| 51 | * stdio_filebuf<char>. |
| 52 | */ |
| 53 | template<typename _CharT, typename _Traits = std::char_traits<_CharT> > |
| 54 | class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits> |
| 55 | { |
| 56 | public: |
| 57 | // Types: |
| 58 | typedef _CharT char_type; |
| 59 | typedef _Traits traits_type; |
| 60 | typedef typename traits_type::int_type int_type; |
| 61 | typedef typename traits_type::pos_type pos_type; |
| 62 | typedef typename traits_type::off_type off_type; |
| 63 | typedef std::size_t size_t; |
| 64 | |
| 65 | public: |
| 66 | /** |
| 67 | * deferred initialization |
| 68 | */ |
| 69 | stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {} |
| 70 | |
| 71 | /** |
| 72 | * @param __fd An open file descriptor. |
| 73 | * @param __mode Same meaning as in a standard filebuf. |
| 74 | * @param __size Optimal or preferred size of internal buffer, |
| 75 | * in chars. |
| 76 | * |
| 77 | * This constructor associates a file stream buffer with an open |
| 78 | * POSIX file descriptor. The file descriptor will be automatically |
| 79 | * closed when the stdio_filebuf is closed/destroyed. |
| 80 | */ |
| 81 | stdio_filebuf(int __fd, std::ios_base::openmode __mode, |
| 82 | size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ)); |
| 83 | |
| 84 | /** |
| 85 | * @param __f An open @c FILE*. |
| 86 | * @param __mode Same meaning as in a standard filebuf. |
| 87 | * @param __size Optimal or preferred size of internal buffer, |
| 88 | * in chars. Defaults to system's @c BUFSIZ. |
| 89 | * |
| 90 | * This constructor associates a file stream buffer with an open |
| 91 | * C @c FILE*. The @c FILE* will not be automatically closed when the |
| 92 | * stdio_filebuf is closed/destroyed. |
| 93 | */ |
| 94 | stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, |
| 95 | size_t __size = static_cast<size_t>(_GLIBCXX_BUFSIZ)); |
| 96 | |
| 97 | /** |
| 98 | * Closes the external data stream if the file descriptor constructor |
| 99 | * was used. |
| 100 | */ |
| 101 | virtual |
| 102 | ~stdio_filebuf(); |
| 103 | |
| 104 | #if __cplusplus >= 201103L |
| 105 | stdio_filebuf(stdio_filebuf&&) = default; |
| 106 | stdio_filebuf& operator=(stdio_filebuf&&) = default; |
| 107 | |
| 108 | void |
| 109 | swap(stdio_filebuf& __fb) |
| 110 | { std::basic_filebuf<_CharT, _Traits>::swap(__fb); } |
| 111 | #endif |
| 112 | |
| 113 | /** |
| 114 | * @return The underlying file descriptor. |
| 115 | * |
| 116 | * Once associated with an external data stream, this function can be |
| 117 | * used to access the underlying POSIX file descriptor. Note that |
| 118 | * there is no way for the library to track what you do with the |
| 119 | * descriptor, so be careful. |
| 120 | */ |
| 121 | int |
| 122 | fd() { return this->_M_file.fd(); } |
| 123 | |
| 124 | /** |
| 125 | * @return The underlying FILE*. |
| 126 | * |
| 127 | * This function can be used to access the underlying "C" file pointer. |
| 128 | * Note that there is no way for the library to track what you do |
| 129 | * with the file, so be careful. |
| 130 | */ |
| 131 | std::__c_file* |
| 132 | file() { return this->_M_file.file(); } |
| 133 | }; |
| 134 | |
| 135 | template<typename _CharT, typename _Traits> |
| 136 | stdio_filebuf<_CharT, _Traits>::~stdio_filebuf() |
| 137 | { } |
| 138 | |
| 139 | template<typename _CharT, typename _Traits> |
| 140 | stdio_filebuf<_CharT, _Traits>:: |
| 141 | stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size) |
| 142 | { |
| 143 | this->_M_file.sys_open(__fd, __mode); |
| 144 | if (this->is_open()) |
| 145 | { |
| 146 | this->_M_mode = __mode; |
| 147 | this->_M_buf_size = __size; |
| 148 | this->_M_allocate_internal_buffer(); |
| 149 | this->_M_reading = false; |
| 150 | this->_M_writing = false; |
| 151 | this->_M_set_buffer(-1); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | template<typename _CharT, typename _Traits> |
| 156 | stdio_filebuf<_CharT, _Traits>:: |
| 157 | stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode, |
| 158 | size_t __size) |
| 159 | { |
| 160 | this->_M_file.sys_open(__f, __mode); |
| 161 | if (this->is_open()) |
| 162 | { |
| 163 | this->_M_mode = __mode; |
| 164 | this->_M_buf_size = __size; |
| 165 | this->_M_allocate_internal_buffer(); |
| 166 | this->_M_reading = false; |
| 167 | this->_M_writing = false; |
| 168 | this->_M_set_buffer(-1); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | _GLIBCXX_END_NAMESPACE_VERSION |
| 173 | } // namespace |
| 174 | |
| 175 | #endif |