file_descriptor.hpp 10 KB

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