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;
179 assert(new_size <= static_cast<std::size_t>(std::numeric_limits<__int64>::max()));
181 if (::_chsize_s(fd, static_cast<__int64>(new_size)) != 0) {
183 assert(std::numeric_limits<off_t>::max() >= std::numeric_limits<size_t>::max() || new_size <= std::numeric_limits<off_t>::max());
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 std::size_t file_size(int fd)
Definition: file.hpp:109
bool isatty(int fd)
Definition: file.hpp:228
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
std::size_t get_pagesize()
Definition: file.hpp:193
std::size_t file_offset(int fd)
Definition: file.hpp:211
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:176