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