Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2003-2007 Jonathan Turkanis
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
7
|
Chris@16
|
8 // Inspired by fdstream.hpp, (C) Copyright Nicolai M. Josuttis 2001,
|
Chris@16
|
9 // available at http://www.josuttis.com/cppcode/fdstream.html.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_IOSTREAMS_FILE_DESCRIPTOR_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_IOSTREAMS_FILE_DESCRIPTOR_HPP_INCLUDED
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif
|
Chris@16
|
17
|
Chris@16
|
18 #include <string>
|
Chris@16
|
19 #include <boost/cstdint.hpp> // intmax_t.
|
Chris@16
|
20 #include <boost/iostreams/categories.hpp> // tags.
|
Chris@16
|
21 #include <boost/iostreams/detail/config/auto_link.hpp>
|
Chris@16
|
22 #include <boost/iostreams/detail/config/dyn_link.hpp>
|
Chris@16
|
23 #include <boost/iostreams/detail/config/windows_posix.hpp>
|
Chris@16
|
24 #include <boost/iostreams/detail/file_handle.hpp>
|
Chris@16
|
25 #include <boost/iostreams/detail/ios.hpp> // openmode, seekdir, int types.
|
Chris@16
|
26 #include <boost/iostreams/detail/path.hpp>
|
Chris@16
|
27 #include <boost/iostreams/positioning.hpp>
|
Chris@16
|
28 #include <boost/shared_ptr.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 // Must come last.
|
Chris@16
|
31 #include <boost/config/abi_prefix.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost { namespace iostreams {
|
Chris@16
|
34
|
Chris@16
|
35 // Forward declarations
|
Chris@16
|
36 class file_descriptor_source;
|
Chris@16
|
37 class file_descriptor_sink;
|
Chris@16
|
38 namespace detail { struct file_descriptor_impl; }
|
Chris@16
|
39
|
Chris@16
|
40 enum file_descriptor_flags
|
Chris@16
|
41 {
|
Chris@16
|
42 never_close_handle = 0,
|
Chris@16
|
43 close_handle = 3
|
Chris@16
|
44 };
|
Chris@16
|
45
|
Chris@16
|
46 class BOOST_IOSTREAMS_DECL file_descriptor {
|
Chris@16
|
47 public:
|
Chris@16
|
48 friend class file_descriptor_source;
|
Chris@16
|
49 friend class file_descriptor_sink;
|
Chris@16
|
50 typedef detail::file_handle handle_type;
|
Chris@16
|
51 typedef char char_type;
|
Chris@16
|
52 struct category
|
Chris@16
|
53 : seekable_device_tag,
|
Chris@16
|
54 closable_tag
|
Chris@16
|
55 { };
|
Chris@16
|
56
|
Chris@16
|
57 // Default constructor
|
Chris@16
|
58 file_descriptor();
|
Chris@16
|
59
|
Chris@16
|
60 // Constructors taking file desciptors
|
Chris@16
|
61 file_descriptor(handle_type fd, file_descriptor_flags);
|
Chris@16
|
62 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
63 file_descriptor(int fd, file_descriptor_flags);
|
Chris@16
|
64 #endif
|
Chris@16
|
65
|
Chris@16
|
66 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
67 // Constructors taking file desciptors
|
Chris@16
|
68 explicit file_descriptor(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
69 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
70 explicit file_descriptor(int fd, bool close_on_exit = false);
|
Chris@16
|
71 #endif
|
Chris@16
|
72 #endif
|
Chris@16
|
73
|
Chris@16
|
74 // Constructor taking a std:: string
|
Chris@16
|
75 explicit file_descriptor( const std::string& path,
|
Chris@16
|
76 BOOST_IOS::openmode mode =
|
Chris@16
|
77 BOOST_IOS::in | BOOST_IOS::out );
|
Chris@16
|
78
|
Chris@16
|
79 // Constructor taking a C-style string
|
Chris@16
|
80 explicit file_descriptor( const char* path,
|
Chris@16
|
81 BOOST_IOS::openmode mode =
|
Chris@16
|
82 BOOST_IOS::in | BOOST_IOS::out );
|
Chris@16
|
83
|
Chris@16
|
84 // Constructor taking a Boost.Filesystem path
|
Chris@16
|
85 template<typename Path>
|
Chris@16
|
86 explicit file_descriptor( const Path& path,
|
Chris@16
|
87 BOOST_IOS::openmode mode =
|
Chris@16
|
88 BOOST_IOS::in | BOOST_IOS::out )
|
Chris@16
|
89 {
|
Chris@16
|
90 init();
|
Chris@16
|
91 open(detail::path(path), mode);
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 // Copy constructor
|
Chris@16
|
95 file_descriptor(const file_descriptor& other);
|
Chris@16
|
96
|
Chris@16
|
97 // open overloads taking file descriptors
|
Chris@16
|
98 void open(handle_type fd, file_descriptor_flags);
|
Chris@16
|
99 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
100 void open(int fd, file_descriptor_flags);
|
Chris@16
|
101 #endif
|
Chris@16
|
102
|
Chris@16
|
103 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
104 // open overloads taking file descriptors
|
Chris@16
|
105 void open(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
106 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
107 void open(int fd, bool close_on_exit = false);
|
Chris@16
|
108 #endif
|
Chris@16
|
109 #endif
|
Chris@16
|
110
|
Chris@16
|
111 // open overload taking a std::string
|
Chris@16
|
112 void open( const std::string& path,
|
Chris@16
|
113 BOOST_IOS::openmode mode =
|
Chris@16
|
114 BOOST_IOS::in | BOOST_IOS::out );
|
Chris@16
|
115
|
Chris@16
|
116 // open overload taking C-style string
|
Chris@16
|
117 void open( const char* path,
|
Chris@16
|
118 BOOST_IOS::openmode mode =
|
Chris@16
|
119 BOOST_IOS::in | BOOST_IOS::out );
|
Chris@16
|
120
|
Chris@16
|
121 // open overload taking a Boost.Filesystem path
|
Chris@16
|
122 template<typename Path>
|
Chris@16
|
123 void open( const Path& path,
|
Chris@16
|
124 BOOST_IOS::openmode mode =
|
Chris@16
|
125 BOOST_IOS::in | BOOST_IOS::out )
|
Chris@16
|
126 { open(detail::path(path), mode); }
|
Chris@16
|
127
|
Chris@16
|
128 bool is_open() const;
|
Chris@16
|
129 void close();
|
Chris@16
|
130 std::streamsize read(char_type* s, std::streamsize n);
|
Chris@16
|
131 std::streamsize write(const char_type* s, std::streamsize n);
|
Chris@16
|
132 std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
|
Chris@16
|
133 handle_type handle() const;
|
Chris@16
|
134 private:
|
Chris@16
|
135 void init();
|
Chris@16
|
136
|
Chris@16
|
137 // open overload taking a detail::path
|
Chris@16
|
138 void open( const detail::path& path,
|
Chris@16
|
139 BOOST_IOS::openmode,
|
Chris@16
|
140 BOOST_IOS::openmode = BOOST_IOS::openmode(0) );
|
Chris@16
|
141
|
Chris@16
|
142 typedef detail::file_descriptor_impl impl_type;
|
Chris@16
|
143 shared_ptr<impl_type> pimpl_;
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146 class BOOST_IOSTREAMS_DECL file_descriptor_source : private file_descriptor {
|
Chris@16
|
147 public:
|
Chris@16
|
148 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
149 typedef void* handle_type; // A.k.a HANDLE
|
Chris@16
|
150 #else
|
Chris@16
|
151 typedef int handle_type;
|
Chris@16
|
152 #endif
|
Chris@16
|
153 typedef char char_type;
|
Chris@16
|
154 struct category
|
Chris@16
|
155 : input_seekable,
|
Chris@16
|
156 device_tag,
|
Chris@16
|
157 closable_tag
|
Chris@16
|
158 { };
|
Chris@16
|
159 using file_descriptor::is_open;
|
Chris@16
|
160 using file_descriptor::close;
|
Chris@16
|
161 using file_descriptor::read;
|
Chris@16
|
162 using file_descriptor::seek;
|
Chris@16
|
163 using file_descriptor::handle;
|
Chris@16
|
164
|
Chris@16
|
165 // Default constructor
|
Chris@16
|
166 file_descriptor_source() { }
|
Chris@16
|
167
|
Chris@16
|
168 // Constructors taking file desciptors
|
Chris@16
|
169 explicit file_descriptor_source(handle_type fd, file_descriptor_flags);
|
Chris@16
|
170 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
171 explicit file_descriptor_source(int fd, file_descriptor_flags);
|
Chris@16
|
172 #endif
|
Chris@16
|
173
|
Chris@16
|
174 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
175 // Constructors taking file desciptors
|
Chris@16
|
176 explicit file_descriptor_source(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
177 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
178 explicit file_descriptor_source(int fd, bool close_on_exit = false);
|
Chris@16
|
179 #endif
|
Chris@16
|
180 #endif
|
Chris@16
|
181
|
Chris@16
|
182 // Constructor taking a std:: string
|
Chris@16
|
183 explicit file_descriptor_source( const std::string& path,
|
Chris@16
|
184 BOOST_IOS::openmode mode = BOOST_IOS::in );
|
Chris@16
|
185
|
Chris@16
|
186 // Constructor taking a C-style string
|
Chris@16
|
187 explicit file_descriptor_source( const char* path,
|
Chris@16
|
188 BOOST_IOS::openmode mode = BOOST_IOS::in );
|
Chris@16
|
189
|
Chris@16
|
190 // Constructor taking a Boost.Filesystem path
|
Chris@16
|
191 template<typename Path>
|
Chris@16
|
192 explicit file_descriptor_source( const Path& path,
|
Chris@16
|
193 BOOST_IOS::openmode mode = BOOST_IOS::in )
|
Chris@16
|
194 { open(detail::path(path), mode); }
|
Chris@16
|
195
|
Chris@16
|
196 // Copy constructor
|
Chris@16
|
197 file_descriptor_source(const file_descriptor_source& other);
|
Chris@16
|
198
|
Chris@16
|
199 // Constructors taking file desciptors
|
Chris@16
|
200 void open(handle_type fd, file_descriptor_flags);
|
Chris@16
|
201 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
202 void open(int fd, file_descriptor_flags);
|
Chris@16
|
203 #endif
|
Chris@16
|
204
|
Chris@16
|
205 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
206 // open overloads taking file descriptors
|
Chris@16
|
207 void open(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
208 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
209 void open(int fd, bool close_on_exit = false);
|
Chris@16
|
210 #endif
|
Chris@16
|
211 #endif
|
Chris@16
|
212
|
Chris@16
|
213 // open overload taking a std::string
|
Chris@16
|
214 void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in);
|
Chris@16
|
215
|
Chris@16
|
216 // open overload taking C-style string
|
Chris@16
|
217 void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in);
|
Chris@16
|
218
|
Chris@16
|
219 // open overload taking a Boost.Filesystem path
|
Chris@16
|
220 template<typename Path>
|
Chris@16
|
221 void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::in);
|
Chris@16
|
222 private:
|
Chris@16
|
223
|
Chris@16
|
224 // open overload taking a detail::path
|
Chris@16
|
225 void open(const detail::path& path, BOOST_IOS::openmode);
|
Chris@16
|
226 };
|
Chris@16
|
227
|
Chris@16
|
228 class BOOST_IOSTREAMS_DECL file_descriptor_sink : private file_descriptor {
|
Chris@16
|
229 public:
|
Chris@16
|
230 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
231 typedef void* handle_type; // A.k.a HANDLE
|
Chris@16
|
232 #else
|
Chris@16
|
233 typedef int handle_type;
|
Chris@16
|
234 #endif
|
Chris@16
|
235 typedef char char_type;
|
Chris@16
|
236 struct category
|
Chris@16
|
237 : output_seekable,
|
Chris@16
|
238 device_tag,
|
Chris@16
|
239 closable_tag
|
Chris@16
|
240 { };
|
Chris@16
|
241 using file_descriptor::is_open;
|
Chris@16
|
242 using file_descriptor::close;
|
Chris@16
|
243 using file_descriptor::write;
|
Chris@16
|
244 using file_descriptor::seek;
|
Chris@16
|
245 using file_descriptor::handle;
|
Chris@16
|
246
|
Chris@16
|
247 // Default constructor
|
Chris@16
|
248 file_descriptor_sink() { }
|
Chris@16
|
249
|
Chris@16
|
250 // Constructors taking file desciptors
|
Chris@16
|
251 file_descriptor_sink(handle_type fd, file_descriptor_flags);
|
Chris@16
|
252 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
253 file_descriptor_sink(int fd, file_descriptor_flags);
|
Chris@16
|
254 #endif
|
Chris@16
|
255
|
Chris@16
|
256 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
257 // Constructors taking file desciptors
|
Chris@16
|
258 explicit file_descriptor_sink(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
259 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
260 explicit file_descriptor_sink(int fd, bool close_on_exit = false);
|
Chris@16
|
261 #endif
|
Chris@16
|
262 #endif
|
Chris@16
|
263
|
Chris@16
|
264 // Constructor taking a std:: string
|
Chris@16
|
265 explicit file_descriptor_sink( const std::string& path,
|
Chris@16
|
266 BOOST_IOS::openmode mode = BOOST_IOS::out );
|
Chris@16
|
267
|
Chris@16
|
268 // Constructor taking a C-style string
|
Chris@16
|
269 explicit file_descriptor_sink( const char* path,
|
Chris@16
|
270 BOOST_IOS::openmode mode = BOOST_IOS::out );
|
Chris@16
|
271
|
Chris@16
|
272 // Constructor taking a Boost.Filesystem path
|
Chris@16
|
273 template<typename Path>
|
Chris@16
|
274 explicit file_descriptor_sink( const Path& path,
|
Chris@16
|
275 BOOST_IOS::openmode mode = BOOST_IOS::out )
|
Chris@16
|
276 { open(detail::path(path), mode); }
|
Chris@16
|
277
|
Chris@16
|
278 // Copy constructor
|
Chris@16
|
279 file_descriptor_sink(const file_descriptor_sink& other);
|
Chris@16
|
280
|
Chris@16
|
281 // open overloads taking file descriptors
|
Chris@16
|
282 void open(handle_type fd, file_descriptor_flags);
|
Chris@16
|
283 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
284 void open(int fd, file_descriptor_flags);
|
Chris@16
|
285 #endif
|
Chris@16
|
286
|
Chris@16
|
287 #if defined(BOOST_IOSTREAMS_USE_DEPRECATED)
|
Chris@16
|
288 // open overloads taking file descriptors
|
Chris@16
|
289 void open(handle_type fd, bool close_on_exit = false);
|
Chris@16
|
290 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
291 void open(int fd, bool close_on_exit = false);
|
Chris@16
|
292 #endif
|
Chris@16
|
293 #endif
|
Chris@16
|
294
|
Chris@16
|
295 // open overload taking a std::string
|
Chris@16
|
296 void open( const std::string& path,
|
Chris@16
|
297 BOOST_IOS::openmode mode = BOOST_IOS::out );
|
Chris@16
|
298
|
Chris@16
|
299 // open overload taking C-style string
|
Chris@16
|
300 void open( const char* path,
|
Chris@16
|
301 BOOST_IOS::openmode mode = BOOST_IOS::out );
|
Chris@16
|
302
|
Chris@16
|
303 // open overload taking a Boost.Filesystem path
|
Chris@16
|
304 template<typename Path>
|
Chris@16
|
305 void open( const Path& path,
|
Chris@16
|
306 BOOST_IOS::openmode mode = BOOST_IOS::out )
|
Chris@16
|
307 { open(detail::path(path), mode); }
|
Chris@16
|
308 private:
|
Chris@16
|
309
|
Chris@16
|
310 // open overload taking a detail::path
|
Chris@16
|
311 void open(const detail::path& path, BOOST_IOS::openmode);
|
Chris@16
|
312 };
|
Chris@16
|
313
|
Chris@16
|
314 } } // End namespaces iostreams, boost.
|
Chris@16
|
315
|
Chris@16
|
316 #include <boost/config/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
|
Chris@16
|
317
|
Chris@16
|
318 #endif // #ifndef BOOST_IOSTREAMS_FILE_DESCRIPTOR_HPP_INCLUDED
|