Libosmium  2.12.2
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 #endif
51 
52 #ifndef _MSC_VER
53 # include <unistd.h>
54 #endif
55 
56 #include <osmium/util/cast.hpp>
57 
58 namespace osmium {
59 
60  namespace util {
61 
70  inline size_t file_size(int fd) {
71 #ifdef _MSC_VER
72  // Windows implementation
73  // https://msdn.microsoft.com/en-us/library/dfbc2kec.aspx
74  const auto size = ::_filelengthi64(fd);
75  if (size == -1L) {
76  throw std::system_error{errno, std::system_category(), "Could not get file size"};
77  }
78  return size_t(size);
79 #else
80  // Unix implementation
81  struct stat s;
82  if (::fstat(fd, &s) != 0) {
83  throw std::system_error{errno, std::system_category(), "Could not get file size"};
84  }
85  return size_t(s.st_size);
86 #endif
87  }
88 
97  inline size_t file_size(const char* name) {
98 #ifdef _MSC_VER
99  // Windows implementation
100  // https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
101  struct _stat64 s;
102  if (::_stati64(name, &s) != 0) {
103  throw std::system_error{errno, std::system_category(), std::string{"Could not get file size of file '"} + name + "'"};
104  }
105 #else
106  // Unix implementation
107  struct stat s;
108  if (::stat(name, &s) != 0) {
109  throw std::system_error{errno, std::system_category(), std::string{"Could not get file size of file '"} + name + "'"};
110  }
111 #endif
112  return size_t(s.st_size);
113  }
114 
123  inline size_t file_size(const std::string& name) {
124  return file_size(name.c_str());
125  }
126 
135  inline void resize_file(int fd, size_t new_size) {
136 #ifdef _WIN32
137  // https://msdn.microsoft.com/en-us/library/whx354w1.aspx
138  if (::_chsize_s(fd, static_cast_with_assert<__int64>(new_size)) != 0) {
139 #else
140  if (::ftruncate(fd, static_cast_with_assert<off_t>(new_size)) != 0) {
141 #endif
142  throw std::system_error{errno, std::system_category(), "Could not resize file"};
143  }
144  }
145 
149  inline size_t get_pagesize() {
150 #ifdef _WIN32
151  // Windows implementation
152  SYSTEM_INFO si;
153  GetSystemInfo(&si);
154  return si.dwPageSize;
155 #else
156  // Unix implementation
157  return size_t(::sysconf(_SC_PAGESIZE));
158 #endif
159  }
160 
167  inline size_t file_offset(int fd) {
168 #ifdef _MSC_VER
169  // https://msdn.microsoft.com/en-us/library/1yee101t.aspx
170  auto offset = _lseeki64(fd, 0, SEEK_CUR);
171 #else
172  auto offset = ::lseek(fd, 0, SEEK_CUR);
173 #endif
174  if (offset == -1) {
175  return 0;
176  }
177  return size_t(offset);
178  }
179 
183  inline bool isatty(int fd) {
184 #ifdef _MSC_VER
185  // https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
186  return _isatty(fd) != 0;
187 #else
188  return ::isatty(fd) != 0;
189 #endif
190  }
191 
192  } // namespace util
193 
194 } // namespace osmium
195 
196 #endif // OSMIUM_UTIL_FILE_HPP
size_t file_size(int fd)
Definition: file.hpp:70
bool isatty(int fd)
Definition: file.hpp:183
size_t get_pagesize()
Definition: file.hpp:149
size_t file_offset(int fd)
Definition: file.hpp:167
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
void resize_file(int fd, size_t new_size)
Definition: file.hpp:135