Silicium
write.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_WRITE_HPP
2 #define SILICIUM_WRITE_HPP
3 
4 #include <silicium/error_or.hpp>
8 #include <limits>
9 
10 namespace Si
11 {
13  inline error_or<std::size_t> write(native_file_descriptor file, memory_range data)
14  {
15  std::size_t total_written = 0;
16  do
17  {
18 #ifdef _WIN32
19  DWORD written = 0;
20  assert(std::numeric_limits<decltype(total_written)>::max() >= std::numeric_limits<decltype(written)>::max());
21  DWORD const piece = static_cast<DWORD>(std::min(
22  static_cast<std::size_t>(std::numeric_limits<DWORD>::max()),
23  data.size() - total_written
24  ));
25  if (!WriteFile(file, data.begin() + total_written, piece, &written, nullptr))
26  {
27  return get_last_error();
28  }
29 #else
30  ssize_t const written = ::write(file, data.begin() + total_written, data.size() - total_written);
31  if (written < 0)
32  {
33  return get_last_error();
34  }
35 #endif
36  if (written == 0)
37  {
38  break;
39  }
40  total_written += written;
41  }
42  while (total_written < static_cast<std::size_t>(data.size()));
43  return total_written;
44  }
45 }
46 
47 #endif
BOOST_CONSTEXPR Iterator const & begin() const BOOST_NOEXCEPT
Definition: iterator_range.hpp:61
Definition: absolute_path.hpp:19
Definition: iterator_range.hpp:26
SILICIUM_USE_RESULT error_or< std::size_t > write(native_file_descriptor file, memory_range data)
Definition: write.hpp:13
Definition: error_or.hpp:48
SILICIUM_USE_RESULT boost::system::error_code get_last_error()
Definition: get_last_error.hpp:16
#define SILICIUM_USE_RESULT
Definition: config.hpp:147
BOOST_CONSTEXPR difference_type size() const BOOST_NOEXCEPT
Definition: iterator_range.hpp:78