Libosmium  2.13.1
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-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 <cstddef>
37 #include <sstream>
38 #include <string>
39 #include <vector>
40 
41 #include <osmium/io/error.hpp>
44 #include <osmium/util/options.hpp>
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::util::Options {
73 
74  private:
75 
76  std::string m_filename;
77 
78  const char* m_buffer;
79  size_t m_buffer_size;
80 
81  std::string m_format_string;
82 
84 
86 
87  bool m_has_multiple_object_versions {false};
88 
89  public:
90 
103  explicit File(const std::string& filename = "", const std::string& format = "") :
104  Options(),
105  m_filename(filename),
106  m_buffer(nullptr),
107  m_buffer_size(0),
108  m_format_string(format) {
109 
110  // stdin/stdout
111  if (m_filename == "-") {
112  m_filename = "";
113  }
114 
115  // if filename is a URL, default to XML format
116  const std::string protocol{m_filename.substr(0, m_filename.find_first_of(':'))};
117  if (protocol == "http" || protocol == "https") {
118  m_file_format = file_format::xml;
119  }
120 
121  if (format.empty()) {
122  detect_format_from_suffix(m_filename);
123  } else {
124  parse_format(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  if (format != "") {
144  parse_format(format);
145  }
146  }
147 
148  File(const File&) = default;
149  File& operator=(const File&) = default;
150 
151  File(File&&) = default;
152  File& operator=(File&&) = default;
153 
154  ~File() = default;
155 
156  const char* buffer() const noexcept {
157  return m_buffer;
158  }
159 
160  size_t buffer_size() const noexcept {
161  return m_buffer_size;
162  }
163 
164  void parse_format(const std::string& format) {
165  std::vector<std::string> options = detail::split(format, ',');
166 
167  // if the first item in the format list doesn't contain
168  // an equals sign, it is a format
169  if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
170  detect_format_from_suffix(options[0]);
171  options.erase(options.begin());
172  }
173 
174  for (auto& option : options) {
175  const size_t pos = option.find_first_of('=');
176  if (pos == std::string::npos) {
177  set(option, true);
178  } else {
179  std::string value{option.substr(pos+1)};
180  option.erase(pos);
181  set(option, value);
182  }
183  }
184 
185  if (get("history") == "true") {
186  m_has_multiple_object_versions = true;
187  } else if (get("history") == "false") {
188  m_has_multiple_object_versions = false;
189  }
190  }
191 
192  void detect_format_from_suffix(const std::string& name) {
193  std::vector<std::string> suffixes = detail::split(name, '.');
194 
195  if (suffixes.empty()) {
196  return;
197  }
198 
199  // if the last suffix is one of a known set of compressions,
200  // set that compression
201  if (suffixes.back() == "gz") {
202  m_file_compression = file_compression::gzip;
203  suffixes.pop_back();
204  } else if (suffixes.back() == "bz2") {
205  m_file_compression = file_compression::bzip2;
206  suffixes.pop_back();
207  }
208 
209  if (suffixes.empty()) {
210  return;
211  }
212 
213  // if the last suffix is one of a known set of formats,
214  // set that format
215  if (suffixes.back() == "pbf") {
216  m_file_format = file_format::pbf;
217  suffixes.pop_back();
218  } else if (suffixes.back() == "xml") {
219  m_file_format = file_format::xml;
220  suffixes.pop_back();
221  } else if (suffixes.back() == "opl") {
222  m_file_format = file_format::opl;
223  suffixes.pop_back();
224  } else if (suffixes.back() == "json") {
225  m_file_format = file_format::json;
226  suffixes.pop_back();
227  } else if (suffixes.back() == "o5m") {
228  m_file_format = file_format::o5m;
229  suffixes.pop_back();
230  } else if (suffixes.back() == "o5c") {
231  m_file_format = file_format::o5m;
232  m_has_multiple_object_versions = true;
233  set("o5c_change_format", true);
234  suffixes.pop_back();
235  } else if (suffixes.back() == "debug") {
236  m_file_format = file_format::debug;
237  suffixes.pop_back();
238  } else if (suffixes.back() == "blackhole") {
239  m_file_format = file_format::blackhole;
240  suffixes.pop_back();
241  }
242 
243  if (suffixes.empty()) {
244  return;
245  }
246 
247  if (suffixes.back() == "osm") {
248  if (m_file_format == file_format::unknown) {
249  m_file_format = file_format::xml;
250  }
251  suffixes.pop_back();
252  } else if (suffixes.back() == "osh") {
253  if (m_file_format == file_format::unknown) {
254  m_file_format = file_format::xml;
255  }
256  m_has_multiple_object_versions = true;
257  suffixes.pop_back();
258  } else if (suffixes.back() == "osc") {
259  if (m_file_format == file_format::unknown) {
260  m_file_format = file_format::xml;
261  }
262  m_has_multiple_object_versions = true;
263  set("xml_change_format", true);
264  suffixes.pop_back();
265  }
266  }
267 
274  const File& check() const {
275  if (m_file_format == file_format::unknown) {
276  std::string msg{"Could not detect file format"};
277  if (!m_format_string.empty()) {
278  msg += " from format string '";
279  msg += m_format_string;
280  msg += "'";
281  }
282  if (m_filename.empty()) {
283  msg += " for stdin/stdout";
284  } else {
285  msg += " for filename '";
286  msg += m_filename;
287  msg += "'";
288  }
289  msg += ".";
290  throw io_error{msg};
291  }
292  return *this;
293  }
294 
295  file_format format() const noexcept {
296  return m_file_format;
297  }
298 
299  File& set_format(file_format format) noexcept {
300  m_file_format = format;
301  return *this;
302  }
303 
304  file_compression compression() const noexcept {
305  return m_file_compression;
306  }
307 
308  File& set_compression(file_compression compression) noexcept {
309  m_file_compression = compression;
310  return *this;
311  }
312 
313  bool has_multiple_object_versions() const noexcept {
314  return m_has_multiple_object_versions;
315  }
316 
317  File& set_has_multiple_object_versions(bool value) noexcept {
318  m_has_multiple_object_versions = value;
319  return *this;
320  }
321 
322  File& filename(const std::string& filename) {
323  if (filename == "-") {
324  m_filename = "";
325  } else {
326  m_filename = filename;
327  }
328  return *this;
329  }
330 
331  const std::string& filename() const noexcept {
332  return m_filename;
333  }
334 
335  }; // class File
336 
337  } // namespace io
338 
339 } // namespace osmium
340 
341 #endif // OSMIUM_IO_FILE_HPP
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:317
void parse_format(const std::string &format)
Definition: file.hpp:164
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:137
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:313
File(const std::string &filename="", const std::string &format="")
Definition: file.hpp:103
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:192
size_t buffer_size() const noexcept
Definition: file.hpp:160
Definition: options.hpp:58
Definition: file.hpp:72
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:308
std::string m_format_string
Definition: file.hpp:81
file_format format() const noexcept
Definition: file.hpp:295
Definition: error.hpp:44
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:299
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:156
std::string m_filename
Definition: file.hpp:76
const File & check() const
Definition: file.hpp:274
file_compression compression() const noexcept
Definition: file.hpp:304
size_t m_buffer_size
Definition: file.hpp:79
File & filename(const std::string &filename)
Definition: file.hpp:322
const std::string & filename() const noexcept
Definition: file.hpp:331
const char * m_buffer
Definition: file.hpp:78