Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
file.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_IO_FILE_HPP
2 #define OSMIUM_IO_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 <cstddef>
37 #include <stdexcept>
38 #include <sstream>
39 #include <string>
40 #include <vector>
41 
44 #include <osmium/util/options.hpp>
46 
47 namespace osmium {
48 
52  namespace io {
53 
54  namespace detail {
55 
56  inline std::vector<std::string> split(const std::string& in, const char delim) {
57  std::vector<std::string> result;
58  std::stringstream ss(in);
59  std::string item;
60  while (std::getline(ss, item, delim)) {
61  result.push_back(item);
62  }
63  return result;
64  }
65 
66  } // namespace detail
67 
73  class File : public osmium::util::Options {
74 
75  private:
76 
77  std::string m_filename;
78 
79  const char* m_buffer;
80  size_t m_buffer_size;
81 
82  std::string m_format_string;
83 
85 
87 
89 
90  public:
91 
102  explicit File(const std::string& filename = "", const std::string& format = "") :
103  Options(),
104  m_filename(filename),
105  m_buffer(nullptr),
106  m_buffer_size(0),
107  m_format_string(format) {
108 
109  // stdin/stdout
110  if (filename == "" || filename == "-") {
111  m_filename = "";
113  }
114 
115  // filename is actually a URL
116  std::string protocol = m_filename.substr(0, m_filename.find_first_of(':'));
117  if (protocol == "http" || protocol == "https") {
119  }
120 
121  detect_format_from_suffix(m_filename);
122 
123  if (format != "") {
125  }
126  }
127 
137  explicit File(const char* buffer, size_t size, const std::string& format = "") :
138  Options(),
139  m_filename(),
140  m_buffer(buffer),
141  m_buffer_size(size),
142  m_format_string(format) {
143 
145 
146  if (format != "") {
148  }
149  }
150 
151  File(const File&) = default;
152  File& operator=(const File&) = default;
153 
154  File(File&&) = default;
155  File& operator=(File&&) = default;
156 
157  ~File() = default;
158 
159  const char* buffer() const noexcept {
160  return m_buffer;
161  }
162 
163  size_t buffer_size() const noexcept {
164  return m_buffer_size;
165  }
166 
167  void parse_format(const std::string& format) {
168  std::vector<std::string> options = detail::split(format, ',');
169 
170  // if the first item in the format list doesn't contain
171  // an equals sign, it is a format
172  if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
173  detect_format_from_suffix(options[0]);
174  options.erase(options.begin());
175  }
176 
177  for (auto& option : options) {
178  size_t pos = option.find_first_of('=');
179  if (pos == std::string::npos) {
180  set(option, true);
181  } else {
182  std::string value = option.substr(pos+1);
183  option.erase(pos);
184  set(option, value);
185  }
186  }
187 
188  if (get("history") == "true") {
190  } else if (get("history") == "false") {
192  }
193  }
194 
195  void detect_format_from_suffix(const std::string& name) {
196  std::vector<std::string> suffixes = detail::split(name, '.');
197 
198  if (suffixes.empty()) return;
199 
200  // if the last suffix is one of a known set of compressions,
201  // set that compression
202  if (suffixes.back() == "gz") {
204  suffixes.pop_back();
205  } else if (suffixes.back() == "bz2") {
207  suffixes.pop_back();
208  }
209 
210  if (suffixes.empty()) return;
211 
212  // if the last suffix is one of a known set of formats,
213  // set that format
214  if (suffixes.back() == "pbf") {
216  suffixes.pop_back();
217  } else if (suffixes.back() == "xml") {
219  suffixes.pop_back();
220  } else if (suffixes.back() == "opl") {
222  suffixes.pop_back();
223  }
224 
225  if (suffixes.empty()) return;
226 
227  if (suffixes.back() == "osm") {
229  suffixes.pop_back();
230  } else if (suffixes.back() == "osh") {
233  suffixes.pop_back();
234  } else if (suffixes.back() == "osc") {
237  set("xml_change_format", true);
238  suffixes.pop_back();
239  }
240  }
241 
248  void check() const {
250  std::string msg = "Could not detect file format";
251  if (!m_format_string.empty()) {
252  msg += " from format string '";
253  msg += m_format_string;
254  msg += "'";
255  }
256  if (m_filename.empty()) {
257  msg += " for stdin/stdout";
258  } else {
259  msg += " for filename '";
260  msg += m_filename;
261  msg += "'";
262  }
263  msg += ".";
264  throw std::runtime_error(msg);
265  }
266  }
267 
276  }
277 
286  }
287 
296  }
297 
298  file_format format() const noexcept {
299  return m_file_format;
300  }
301 
304  return *this;
305  }
306 
307  file_compression compression() const noexcept {
308  return m_file_compression;
309  }
310 
313  return *this;
314  }
315 
316  bool has_multiple_object_versions() const noexcept {
318  }
319 
320  File& set_has_multiple_object_versions(bool value) noexcept {
322  return *this;
323  }
324 
325  File& filename(const std::string& filename) {
326  if (filename == "-") {
327  m_filename = "";
328  } else {
329  m_filename = filename;
330  }
331  return *this;
332  }
333 
334  const std::string& filename() const noexcept {
335  return m_filename;
336  }
337 
338  }; // class File
339 
340  } // namespace io
341 
342 } // namespace osmium
343 
344 #endif // OSMIUM_IO_FILE_HPP
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:320
void parse_format(const std::string &format)
Definition: file.hpp:167
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:137
void default_settings_for_url()
Definition: file.hpp:293
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:316
void default_settings_for_stdinout()
Definition: file.hpp:273
File(const std::string &filename="", const std::string &format="")
Definition: file.hpp:102
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:195
size_t buffer_size() const noexcept
Definition: file.hpp:163
bool m_has_multiple_object_versions
Definition: file.hpp:88
Definition: options.hpp:53
Definition: file.hpp:73
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
file_compression m_file_compression
Definition: file.hpp:86
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:311
std::string m_format_string
Definition: file.hpp:82
file_format format() const noexcept
Definition: file.hpp:298
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:302
void default_settings_for_file()
Definition: file.hpp:283
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:159
File & operator=(const File &)=default
file_format m_file_format
Definition: file.hpp:84
size_t size() const noexcept
Definition: options.hpp:121
std::string m_filename
Definition: file.hpp:77
file_compression compression() const noexcept
Definition: file.hpp:307
void check() const
Definition: file.hpp:248
size_t m_buffer_size
Definition: file.hpp:80
File & filename(const std::string &filename)
Definition: file.hpp:325
const std::string & filename() const noexcept
Definition: file.hpp:334
void set(const std::string &key, const std::string &value)
Definition: options.hpp:78
const char * m_buffer
Definition: file.hpp:79