2 // Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
11 #define BOOST_BEAST_CORE_IMPL_FILE_STDIO_IPP
28 file_stdio(file_stdio&& other)
37 operator=(file_stdio&& other)
51 native_handle(FILE* f)
65 int failed = fclose(f_);
69 ec.assign(errno, generic_category());
73 ec.assign(0, ec.category());
79 open(char const* path, file_mode mode, error_code& ec)
90 case file_mode::read: s = "rb"; break;
91 case file_mode::scan: s = "rb"; break;
92 case file_mode::write: s = "wb"; break;
93 case file_mode::write_new: s = "wbx"; break;
94 case file_mode::write_existing: s = "wb"; break;
95 case file_mode::append: s = "ab"; break;
96 case file_mode::append_new: s = "abx"; break;
97 case file_mode::append_existing: s = "ab"; break;
100 auto const ev = fopen_s(&f_, path, s);
104 ec.assign(ev, generic_category());
108 f_ = std::fopen(path, s);
111 ec.assign(errno, generic_category());
115 ec.assign(0, ec.category());
121 size(error_code& ec) const
125 ec = make_error_code(errc::invalid_argument);
128 long pos = std::ftell(f_);
131 ec.assign(errno, generic_category());
134 int result = std::fseek(f_, 0, SEEK_END);
137 ec.assign(errno, generic_category());
140 long size = std::ftell(f_);
143 ec.assign(errno, generic_category());
144 std::fseek(f_, pos, SEEK_SET);
147 result = std::fseek(f_, pos, SEEK_SET);
149 ec.assign(errno, generic_category());
151 ec.assign(0, ec.category());
158 pos(error_code& ec) const
162 ec = make_error_code(errc::invalid_argument);
165 long pos = std::ftell(f_);
168 ec.assign(errno, generic_category());
171 ec.assign(0, ec.category());
178 seek(std::uint64_t offset, error_code& ec)
182 ec = make_error_code(errc::invalid_argument);
185 if(offset > (std::numeric_limits<long>::max)())
187 ec = make_error_code(errc::invalid_seek);
190 int result = std::fseek(f_,
191 static_cast<long>(offset), SEEK_SET);
193 ec.assign(errno, generic_category());
195 ec.assign(0, ec.category());
201 read(void* buffer, std::size_t n, error_code& ec) const
205 ec = make_error_code(errc::invalid_argument);
208 auto nread = std::fread(buffer, 1, n, f_);
211 ec.assign(errno, generic_category());
220 write(void const* buffer, std::size_t n, error_code& ec)
224 ec = make_error_code(errc::invalid_argument);
227 auto nwritten = std::fwrite(buffer, 1, n, f_);
230 ec.assign(errno, generic_category());