Libosmium  2.11.3
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 # include <io.h>
46 # include <windows.h>
47 #endif
48 
49 #ifndef _MSC_VER
50 # include <unistd.h>
51 #endif
52 
53 #include <osmium/util/cast.hpp>
54 
55 namespace osmium {
56 
57  namespace util {
58 
67  inline size_t file_size(int fd) {
68 #ifdef _MSC_VER
69  // Windows implementation
70  // https://msdn.microsoft.com/en-us/library/dfbc2kec.aspx
71  const auto size = ::_filelengthi64(fd);
72  if (size == -1L) {
73  throw std::system_error(errno, std::system_category(), "_filelengthi64 failed");
74  }
75  return size_t(size);
76 #else
77  // Unix implementation
78  struct stat s;
79  if (::fstat(fd, &s) != 0) {
80  throw std::system_error(errno, std::system_category(), "fstat failed");
81  }
82  return size_t(s.st_size);
83 #endif
84  }
85 
94  inline size_t file_size(const char* name) {
95 #ifdef _MSC_VER
96  // Windows implementation
97  // https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
98  struct _stat64 s;
99  if (::_stati64(name, &s) != 0) {
100  throw std::system_error(errno, std::system_category(), "_stati64 failed");
101  }
102 #else
103  // Unix implementation
104  struct stat s;
105  if (::stat(name, &s) != 0) {
106  throw std::system_error(errno, std::system_category(), "stat failed");
107  }
108 #endif
109  return size_t(s.st_size);
110  }
111 
120  inline size_t file_size(const std::string& name) {
121  return file_size(name.c_str());
122  }
123 
132  inline void resize_file(int fd, size_t new_size) {
133 #ifdef _WIN32
134  // https://msdn.microsoft.com/en-us/library/whx354w1.aspx
135  if (::_chsize_s(fd, static_cast_with_assert<__int64>(new_size)) != 0) {
136 #else
137  if (::ftruncate(fd, static_cast_with_assert<off_t>(new_size)) != 0) {
138 #endif
139  throw std::system_error(errno, std::system_category(), "resizing file failed");
140  }
141  }
142 
146  inline size_t get_pagesize() {
147 #ifdef _WIN32
148  // Windows implementation
149  SYSTEM_INFO si;
150  GetSystemInfo(&si);
151  return si.dwPageSize;
152 #else
153  // Unix implementation
154  return size_t(::sysconf(_SC_PAGESIZE));
155 #endif
156  }
157 
164  inline size_t file_offset(int fd) {
165 #ifdef _MSC_VER
166  // https://msdn.microsoft.com/en-us/library/1yee101t.aspx
167  auto offset = _lseeki64(fd, 0, SEEK_CUR);
168 #else
169  auto offset = ::lseek(fd, 0, SEEK_CUR);
170 #endif
171  if (offset == -1) {
172  return 0;
173  }
174  return size_t(offset);
175  }
176 
180  inline bool isatty(int fd) {
181 #ifdef _MSC_VER
182  // https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx
183  return _isatty(fd) != 0;
184 #else
185  return ::isatty(fd) != 0;
186 #endif
187  }
188 
189  } // namespace util
190 
191 } // namespace osmium
192 
193 #endif // OSMIUM_UTIL_FILE_HPP
size_t file_size(int fd)
Definition: file.hpp:67
bool isatty(int fd)
Definition: file.hpp:180
size_t get_pagesize()
Definition: file.hpp:146
size_t file_offset(int fd)
Definition: file.hpp:164
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
void resize_file(int fd, size_t new_size)
Definition: file.hpp:132