Libosmium  2.16.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 <osmium/io/error.hpp>
39 #include <osmium/util/options.hpp>
40 
41 #include <cstddef>
42 #include <sstream>
43 #include <string>
44 #include <vector>
45 
46 namespace osmium {
47 
51  namespace io {
52 
53  namespace detail {
54 
55  inline std::vector<std::string> split(const std::string& in, const char delim) {
56  std::vector<std::string> result;
57  std::stringstream ss(in);
58  std::string item;
59  while (std::getline(ss, item, delim)) {
60  result.push_back(item);
61  }
62  return result;
63  }
64 
65  } // namespace detail
66 
72  class File : public osmium::Options {
73 
74  private:
75 
76  std::string m_filename{};
77 
78  const char* m_buffer = nullptr;
79  size_t m_buffer_size = 0;
80 
81  std::string m_format_string;
82 
84 
86 
88 
89  public:
90 
103  explicit File(std::string filename = "", std::string format = "") :
104  m_filename(std::move(filename)),
105  m_format_string(std::move(format)) {
106 
107  // stdin/stdout
108  if (m_filename == "-") {
109  m_filename = "";
110  }
111 
112  // if filename is a URL, default to XML format
113  const std::string protocol{m_filename.substr(0, m_filename.find_first_of(':'))};
114  if (protocol == "http" || protocol == "https") {
116  }
117 
118  if (m_format_string.empty()) {
120  } else {
122  }
123  }
124 
134  explicit File(const char* buffer, size_t size, const std::string& format = "") :
135  m_buffer(buffer),
136  m_buffer_size(size),
138  if (!format.empty()) {
140  }
141  }
142 
143  const char* buffer() const noexcept {
144  return m_buffer;
145  }
146 
147  size_t buffer_size() const noexcept {
148  return m_buffer_size;
149  }
150 
151  void parse_format(const std::string& format) {
152  std::vector<std::string> options = detail::split(format, ',');
153 
154  // if the first item in the format list doesn't contain
155  // an equals sign, it is a format
156  if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
157  detect_format_from_suffix(options[0]);
158  options.erase(options.begin());
159  }
160 
161  for (auto& option : options) {
162  const size_t pos = option.find_first_of('=');
163  if (pos == std::string::npos) {
164  set(option, true);
165  } else {
166  std::string value{option.substr(pos + 1)};
167  option.erase(pos);
168  set(option, value);
169  }
170  }
171 
172  if (get("history") == "true") {
174  } else if (get("history") == "false") {
176  }
177  }
178 
179  void detect_format_from_suffix(const std::string& name) {
180  std::vector<std::string> suffixes = detail::split(name, '.');
181 
182  if (suffixes.empty()) {
183  return;
184  }
185 
186  // if the last suffix is one of a known set of compressions,
187  // set that compression
188  if (suffixes.back() == "gz") {
190  suffixes.pop_back();
191  } else if (suffixes.back() == "bz2") {
193  suffixes.pop_back();
194  }
195 
196  if (suffixes.empty()) {
197  return;
198  }
199 
200  // if the last suffix is one of a known set of formats,
201  // set that format
202  if (suffixes.back() == "pbf") {
204  suffixes.pop_back();
205  } else if (suffixes.back() == "xml") {
207  suffixes.pop_back();
208  } else if (suffixes.back() == "opl") {
210  suffixes.pop_back();
211  } else if (suffixes.back() == "json") {
213  suffixes.pop_back();
214  } else if (suffixes.back() == "o5m") {
216  suffixes.pop_back();
217  } else if (suffixes.back() == "o5c") {
220  set("o5c_change_format", true);
221  suffixes.pop_back();
222  } else if (suffixes.back() == "debug") {
224  suffixes.pop_back();
225  } else if (suffixes.back() == "blackhole") {
227  suffixes.pop_back();
228  }
229 
230  if (suffixes.empty()) {
231  return;
232  }
233 
234  if (suffixes.back() == "osm") {
237  }
238  suffixes.pop_back();
239  } else if (suffixes.back() == "osh") {
242  }
244  suffixes.pop_back();
245  } else if (suffixes.back() == "osc") {
248  }
250  set("xml_change_format", true);
251  suffixes.pop_back();
252  }
253  }
254 
261  const File& check() const {
263  std::string msg{"Could not detect file format"};
264  if (!m_format_string.empty()) {
265  msg += " from format string '";
266  msg += m_format_string;
267  msg += "'";
268  }
269  if (m_filename.empty()) {
270  msg += " for stdin/stdout";
271  } else {
272  msg += " for filename '";
273  msg += m_filename;
274  msg += "'";
275  }
276  msg += ".";
277  throw io_error{msg};
278  }
279  return *this;
280  }
281 
282  file_format format() const noexcept {
283  return m_file_format;
284  }
285 
288  return *this;
289  }
290 
291  file_compression compression() const noexcept {
292  return m_file_compression;
293  }
294 
297  return *this;
298  }
299 
300  bool has_multiple_object_versions() const noexcept {
302  }
303 
304  File& set_has_multiple_object_versions(bool value) noexcept {
306  return *this;
307  }
308 
309  File& filename(const std::string& filename) {
310  if (filename == "-") {
311  m_filename = "";
312  } else {
314  }
315  return *this;
316  }
317 
318  const std::string& filename() const noexcept {
319  return m_filename;
320  }
321 
322  }; // class File
323 
324  } // namespace io
325 
326 } // namespace osmium
327 
328 #endif // OSMIUM_IO_FILE_HPP
Definition: file.hpp:72
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:295
File & filename(const std::string &filename)
Definition: file.hpp:309
bool m_has_multiple_object_versions
Definition: file.hpp:87
std::string m_filename
Definition: file.hpp:76
size_t buffer_size() const noexcept
Definition: file.hpp:147
File(std::string filename="", std::string format="")
Definition: file.hpp:103
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:300
const File & check() const
Definition: file.hpp:261
size_t m_buffer_size
Definition: file.hpp:79
file_compression m_file_compression
Definition: file.hpp:85
file_format m_file_format
Definition: file.hpp:83
const char * buffer() const noexcept
Definition: file.hpp:143
std::string m_format_string
Definition: file.hpp:81
File & set_format(file_format format) noexcept
Definition: file.hpp:286
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:134
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:179
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:304
const char * m_buffer
Definition: file.hpp:78
void parse_format(const std::string &format)
Definition: file.hpp:151
const std::string & filename() const noexcept
Definition: file.hpp:318
file_compression compression() const noexcept
Definition: file.hpp:291
file_format format() const noexcept
Definition: file.hpp:282
Definition: options.hpp:58
Definition: attr.hpp:342
file_format
Definition: file_format.hpp:42
file_compression
Definition: file_compression.hpp:42
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:551
Definition: error.hpp:44