Libosmium  2.13.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-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  }
239 
240  if (suffixes.empty()) {
241  return;
242  }
243 
244  if (suffixes.back() == "osm") {
245  if (m_file_format == file_format::unknown) {
246  m_file_format = file_format::xml;
247  }
248  suffixes.pop_back();
249  } else if (suffixes.back() == "osh") {
250  if (m_file_format == file_format::unknown) {
251  m_file_format = file_format::xml;
252  }
253  m_has_multiple_object_versions = true;
254  suffixes.pop_back();
255  } else if (suffixes.back() == "osc") {
256  if (m_file_format == file_format::unknown) {
257  m_file_format = file_format::xml;
258  }
259  m_has_multiple_object_versions = true;
260  set("xml_change_format", true);
261  suffixes.pop_back();
262  }
263  }
264 
271  const File& check() const {
272  if (m_file_format == file_format::unknown) {
273  std::string msg{"Could not detect file format"};
274  if (!m_format_string.empty()) {
275  msg += " from format string '";
276  msg += m_format_string;
277  msg += "'";
278  }
279  if (m_filename.empty()) {
280  msg += " for stdin/stdout";
281  } else {
282  msg += " for filename '";
283  msg += m_filename;
284  msg += "'";
285  }
286  msg += ".";
287  throw io_error{msg};
288  }
289  return *this;
290  }
291 
292  file_format format() const noexcept {
293  return m_file_format;
294  }
295 
296  File& set_format(file_format format) noexcept {
297  m_file_format = format;
298  return *this;
299  }
300 
301  file_compression compression() const noexcept {
302  return m_file_compression;
303  }
304 
305  File& set_compression(file_compression compression) noexcept {
306  m_file_compression = compression;
307  return *this;
308  }
309 
310  bool has_multiple_object_versions() const noexcept {
311  return m_has_multiple_object_versions;
312  }
313 
314  File& set_has_multiple_object_versions(bool value) noexcept {
315  m_has_multiple_object_versions = value;
316  return *this;
317  }
318 
319  File& filename(const std::string& filename) {
320  if (filename == "-") {
321  m_filename = "";
322  } else {
323  m_filename = filename;
324  }
325  return *this;
326  }
327 
328  const std::string& filename() const noexcept {
329  return m_filename;
330  }
331 
332  }; // class File
333 
334  } // namespace io
335 
336 } // namespace osmium
337 
338 #endif // OSMIUM_IO_FILE_HPP
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:314
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:310
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:305
std::string m_format_string
Definition: file.hpp:81
file_format format() const noexcept
Definition: file.hpp:292
Definition: error.hpp:44
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:296
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:271
file_compression compression() const noexcept
Definition: file.hpp:301
size_t m_buffer_size
Definition: file.hpp:79
File & filename(const std::string &filename)
Definition: file.hpp:319
const std::string & filename() const noexcept
Definition: file.hpp:328
const char * m_buffer
Definition: file.hpp:78