1#ifndef OSMIUM_UTIL_FILE_HPP
2#define OSMIUM_UTIL_FILE_HPP
44#include <system_error>
47# ifndef WIN32_LEAN_AND_MEAN
48# define WIN32_LEAN_AND_MEAN
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);
232 osmium::detail::disable_invalid_parameter_handler diph;
234 return _isatty(fd) != 0;
void resize_file(int fd, std::size_t new_size)
Definition: file.hpp:177
bool isatty(int fd) noexcept
Definition: file.hpp:230
std::size_t file_offset(int fd) noexcept
Definition: file.hpp:211
std::size_t file_size(int fd)
Definition: file.hpp:109
std::size_t get_pagesize() noexcept
Definition: file.hpp:193
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53