Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
data_file.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_DATA_FILE_HPP
2 #define OSMIUM_UTIL_DATA_FILE_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 <stdexcept>
40 #include <string>
41 #include <system_error>
42 
43 #ifdef _WIN32
44 # include <io.h>
45 # include <windows.h>
46 #endif
47 
48 #include <osmium/util/file.hpp>
49 
50 namespace osmium {
51 
52  namespace util {
53 
58  class DataFile {
59 
60  FILE* m_file;
61 
62  public:
63 
70  m_file(::tmpfile()) {
71  if (!m_file) {
72  throw std::system_error(errno, std::system_category(), "tmpfile failed");
73  }
74  }
75 
82  explicit DataFile(size_t size) :
83  DataFile() {
84  grow(size);
85  }
86 
94  DataFile(const char* filename, bool writable) :
95  m_file(::fopen(filename, writable ? "wb+" : "rb" )) {
96  if (!m_file) {
97  throw std::system_error(errno, std::system_category(), "fopen failed");
98  }
99  }
100 
108  DataFile(const std::string& filename, bool writable) :
109  DataFile(filename.c_str(), writable) {
110  }
111 
116  operator bool() const noexcept {
117  return m_file != nullptr;
118  }
119 
127  void close() {
128  if (m_file) {
129  if (::fclose(m_file) != 0) {
130  throw std::system_error(errno, std::system_category(), "fclose failed");
131  }
132  m_file = nullptr;
133  }
134  }
135 
136  ~DataFile() noexcept {
137  try {
138  close();
139  } catch (std::system_error&) {
140  // ignore
141  }
142  }
143 
150  int fd() const {
151  if (!m_file) {
152  throw std::runtime_error("no open file");
153  }
154 
155  int fd = ::fileno(m_file);
156 
157  if (fd == -1) {
158  throw std::system_error(errno, std::system_category(), "fileno failed");
159  }
160 
161  return fd;
162  }
163 
169  size_t size() const {
170  return osmium::util::file_size(fd());
171  }
172 
181  void grow(size_t new_size) const {
182  if (size() < new_size) {
183  osmium::util::resize_file(fd(), new_size);
184  }
185  }
186 
187  }; // class DataFile
188 
189  } // namespace util
190 
191 } // namespace osmium
192 
193 
194 #endif // OSMIUM_UTIL_DATA_FILE_HPP
size_t file_size(int fd)
Definition: file.hpp:67
DataFile(const std::string &filename, bool writable)
Definition: data_file.hpp:108
void grow(size_t new_size) const
Definition: data_file.hpp:181
size_t size() const
Definition: data_file.hpp:169
DataFile()
Definition: data_file.hpp:69
DataFile(const char *filename, bool writable)
Definition: data_file.hpp:94
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
int fd() const
Definition: data_file.hpp:150
~DataFile() noexcept
Definition: data_file.hpp:136
Definition: data_file.hpp:58
void close()
Definition: data_file.hpp:127
void resize_file(int fd, size_t new_size)
Definition: file.hpp:94
FILE * m_file
Definition: data_file.hpp:60
DataFile(size_t size)
Definition: data_file.hpp:82