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_STDIO_HPP
11 #define BOOST_BEAST_CORE_FILE_STDIO_HPP
12 
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/error.hpp>
15 #include <boost/beast/core/file_base.hpp>
16 #include <cstdio>
17 #include <cstdint>
18 
19 namespace boost {
20 namespace beast {
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_BEAST_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_BEAST_DECL
56     file_stdio(file_stdio&& other);
57 
58     /** Assignment
59 
60         The moved-from object behaves as if default constructed.
61     */
62     BOOST_BEAST_DECL
63     file_stdio& operator=(file_stdio&& other);
64 
65     /// Returns the native handle associated with the file.
66     std::FILE*
native_handle() const67     native_handle() const
68     {
69         return f_;
70     }
71 
72     /** Set the native handle associated with the file.
73 
74         If the file is open it is first closed.
75 
76         @param f The native file handle to assign.
77     */
78     BOOST_BEAST_DECL
79     void
80     native_handle(std::FILE* f);
81 
82     /// Returns `true` if the file is open
83     bool
is_open() const84     is_open() const
85     {
86         return f_ != nullptr;
87     }
88 
89     /** Close the file if open
90 
91         @param ec Set to the error, if any occurred.
92     */
93     BOOST_BEAST_DECL
94     void
95     close(error_code& ec);
96 
97     /** Open a file at the given path with the specified mode
98 
99         @param path The utf-8 encoded path to the file
100 
101         @param mode The file mode to use
102 
103         @param ec Set to the error, if any occurred
104     */
105     BOOST_BEAST_DECL
106     void
107     open(char const* path, file_mode mode, error_code& ec);
108 
109     /** Return the size of the open file
110 
111         @param ec Set to the error, if any occurred
112 
113         @return The size in bytes
114     */
115     BOOST_BEAST_DECL
116     std::uint64_t
117     size(error_code& ec) const;
118 
119     /** Return the current position in the open file
120 
121         @param ec Set to the error, if any occurred
122 
123         @return The offset in bytes from the beginning of the file
124     */
125     BOOST_BEAST_DECL
126     std::uint64_t
127     pos(error_code& ec) const;
128 
129     /** Adjust the current position in the open file
130 
131         @param offset The offset in bytes from the beginning of the file
132 
133         @param ec Set to the error, if any occurred
134     */
135     BOOST_BEAST_DECL
136     void
137     seek(std::uint64_t offset, error_code& ec);
138 
139     /** Read from the open file
140 
141         @param buffer The buffer for storing the result of the read
142 
143         @param n The number of bytes to read
144 
145         @param ec Set to the error, if any occurred
146     */
147     BOOST_BEAST_DECL
148     std::size_t
149     read(void* buffer, std::size_t n, error_code& ec) const;
150 
151     /** Write to the open file
152 
153         @param buffer The buffer holding the data to write
154 
155         @param n The number of bytes to write
156 
157         @param ec Set to the error, if any occurred
158     */
159     BOOST_BEAST_DECL
160     std::size_t
161     write(void const* buffer, std::size_t n, error_code& ec);
162 };
163 
164 } // beast
165 } // boost
166 
167 #ifdef BOOST_BEAST_HEADER_ONLY
168 #include <boost/beast/core/impl/file_stdio.ipp>
169 #endif
170 
171 #endif
172