annotate libstdc++-v3/include/ext/stdio_filebuf.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // File descriptor layer for filebuf -*- C++ -*-
kono
parents:
diff changeset
2
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
3 // Copyright (C) 2002-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4 //
kono
parents:
diff changeset
5 // This file is part of the GNU ISO C++ Library. This library is free
kono
parents:
diff changeset
6 // software; you can redistribute it and/or modify it under the
kono
parents:
diff changeset
7 // terms of the GNU General Public License as published by the
kono
parents:
diff changeset
8 // Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 // any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 // This library is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 // GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 // Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 // permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 // 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 // a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 // <http://www.gnu.org/licenses/>.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /** @file ext/stdio_filebuf.h
kono
parents:
diff changeset
26 * This file is a GNU extension to the Standard C++ Library.
kono
parents:
diff changeset
27 */
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 #ifndef _STDIO_FILEBUF_H
kono
parents:
diff changeset
30 #define _STDIO_FILEBUF_H 1
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 #pragma GCC system_header
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 #include <fstream>
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 /**
kono
parents:
diff changeset
41 * @brief Provides a layer of compatibility for C/POSIX.
kono
parents:
diff changeset
42 * @ingroup io
kono
parents:
diff changeset
43 *
kono
parents:
diff changeset
44 * This GNU extension provides extensions for working with standard C
kono
parents:
diff changeset
45 * FILE*'s and POSIX file descriptors. It must be instantiated by the
kono
parents:
diff changeset
46 * user with the type of character used in the file stream, e.g.,
kono
parents:
diff changeset
47 * stdio_filebuf<char>.
kono
parents:
diff changeset
48 */
kono
parents:
diff changeset
49 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
kono
parents:
diff changeset
50 class stdio_filebuf : public std::basic_filebuf<_CharT, _Traits>
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 public:
kono
parents:
diff changeset
53 // Types:
kono
parents:
diff changeset
54 typedef _CharT char_type;
kono
parents:
diff changeset
55 typedef _Traits traits_type;
kono
parents:
diff changeset
56 typedef typename traits_type::int_type int_type;
kono
parents:
diff changeset
57 typedef typename traits_type::pos_type pos_type;
kono
parents:
diff changeset
58 typedef typename traits_type::off_type off_type;
kono
parents:
diff changeset
59 typedef std::size_t size_t;
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 public:
kono
parents:
diff changeset
62 /**
kono
parents:
diff changeset
63 * deferred initialization
kono
parents:
diff changeset
64 */
kono
parents:
diff changeset
65 stdio_filebuf() : std::basic_filebuf<_CharT, _Traits>() {}
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 /**
kono
parents:
diff changeset
68 * @param __fd An open file descriptor.
kono
parents:
diff changeset
69 * @param __mode Same meaning as in a standard filebuf.
kono
parents:
diff changeset
70 * @param __size Optimal or preferred size of internal buffer,
kono
parents:
diff changeset
71 * in chars.
kono
parents:
diff changeset
72 *
kono
parents:
diff changeset
73 * This constructor associates a file stream buffer with an open
kono
parents:
diff changeset
74 * POSIX file descriptor. The file descriptor will be automatically
kono
parents:
diff changeset
75 * closed when the stdio_filebuf is closed/destroyed.
kono
parents:
diff changeset
76 */
kono
parents:
diff changeset
77 stdio_filebuf(int __fd, std::ios_base::openmode __mode,
kono
parents:
diff changeset
78 size_t __size = static_cast<size_t>(BUFSIZ));
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 /**
kono
parents:
diff changeset
81 * @param __f An open @c FILE*.
kono
parents:
diff changeset
82 * @param __mode Same meaning as in a standard filebuf.
kono
parents:
diff changeset
83 * @param __size Optimal or preferred size of internal buffer,
kono
parents:
diff changeset
84 * in chars. Defaults to system's @c BUFSIZ.
kono
parents:
diff changeset
85 *
kono
parents:
diff changeset
86 * This constructor associates a file stream buffer with an open
kono
parents:
diff changeset
87 * C @c FILE*. The @c FILE* will not be automatically closed when the
kono
parents:
diff changeset
88 * stdio_filebuf is closed/destroyed.
kono
parents:
diff changeset
89 */
kono
parents:
diff changeset
90 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
kono
parents:
diff changeset
91 size_t __size = static_cast<size_t>(BUFSIZ));
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 /**
kono
parents:
diff changeset
94 * Closes the external data stream if the file descriptor constructor
kono
parents:
diff changeset
95 * was used.
kono
parents:
diff changeset
96 */
kono
parents:
diff changeset
97 virtual
kono
parents:
diff changeset
98 ~stdio_filebuf();
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 #if __cplusplus >= 201103L
kono
parents:
diff changeset
101 stdio_filebuf(stdio_filebuf&&) = default;
kono
parents:
diff changeset
102 stdio_filebuf& operator=(stdio_filebuf&&) = default;
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 void
kono
parents:
diff changeset
105 swap(stdio_filebuf& __fb)
kono
parents:
diff changeset
106 { std::basic_filebuf<_CharT, _Traits>::swap(__fb); }
kono
parents:
diff changeset
107 #endif
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 /**
kono
parents:
diff changeset
110 * @return The underlying file descriptor.
kono
parents:
diff changeset
111 *
kono
parents:
diff changeset
112 * Once associated with an external data stream, this function can be
kono
parents:
diff changeset
113 * used to access the underlying POSIX file descriptor. Note that
kono
parents:
diff changeset
114 * there is no way for the library to track what you do with the
kono
parents:
diff changeset
115 * descriptor, so be careful.
kono
parents:
diff changeset
116 */
kono
parents:
diff changeset
117 int
kono
parents:
diff changeset
118 fd() { return this->_M_file.fd(); }
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 /**
kono
parents:
diff changeset
121 * @return The underlying FILE*.
kono
parents:
diff changeset
122 *
kono
parents:
diff changeset
123 * This function can be used to access the underlying "C" file pointer.
kono
parents:
diff changeset
124 * Note that there is no way for the library to track what you do
kono
parents:
diff changeset
125 * with the file, so be careful.
kono
parents:
diff changeset
126 */
kono
parents:
diff changeset
127 std::__c_file*
kono
parents:
diff changeset
128 file() { return this->_M_file.file(); }
kono
parents:
diff changeset
129 };
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 template<typename _CharT, typename _Traits>
kono
parents:
diff changeset
132 stdio_filebuf<_CharT, _Traits>::~stdio_filebuf()
kono
parents:
diff changeset
133 { }
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 template<typename _CharT, typename _Traits>
kono
parents:
diff changeset
136 stdio_filebuf<_CharT, _Traits>::
kono
parents:
diff changeset
137 stdio_filebuf(int __fd, std::ios_base::openmode __mode, size_t __size)
kono
parents:
diff changeset
138 {
kono
parents:
diff changeset
139 this->_M_file.sys_open(__fd, __mode);
kono
parents:
diff changeset
140 if (this->is_open())
kono
parents:
diff changeset
141 {
kono
parents:
diff changeset
142 this->_M_mode = __mode;
kono
parents:
diff changeset
143 this->_M_buf_size = __size;
kono
parents:
diff changeset
144 this->_M_allocate_internal_buffer();
kono
parents:
diff changeset
145 this->_M_reading = false;
kono
parents:
diff changeset
146 this->_M_writing = false;
kono
parents:
diff changeset
147 this->_M_set_buffer(-1);
kono
parents:
diff changeset
148 }
kono
parents:
diff changeset
149 }
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 template<typename _CharT, typename _Traits>
kono
parents:
diff changeset
152 stdio_filebuf<_CharT, _Traits>::
kono
parents:
diff changeset
153 stdio_filebuf(std::__c_file* __f, std::ios_base::openmode __mode,
kono
parents:
diff changeset
154 size_t __size)
kono
parents:
diff changeset
155 {
kono
parents:
diff changeset
156 this->_M_file.sys_open(__f, __mode);
kono
parents:
diff changeset
157 if (this->is_open())
kono
parents:
diff changeset
158 {
kono
parents:
diff changeset
159 this->_M_mode = __mode;
kono
parents:
diff changeset
160 this->_M_buf_size = __size;
kono
parents:
diff changeset
161 this->_M_allocate_internal_buffer();
kono
parents:
diff changeset
162 this->_M_reading = false;
kono
parents:
diff changeset
163 this->_M_writing = false;
kono
parents:
diff changeset
164 this->_M_set_buffer(-1);
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166 }
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 _GLIBCXX_END_NAMESPACE_VERSION
kono
parents:
diff changeset
169 } // namespace
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 #endif