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