1212#define BOOST_HTTP_PROTO_FILE_HPP
1313
1414#include < boost/http_proto/detail/config.hpp>
15+ #include < boost/http_proto/detail/except.hpp>
1516#include < boost/http_proto/detail/file_posix.hpp>
1617#include < boost/http_proto/detail/file_stdio.hpp>
1718#include < boost/http_proto/detail/file_win32.hpp>
@@ -27,19 +28,20 @@ namespace http_proto {
2728 file_source to enable streaming HTTP message
2829 bodies to and from files.
2930
30- @par Example
31+ @par Example 1
3132 @code
32- file f;
33- system::error_code ec;
34-
35- f.open("example.zip", file_mode::write_new, ec);
36- if(ec.failed())
37- throw system::system_error(ec);
33+ file f("example.zip", file_mode::scan);
34+ response.set_payload_size(f.size());
35+ serializer.start<file_source>(response, std::move(f));
36+ @endcode
3837
39- parser.set_body<file_sink>(std::move(f));
38+ @par Example 2
39+ @code
40+ parser.set_body<file_sink>("example.zip", file_mode::write_new);
4041 @endcode
4142
4243 @see
44+ @ref file_mode,
4345 @ref file_sink,
4446 @ref file_source.
4547*/
@@ -63,9 +65,34 @@ class file
6365 using native_handle_type = impl_type::native_handle_type;
6466
6567 /* * Constructor.
68+
69+ There is no open file initially.
6670 */
6771 file () = default ;
6872
73+ /* * Constructor.
74+
75+ Open a file at the given path with the specified mode.
76+
77+ @par Exception Safety
78+ Exception thrown if operation fails.
79+
80+ @throw system_error
81+ Operation fails.
82+
83+ @param path The UTF-8 encoded path to the file.
84+
85+ @param mode The file mode to use.
86+
87+ @see
88+ @ref file_mode,
89+ @ref open.
90+ */
91+ file (char const * path, file_mode mode)
92+ {
93+ open (path, mode);
94+ }
95+
6996 /* * Constructor.
7097
7198 The moved-from object behaves as if default-constructed.
@@ -80,6 +107,12 @@ class file
80107 operator =(
81108 file&& other) noexcept = default ;
82109
110+ /* * Destructor
111+
112+ If the file is open it is first closed.
113+ */
114+ ~file () = default ;
115+
83116 /* * Returns the native handle associated with the file.
84117 */
85118 native_handle_type
@@ -110,6 +143,9 @@ class file
110143
111144 /* * Close the file if open.
112145
146+ Note that, The descriptor is closed even if the function
147+ reports an error.
148+
113149 @param ec Set to the error, if any occurred.
114150 */
115151 void
@@ -118,20 +154,67 @@ class file
118154 impl_.close (ec);
119155 }
120156
157+ /* * Close the file if open.
158+
159+ Note that, The descriptor is closed even if the function
160+ reports an error.
161+
162+ @par Exception Safety
163+ Exception thrown if operation fails.
164+
165+ @throw system_error
166+ Operation fails.
167+ */
168+ void
169+ close ()
170+ {
171+ system::error_code ec;
172+ impl_.close (ec);
173+ if (ec.failed ())
174+ detail::throw_system_error (ec);
175+ }
176+
121177 /* * Open a file at the given path with the specified mode.
122178
123179 @param path The UTF-8 encoded path to the file.
124180
125181 @param mode The file mode to use.
126182
127183 @param ec Set to the error, if any occurred.
184+
185+ @see
186+ @ref file_mode.
128187 */
129188 void
130189 open (char const * path, file_mode mode, system::error_code& ec)
131190 {
132191 impl_.open (path, mode, ec);
133192 }
134193
194+ /* * Open a file at the given path with the specified mode.
195+
196+ @param path The UTF-8 encoded path to the file.
197+
198+ @param mode The file mode to use.
199+
200+ @par Exception Safety
201+ Exception thrown if operation fails.
202+
203+ @throw system_error
204+ Operation fails.
205+
206+ @see
207+ @ref file_mode.
208+ */
209+ void
210+ open (char const * path, file_mode mode)
211+ {
212+ system::error_code ec;
213+ impl_.open (path, mode, ec);
214+ if (ec.failed ())
215+ detail::throw_system_error (ec);
216+ }
217+
135218 /* * Return the size of the open file in bytes.
136219
137220 @param ec Set to the error, if any occurred.
@@ -142,6 +225,24 @@ class file
142225 return impl_.size (ec);
143226 }
144227
228+ /* * Return the size of the open file in bytes.
229+
230+ @par Exception Safety
231+ Exception thrown if operation fails.
232+
233+ @throw system_error
234+ Operation fails.
235+ */
236+ std::uint64_t
237+ size () const
238+ {
239+ system::error_code ec;
240+ auto r = impl_.size (ec);
241+ if (ec.failed ())
242+ detail::throw_system_error (ec);
243+ return r;
244+ }
245+
145246 /* * Return the current position in the file, in bytes from the beginning.
146247
147248 @param ec Set to the error, if any occurred.
@@ -152,6 +253,24 @@ class file
152253 return impl_.pos (ec);
153254 }
154255
256+ /* * Return the current position in the file, in bytes from the beginning.
257+
258+ @par Exception Safety
259+ Exception thrown if operation fails.
260+
261+ @throw system_error
262+ Operation fails.
263+ */
264+ std::uint64_t
265+ pos () const
266+ {
267+ system::error_code ec;
268+ auto r = impl_.pos (ec);
269+ if (ec.failed ())
270+ detail::throw_system_error (ec);
271+ return r;
272+ }
273+
155274 /* * Set the current position in the file.
156275
157276 @param offset The byte offset from the beginning of the file.
@@ -164,6 +283,25 @@ class file
164283 impl_.seek (offset, ec);
165284 }
166285
286+ /* * Set the current position in the file.
287+
288+ @par Exception Safety
289+ Exception thrown if operation fails.
290+
291+ @throw system_error
292+ Operation fails.
293+
294+ @param offset The byte offset from the beginning of the file.
295+ */
296+ void
297+ seek (std::uint64_t offset)
298+ {
299+ system::error_code ec;
300+ impl_.seek (offset, ec);
301+ if (ec.failed ())
302+ detail::throw_system_error (ec);
303+ }
304+
167305 /* * Read data from the file.
168306
169307 @return The number of bytes read. Returns
@@ -182,6 +320,31 @@ class file
182320 return impl_.read (buffer, n, ec);
183321 }
184322
323+ /* * Read data from the file.
324+
325+ @par Exception Safety
326+ Exception thrown if operation fails.
327+
328+ @throw system_error
329+ Operation fails.
330+
331+ @return The number of bytes read. Returns
332+ 0 on end-of-file.
333+
334+ @param buffer The buffer to store the read data.
335+
336+ @param n The number of bytes to read.
337+ */
338+ std::size_t
339+ read (void * buffer, std::size_t n)
340+ {
341+ system::error_code ec;
342+ auto r = impl_.read (buffer, n, ec);
343+ if (ec.failed ())
344+ detail::throw_system_error (ec);
345+ return r;
346+ }
347+
185348 /* * Write data to the file.
186349
187350 @return The number of bytes written.
@@ -199,6 +362,30 @@ class file
199362 {
200363 return impl_.write (buffer, n, ec);
201364 }
365+
366+ /* * Write data to the file.
367+
368+ @par Exception Safety
369+ Exception thrown if operation fails.
370+
371+ @throw system_error
372+ Operation fails.
373+
374+ @return The number of bytes written.
375+
376+ @param buffer The buffer containing the data to write.
377+
378+ @param n The number of bytes to write.
379+ */
380+ std::size_t
381+ write (void const * buffer, std::size_t n)
382+ {
383+ system::error_code ec;
384+ auto r = impl_.write (buffer, n, ec);
385+ if (ec.failed ())
386+ detail::throw_system_error (ec);
387+ return r;
388+ }
202389};
203390
204391} // http_proto
0 commit comments