Silicium
read_file.hpp
Go to the documentation of this file.
1 #ifndef SILICIUM_READ_FILE_HPP
2 #define SILICIUM_READ_FILE_HPP
3 
4 #include <silicium/error_or.hpp>
7 
8 namespace Si
9 {
10  inline error_or<std::size_t> read(native_file_descriptor file, mutable_memory_range destination)
11  {
12 #ifdef _WIN32
13  DWORD read_bytes = 0;
14  DWORD const reading = static_cast<DWORD>(std::min<size_t>(destination.size(), std::numeric_limits<DWORD>::max()));
15  if (!ReadFile(file, destination.begin(), reading, &read_bytes, nullptr))
16  {
17  DWORD error = GetLastError();
18  if (error == ERROR_BROKEN_PIPE)
19  {
20  //end of pipe
21  return static_cast<std::size_t>(0);
22  }
23  return boost::system::error_code(error, boost::system::system_category());
24  }
25 #else
26  ssize_t const read_bytes = ::read(file, destination.begin(), destination.size());
27  if (read_bytes < 0)
28  {
29  return get_last_error();
30  }
31 #endif
32  if (read_bytes == 0)
33  {
34  //end of file
35  return static_cast<std::size_t>(0);
36  }
37  return static_cast<std::size_t>(read_bytes);
38  }
39 }
40 
41 #endif
error_or< std::size_t > read(native_file_descriptor file, mutable_memory_range destination)
Definition: read_file.hpp:10
BOOST_CONSTEXPR Iterator const & begin() const BOOST_NOEXCEPT
Definition: iterator_range.hpp:61
Definition: absolute_path.hpp:19
Definition: iterator_range.hpp:26
Definition: error_or.hpp:48
SILICIUM_USE_RESULT boost::system::error_code get_last_error()
Definition: get_last_error.hpp:16
BOOST_CONSTEXPR difference_type size() const BOOST_NOEXCEPT
Definition: iterator_range.hpp:78