Libosmium  2.13.1
Fast and flexible C++ library for working with OpenStreetMap data
file.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_FILE_HPP
2 #define OSMIUM_UTIL_FILE_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <cerrno>
37 #include <cstddef>
38 #include <cstdio>
39 #include <string>
40 #include <system_error>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 
44 #ifdef _WIN32
45 # ifndef WIN32_LEAN_AND_MEAN
46 # define WIN32_LEAN_AND_MEAN // Prevent winsock.h inclusion; avoid winsock2.h conflict
47 # endif
48 # include <io.h>
49 # include <windows.h>
50 # include <crtdbg.h>
51 #endif
52 
53 #ifndef _MSC_VER
54 # include <unistd.h>
55 #endif
56 
57 #include <osmium/util/cast.hpp>
58 
59 namespace osmium {
60 
61  namespace util {
62 
63 #ifdef _MSC_VER
64  namespace detail {
65 
66  // Disable parameter validation on Windows and reenable it
67  // automatically when scope closes.
68  // https://docs.microsoft.com/en-us/cpp/c-runtime-library/parameter-validation
69  class disable_invalid_parameter_handler {
70 
71  static void invalid_parameter_handler(
72  const wchar_t* expression,
73  const wchar_t* function,
74  const wchar_t* file,
75  unsigned int line,
76  uintptr_t pReserved
77  ) {
78  // do nothing
79  }
80 
81  _invalid_parameter_handler old_handler;
82  int old_report_mode;
83 
84  public:
85 
86  disable_invalid_parameter_handler() :
87  old_handler(_set_thread_local_invalid_parameter_handler(invalid_parameter_handler)),
88  old_report_mode(_CrtSetReportMode(_CRT_ASSERT, 0)) {
89  }
90 
91  ~disable_invalid_parameter_handler() {
92  _CrtSetReportMode(_CRT_ASSERT, old_report_mode);
93  _set_thread_local_invalid_parameter_handler(old_handler);
94  }
95 
96  }; // class disable_invalid_parameter_handler
97 
98  }
99 #endif
100 
109  inline std::size_t file_size(int fd) {
110 #ifdef _MSC_VER
111  // Windows implementation
112  detail::disable_invalid_parameter_handler diph;
113  // https://msdn.microsoft.com/en-us/library/dfbc2kec.aspx
114  const auto size = ::_filelengthi64(fd);
115  if (size < 0) {
116  throw std::system_error{errno, std::system_category(), "Could not get file size"};
117  }
118  return static_cast<std::size_t>(size);
119 #else
120  // Unix implementation
121  struct stat s;
122  if (::fstat(fd, &s) != 0) {
123  throw std::system_error{errno, std::system_category(), "Could not get file size"};
124  }
125  return static_cast<std::size_t>(s.st_size);
126 #endif
127  }
128 
138  inline std::size_t file_size(const char* name) {
139 #ifdef _MSC_VER
140  // Windows implementation
141  // https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
142  struct _stat64 s;
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 + "'"};
145  }
146 #else
147  // Unix implementation
148  struct stat s;
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 + "'"};
151  }
152 #endif
153  return static_cast<std::size_t>(s.st_size);
154  }
155 
164  inline std::size_t file_size(const std::string& name) {
165  return file_size(name.c_str());
166  }
167 
176  inline void resize_file(int fd, std::size_t new_size) {
177 #ifdef _WIN32
178  detail::disable_invalid_parameter_handler diph;
179  // https://msdn.microsoft.com/en-us/library/whx354w1.aspx
180  if (::_chsize_s(fd, static_cast_with_assert<__int64>(new_size)) != 0) {
181 #else
182  if (::ftruncate(fd, static_cast_with_assert<off_t>(new_size)) != 0) {
183 #endif
184  throw std::system_error{errno, std::system_category(), "Could not resize file"};
185  }
186  }
187 
191  inline std::size_t get_pagesize() {
192 #ifdef _WIN32
193  // Windows implementation
194  SYSTEM_INFO si;
195  GetSystemInfo(&si);
196  return si.dwPageSize;
197 #else
198  // Unix implementation
199  return static_cast<std::size_t>(::sysconf(_SC_PAGESIZE));
200 #endif
201  }
202 
209  inline std::size_t file_offset(int fd) {
210 #ifdef _MSC_VER
211  detail::disable_invalid_parameter_handler diph;
212  // https://msdn.microsoft.com/en-us/library/1yee101t.aspx
213  const auto offset = _lseeki64(fd, 0, SEEK_CUR);
214 #else
215  const auto offset = ::lseek(fd, 0, SEEK_CUR);
216 #endif
217  if (offset == -1) {
218  return 0;
219  }
220  return static_cast<std::size_t>(offset);
221  }
222 
226  inline bool isatty(int fd) {
227 #ifdef _MSC_VER
228  detail::disable_invalid_parameter_handler diph;
229  // https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
230  return _isatty(fd) != 0;
231 #else
232  return ::isatty(fd) != 0;
233 #endif
234  }
235 
236  } // namespace util
237 
238 } // namespace osmium
239 
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
Definition: attr.hpp:333
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