file_win32.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_FILE_WIN32_HPP
  10. #define BOOST_BEAST_CORE_FILE_WIN32_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #if ! defined(BOOST_BEAST_USE_WIN32_FILE)
  13. # ifdef _WIN32
  14. # define BOOST_BEAST_USE_WIN32_FILE 1
  15. # else
  16. # define BOOST_BEAST_USE_WIN32_FILE 0
  17. # endif
  18. #endif
  19. #if BOOST_BEAST_USE_WIN32_FILE
  20. #include <boost/beast/core/error.hpp>
  21. #include <boost/beast/core/file_base.hpp>
  22. #include <boost/winapi/basic_types.hpp>
  23. #include <boost/winapi/handles.hpp>
  24. #include <cstdio>
  25. #include <cstdint>
  26. namespace boost {
  27. namespace beast {
  28. /** An implementation of File for Win32.
  29. This class implements a <em>File</em> using Win32 native interfaces.
  30. */
  31. class file_win32
  32. {
  33. boost::winapi::HANDLE_ h_ =
  34. boost::winapi::INVALID_HANDLE_VALUE_;
  35. public:
  36. /** The type of the underlying file handle.
  37. This is platform-specific.
  38. */
  39. #if BOOST_BEAST_DOXYGEN
  40. using native_handle_type = HANDLE;
  41. #else
  42. using native_handle_type = boost::winapi::HANDLE_;
  43. #endif
  44. /** Destructor
  45. If the file is open it is first closed.
  46. */
  47. BOOST_BEAST_DECL
  48. ~file_win32();
  49. /** Constructor
  50. There is no open file initially.
  51. */
  52. file_win32() = default;
  53. /** Constructor
  54. The moved-from object behaves as if default constructed.
  55. */
  56. BOOST_BEAST_DECL
  57. file_win32(file_win32&& other);
  58. /** Assignment
  59. The moved-from object behaves as if default constructed.
  60. */
  61. BOOST_BEAST_DECL
  62. file_win32& operator=(file_win32&& other);
  63. /// Returns the native handle associated with the file.
  64. native_handle_type
  65. native_handle()
  66. {
  67. return h_;
  68. }
  69. /** Set the native handle associated with the file.
  70. If the file is open it is first closed.
  71. @param h The native file handle to assign.
  72. */
  73. BOOST_BEAST_DECL
  74. void
  75. native_handle(native_handle_type h);
  76. /// Returns `true` if the file is open
  77. bool
  78. is_open() const
  79. {
  80. return h_ != boost::winapi::INVALID_HANDLE_VALUE_;
  81. }
  82. /** Close the file if open
  83. @param ec Set to the error, if any occurred.
  84. */
  85. BOOST_BEAST_DECL
  86. void
  87. close(error_code& ec);
  88. /** Open a file at the given path with the specified mode
  89. @param path The utf-8 encoded path to the file
  90. @param mode The file mode to use
  91. @param ec Set to the error, if any occurred
  92. */
  93. BOOST_BEAST_DECL
  94. void
  95. open(char const* path, file_mode mode, error_code& ec);
  96. /** Return the size of the open file
  97. @param ec Set to the error, if any occurred
  98. @return The size in bytes
  99. */
  100. BOOST_BEAST_DECL
  101. std::uint64_t
  102. size(error_code& ec) const;
  103. /** Return the current position in the open file
  104. @param ec Set to the error, if any occurred
  105. @return The offset in bytes from the beginning of the file
  106. */
  107. BOOST_BEAST_DECL
  108. std::uint64_t
  109. pos(error_code& ec);
  110. /** Adjust the current position in the open file
  111. @param offset The offset in bytes from the beginning of the file
  112. @param ec Set to the error, if any occurred
  113. */
  114. BOOST_BEAST_DECL
  115. void
  116. seek(std::uint64_t offset, error_code& ec);
  117. /** Read from the open file
  118. @param buffer The buffer for storing the result of the read
  119. @param n The number of bytes to read
  120. @param ec Set to the error, if any occurred
  121. */
  122. BOOST_BEAST_DECL
  123. std::size_t
  124. read(void* buffer, std::size_t n, error_code& ec);
  125. /** Write to the open file
  126. @param buffer The buffer holding the data to write
  127. @param n The number of bytes to write
  128. @param ec Set to the error, if any occurred
  129. */
  130. BOOST_BEAST_DECL
  131. std::size_t
  132. write(void const* buffer, std::size_t n, error_code& ec);
  133. };
  134. } // beast
  135. } // boost
  136. #ifdef BOOST_BEAST_HEADER_ONLY
  137. #include <boost/beast/core/impl/file_win32.ipp>
  138. #endif
  139. #endif
  140. #endif