1 #ifndef OSMIUM_UTIL_FILE_HPP
2 #define OSMIUM_UTIL_FILE_HPP
43 #include <sys/types.h>
44 #include <system_error>
47 # ifndef WIN32_LEAN_AND_MEAN
48 # define WIN32_LEAN_AND_MEAN // Prevent winsock.h inclusion; avoid winsock2.h conflict
67 class disable_invalid_parameter_handler {
69 static void invalid_parameter_handler(
70 const wchar_t* expression,
71 const wchar_t*
function,
79 _invalid_parameter_handler old_handler;
84 disable_invalid_parameter_handler() :
85 old_handler(_set_thread_local_invalid_parameter_handler(invalid_parameter_handler)),
86 old_report_mode(_CrtSetReportMode(_CRT_ASSERT, 0)) {
89 ~disable_invalid_parameter_handler() {
90 _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
91 _set_thread_local_invalid_parameter_handler(old_handler);
99 inline namespace util {
112 osmium::detail::disable_invalid_parameter_handler diph;
114 const auto size = ::_filelengthi64(fd);
116 throw std::system_error{errno, std::system_category(),
"Could not get file size"};
118 return static_cast<std::size_t
>(size);
122 if (::fstat(fd, &s) != 0) {
123 throw std::system_error{errno, std::system_category(),
"Could not get file size"};
125 return static_cast<std::size_t
>(s.st_size);
141 osmium::detail::disable_invalid_parameter_handler diph;
144 if (::_stati64(name, &s) != 0) {
145 throw std::system_error{errno, std::system_category(), std::string{
"Could not get file size of file '"} + name +
"'"};
150 if (::stat(name, &s) != 0) {
151 throw std::system_error{errno, std::system_category(), std::string{
"Could not get file size of file '"} + name +
"'"};
154 return static_cast<std::size_t
>(s.st_size);
179 osmium::detail::disable_invalid_parameter_handler diph;
180 assert(new_size <=
static_cast<std::size_t
>(std::numeric_limits<__int64>::max()));
182 if (::_chsize_s(fd,
static_cast<__int64
>(new_size)) != 0) {
184 if (::ftruncate(fd,
static_cast<off_t
>(new_size)) != 0) {
186 throw std::system_error{errno, std::system_category(),
"Could not resize file"};
198 return si.dwPageSize;
201 return static_cast<std::size_t
>(::sysconf(_SC_PAGESIZE));
213 osmium::detail::disable_invalid_parameter_handler diph;
215 const auto offset = _lseeki64(fd, 0, SEEK_CUR);
217 const auto offset = ::lseek(fd, 0, SEEK_CUR);
222 return static_cast<std::size_t
>(offset);
230 osmium::detail::disable_invalid_parameter_handler diph;
232 return _isatty(fd) != 0;
242 #endif // OSMIUM_UTIL_FILE_HPP