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