Libosmium  2.2.0
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 <cstdio>
38 #include <string>
39 #include <system_error>
40 
41 #ifdef _WIN32
42 # include <io.h>
43 # include <windows.h>
44 #endif
45 
46 #include <osmium/util/file.hpp>
47 
48 namespace osmium {
49 
50  namespace util {
51 
56  class DataFile {
57 
58  FILE* m_file;
59 
60  public:
61 
68  m_file(::tmpfile()) {
69  if (!m_file) {
70  throw std::system_error(errno, std::system_category(), "tmpfile failed");
71  }
72  }
73 
80  explicit DataFile(size_t size) :
81  DataFile() {
82  grow(size);
83  }
84 
92  DataFile(const char* filename, bool writable) :
93  m_file(::fopen(filename, writable ? "wb+" : "rb" )) {
94  if (!m_file) {
95  throw std::system_error(errno, std::system_category(), "fopen failed");
96  }
97  }
98 
106  DataFile(const std::string& filename, bool writable) :
107  DataFile(filename.c_str(), writable) {
108  }
109 
114  operator bool() const noexcept {
115  return m_file != nullptr;
116  }
117 
125  void close() {
126  if (m_file) {
127  if (::fclose(m_file) != 0) {
128  throw std::system_error(errno, std::system_category(), "fclose failed");
129  }
130  m_file = nullptr;
131  }
132  }
133 
134  ~DataFile() noexcept {
135  try {
136  close();
137  } catch (std::system_error&) {
138  // ignore
139  }
140  }
141 
148  int fd() const {
149  if (!m_file) {
150  throw std::runtime_error("no open file");
151  }
152 
153  int fd = ::fileno(m_file);
154 
155  if (fd == -1) {
156  throw std::system_error(errno, std::system_category(), "fileno failed");
157  }
158 
159  return fd;
160  }
161 
167  size_t size() const {
168  return osmium::util::file_size(fd());
169  }
170 
179  void grow(size_t new_size) const {
180  if (size() < new_size) {
181  osmium::util::resize_file(fd(), new_size);
182  }
183  }
184 
185  }; // class DataFile
186 
187  } // namespace util
188 
189 } // namespace osmium
190 
191 
192 #endif // OSMIUM_UTIL_DATA_FILE_HPP
size_t file_size(int fd)
Definition: file.hpp:66
DataFile(const std::string &filename, bool writable)
Definition: data_file.hpp:106
void grow(size_t new_size) const
Definition: data_file.hpp:179
size_t size() const
Definition: data_file.hpp:167
DataFile()
Definition: data_file.hpp:67
DataFile(const char *filename, bool writable)
Definition: data_file.hpp:92
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
int fd() const
Definition: data_file.hpp:148
~DataFile() noexcept
Definition: data_file.hpp:134
Definition: data_file.hpp:56
void close()
Definition: data_file.hpp:125
void resize_file(int fd, size_t new_size)
Definition: file.hpp:93
FILE * m_file
Definition: data_file.hpp:58
DataFile(size_t size)
Definition: data_file.hpp:80