file_stdio.ipp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // Copyright (c) 2015-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
  10. #define BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
  11. #include <boost/beast/core/file_stdio.hpp>
  12. #include <boost/beast/core/detail/win32_unicode_path.hpp>
  13. #include <boost/config/workaround.hpp>
  14. #include <boost/core/exchange.hpp>
  15. #include <limits>
  16. namespace boost {
  17. namespace beast {
  18. file_stdio::
  19. ~file_stdio()
  20. {
  21. if(f_)
  22. fclose(f_);
  23. }
  24. file_stdio::
  25. file_stdio(file_stdio&& other)
  26. : f_(boost::exchange(other.f_, nullptr))
  27. {
  28. }
  29. file_stdio&
  30. file_stdio::
  31. operator=(file_stdio&& other)
  32. {
  33. if(&other == this)
  34. return *this;
  35. if(f_)
  36. fclose(f_);
  37. f_ = other.f_;
  38. other.f_ = nullptr;
  39. return *this;
  40. }
  41. void
  42. file_stdio::
  43. native_handle(std::FILE* f)
  44. {
  45. if(f_)
  46. fclose(f_);
  47. f_ = f;
  48. }
  49. void
  50. file_stdio::
  51. close(error_code& ec)
  52. {
  53. if(f_)
  54. {
  55. int failed = fclose(f_);
  56. f_ = nullptr;
  57. if(failed)
  58. {
  59. ec.assign(errno, generic_category());
  60. return;
  61. }
  62. }
  63. ec = {};
  64. }
  65. void
  66. file_stdio::
  67. open(char const* path, file_mode mode, error_code& ec)
  68. {
  69. if(f_)
  70. {
  71. fclose(f_);
  72. f_ = nullptr;
  73. }
  74. ec = {};
  75. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  76. boost::winapi::WCHAR_ const* s;
  77. detail::win32_unicode_path unicode_path(path, ec);
  78. if (ec)
  79. return;
  80. #else
  81. char const* s;
  82. #endif
  83. switch(mode)
  84. {
  85. default:
  86. case file_mode::read:
  87. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  88. s = L"rb";
  89. #else
  90. s = "rb";
  91. #endif
  92. break;
  93. case file_mode::scan:
  94. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  95. s = L"rbS";
  96. #else
  97. s = "rb";
  98. #endif
  99. break;
  100. case file_mode::write:
  101. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  102. s = L"wb+";
  103. #else
  104. s = "wb+";
  105. #endif
  106. break;
  107. case file_mode::write_new:
  108. {
  109. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  110. # if (defined(BOOST_MSVC) && BOOST_MSVC >= 1910) || (defined(_MSVC_STL_VERSION) && _MSVC_STL_VERSION >= 141)
  111. s = L"wbx";
  112. # else
  113. std::FILE* f0;
  114. auto const ev = ::_wfopen_s(&f0, unicode_path.c_str(), L"rb");
  115. if(! ev)
  116. {
  117. std::fclose(f0);
  118. ec = make_error_code(errc::file_exists);
  119. return;
  120. }
  121. else if(ev !=
  122. errc::no_such_file_or_directory)
  123. {
  124. ec.assign(ev, generic_category());
  125. return;
  126. }
  127. s = L"wb";
  128. # endif
  129. #else
  130. s = "wbx";
  131. #endif
  132. break;
  133. }
  134. case file_mode::write_existing:
  135. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  136. s = L"rb+";
  137. #else
  138. s = "rb+";
  139. #endif
  140. break;
  141. case file_mode::append:
  142. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  143. s = L"ab";
  144. #else
  145. s = "ab";
  146. #endif
  147. break;
  148. case file_mode::append_existing:
  149. {
  150. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  151. std::FILE* f0;
  152. auto const ev =
  153. ::_wfopen_s(&f0, unicode_path.c_str(), L"rb+");
  154. if(ev)
  155. {
  156. ec.assign(ev, generic_category());
  157. return;
  158. }
  159. #else
  160. auto const f0 =
  161. std::fopen(path, "rb+");
  162. if(! f0)
  163. {
  164. ec.assign(errno, generic_category());
  165. return;
  166. }
  167. #endif
  168. std::fclose(f0);
  169. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  170. s = L"ab";
  171. #else
  172. s = "ab";
  173. #endif
  174. break;
  175. }
  176. }
  177. #if defined(BOOST_MSVC) || defined(_MSVC_STL_VERSION)
  178. auto const ev = ::_wfopen_s(&f_, unicode_path.c_str(), s);
  179. if(ev)
  180. {
  181. f_ = nullptr;
  182. ec.assign(ev, generic_category());
  183. return;
  184. }
  185. #else
  186. f_ = std::fopen(path, s);
  187. if(! f_)
  188. {
  189. ec.assign(errno, generic_category());
  190. return;
  191. }
  192. #endif
  193. }
  194. std::uint64_t
  195. file_stdio::
  196. size(error_code& ec) const
  197. {
  198. if(! f_)
  199. {
  200. ec = make_error_code(errc::bad_file_descriptor);
  201. return 0;
  202. }
  203. long pos = std::ftell(f_);
  204. if(pos == -1L)
  205. {
  206. ec.assign(errno, generic_category());
  207. return 0;
  208. }
  209. int result = std::fseek(f_, 0, SEEK_END);
  210. if(result != 0)
  211. {
  212. ec.assign(errno, generic_category());
  213. return 0;
  214. }
  215. long size = std::ftell(f_);
  216. if(size == -1L)
  217. {
  218. ec.assign(errno, generic_category());
  219. std::fseek(f_, pos, SEEK_SET);
  220. return 0;
  221. }
  222. result = std::fseek(f_, pos, SEEK_SET);
  223. if(result != 0)
  224. ec.assign(errno, generic_category());
  225. else
  226. ec = {};
  227. return size;
  228. }
  229. std::uint64_t
  230. file_stdio::
  231. pos(error_code& ec) const
  232. {
  233. if(! f_)
  234. {
  235. ec = make_error_code(errc::bad_file_descriptor);
  236. return 0;
  237. }
  238. long pos = std::ftell(f_);
  239. if(pos == -1L)
  240. {
  241. ec.assign(errno, generic_category());
  242. return 0;
  243. }
  244. ec = {};
  245. return pos;
  246. }
  247. void
  248. file_stdio::
  249. seek(std::uint64_t offset, error_code& ec)
  250. {
  251. if(! f_)
  252. {
  253. ec = make_error_code(errc::bad_file_descriptor);
  254. return;
  255. }
  256. if(offset > static_cast<std::uint64_t>((std::numeric_limits<long>::max)()))
  257. {
  258. ec = make_error_code(errc::invalid_seek);
  259. return;
  260. }
  261. int result = std::fseek(f_,
  262. static_cast<long>(offset), SEEK_SET);
  263. if(result != 0)
  264. ec.assign(errno, generic_category());
  265. else
  266. ec = {};
  267. }
  268. std::size_t
  269. file_stdio::
  270. read(void* buffer, std::size_t n, error_code& ec) const
  271. {
  272. if(! f_)
  273. {
  274. ec = make_error_code(errc::bad_file_descriptor);
  275. return 0;
  276. }
  277. auto nread = std::fread(buffer, 1, n, f_);
  278. if(std::ferror(f_))
  279. {
  280. ec.assign(errno, generic_category());
  281. return 0;
  282. }
  283. return nread;
  284. }
  285. std::size_t
  286. file_stdio::
  287. write(void const* buffer, std::size_t n, error_code& ec)
  288. {
  289. if(! f_)
  290. {
  291. ec = make_error_code(errc::bad_file_descriptor);
  292. return 0;
  293. }
  294. auto nwritten = std::fwrite(buffer, 1, n, f_);
  295. if(std::ferror(f_))
  296. {
  297. ec.assign(errno, generic_category());
  298. return 0;
  299. }
  300. return nwritten;
  301. }
  302. } // beast
  303. } // boost
  304. #endif