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);
143 if (::_stati64(name, &s) != 0) {
144 throw std::system_error{errno, std::system_category(), std::string{
"Could not get file size of file '"} + name +
"'"};
149 if (::stat(name, &s) != 0) {
150 throw std::system_error{errno, std::system_category(), std::string{
"Could not get file size of file '"} + name +
"'"};
153 return static_cast<std::size_t
>(s.st_size);
178 osmium::detail::disable_invalid_parameter_handler diph;
180 if (::_chsize_s(fd, static_cast_with_assert<__int64>(new_size)) != 0) {
182 if (::ftruncate(fd, static_cast_with_assert<off_t>(new_size)) != 0) {
184 throw std::system_error{errno, std::system_category(),
"Could not resize file"};
196 return si.dwPageSize;
199 return static_cast<std::size_t
>(::sysconf(_SC_PAGESIZE));
211 osmium::detail::disable_invalid_parameter_handler diph;
213 const auto offset = _lseeki64(fd, 0, SEEK_CUR);
215 const auto offset = ::lseek(fd, 0, SEEK_CUR);
220 return static_cast<std::size_t
>(offset);
228 osmium::detail::disable_invalid_parameter_handler diph;
230 return _isatty(fd) != 0;
240 #endif // OSMIUM_UTIL_FILE_HPP std::size_t file_size(int fd)
Definition: file.hpp:109
bool isatty(int fd)
Definition: file.hpp:226
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
std::size_t get_pagesize()
Definition: file.hpp:191
std::size_t file_offset(int fd)
Definition: file.hpp:209
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:176