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