file_descriptor.hpp 10 KB

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