Libosmium  2.6.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-2016 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 
42 #include <osmium/io/error.hpp>
45 #include <osmium/util/options.hpp>
47 
48 namespace osmium {
49 
53  namespace io {
54 
55  namespace detail {
56 
57  inline std::vector<std::string> split(const std::string& in, const char delim) {
58  std::vector<std::string> result;
59  std::stringstream ss(in);
60  std::string item;
61  while (std::getline(ss, item, delim)) {
62  result.push_back(item);
63  }
64  return result;
65  }
66 
67  } // namespace detail
68 
74  class File : public osmium::util::Options {
75 
76  private:
77 
78  std::string m_filename;
79 
80  const char* m_buffer;
81  size_t m_buffer_size;
82 
83  std::string m_format_string;
84 
86 
88 
89  bool m_has_multiple_object_versions {false};
90 
91  public:
92 
105  explicit File(const std::string& filename = "", const std::string& format = "") :
106  Options(),
107  m_filename(filename),
108  m_buffer(nullptr),
109  m_buffer_size(0),
110  m_format_string(format) {
111 
112  // stdin/stdout
113  if (m_filename == "-") {
114  m_filename = "";
115  }
116 
117  // if filename is a URL, default to XML format
118  std::string protocol = m_filename.substr(0, m_filename.find_first_of(':'));
119  if (protocol == "http" || protocol == "https") {
120  m_file_format = file_format::xml;
121  }
122 
123  if (format.empty()) {
124  detect_format_from_suffix(m_filename);
125  } else {
126  parse_format(format);
127  }
128  }
129 
139  explicit File(const char* buffer, size_t size, const std::string& format = "") :
140  Options(),
141  m_filename(),
142  m_buffer(buffer),
143  m_buffer_size(size),
144  m_format_string(format) {
145  if (format != "") {
146  parse_format(format);
147  }
148  }
149 
150  File(const File&) = default;
151  File& operator=(const File&) = default;
152 
153  File(File&&) = default;
154  File& operator=(File&&) = default;
155 
156  ~File() = default;
157 
158  const char* buffer() const noexcept {
159  return m_buffer;
160  }
161 
162  size_t buffer_size() const noexcept {
163  return m_buffer_size;
164  }
165 
166  void parse_format(const std::string& format) {
167  std::vector<std::string> options = detail::split(format, ',');
168 
169  // if the first item in the format list doesn't contain
170  // an equals sign, it is a format
171  if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
172  detect_format_from_suffix(options[0]);
173  options.erase(options.begin());
174  }
175 
176  for (auto& option : options) {
177  size_t pos = option.find_first_of('=');
178  if (pos == std::string::npos) {
179  set(option, true);
180  } else {
181  std::string value = option.substr(pos+1);
182  option.erase(pos);
183  set(option, value);
184  }
185  }
186 
187  if (get("history") == "true") {
188  m_has_multiple_object_versions = true;
189  } else if (get("history") == "false") {
190  m_has_multiple_object_versions = false;
191  }
192  }
193 
194  void detect_format_from_suffix(const std::string& name) {
195  std::vector<std::string> suffixes = detail::split(name, '.');
196 
197  if (suffixes.empty()) return;
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()) return;
210 
211  // if the last suffix is one of a known set of formats,
212  // set that format
213  if (suffixes.back() == "pbf") {
214  m_file_format = file_format::pbf;
215  suffixes.pop_back();
216  } else if (suffixes.back() == "xml") {
217  m_file_format = file_format::xml;
218  suffixes.pop_back();
219  } else if (suffixes.back() == "opl") {
220  m_file_format = file_format::opl;
221  suffixes.pop_back();
222  } else if (suffixes.back() == "json") {
223  m_file_format = file_format::json;
224  suffixes.pop_back();
225  } else if (suffixes.back() == "o5m") {
226  m_file_format = file_format::o5m;
227  suffixes.pop_back();
228  } else if (suffixes.back() == "o5c") {
229  m_file_format = file_format::o5m;
230  m_has_multiple_object_versions = true;
231  set("o5c_change_format", true);
232  suffixes.pop_back();
233  } else if (suffixes.back() == "debug") {
234  m_file_format = file_format::debug;
235  suffixes.pop_back();
236  }
237 
238  if (suffixes.empty()) return;
239 
240  if (suffixes.back() == "osm") {
241  if (m_file_format == file_format::unknown) m_file_format = file_format::xml;
242  suffixes.pop_back();
243  } else if (suffixes.back() == "osh") {
244  if (m_file_format == file_format::unknown) m_file_format = file_format::xml;
245  m_has_multiple_object_versions = true;
246  suffixes.pop_back();
247  } else if (suffixes.back() == "osc") {
248  if (m_file_format == file_format::unknown) m_file_format = file_format::xml;
249  m_has_multiple_object_versions = true;
250  set("xml_change_format", true);
251  suffixes.pop_back();
252  }
253  }
254 
261  const File& check() const {
262  if (m_file_format == file_format::unknown) {
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 
286  File& set_format(file_format format) noexcept {
287  m_file_format = format;
288  return *this;
289  }
290 
291  file_compression compression() const noexcept {
292  return m_file_compression;
293  }
294 
295  File& set_compression(file_compression compression) noexcept {
296  m_file_compression = compression;
297  return *this;
298  }
299 
300  bool has_multiple_object_versions() const noexcept {
301  return m_has_multiple_object_versions;
302  }
303 
304  File& set_has_multiple_object_versions(bool value) noexcept {
305  m_has_multiple_object_versions = value;
306  return *this;
307  }
308 
309  File& filename(const std::string& filename) {
310  if (filename == "-") {
311  m_filename = "";
312  } else {
313  m_filename = filename;
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
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:304
void parse_format(const std::string &format)
Definition: file.hpp:166
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:139
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:300
File(const std::string &filename="", const std::string &format="")
Definition: file.hpp:105
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:194
size_t buffer_size() const noexcept
Definition: file.hpp:162
Definition: options.hpp:57
Definition: file.hpp:74
const File & check() const
Definition: file.hpp:261
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: attr.hpp:298
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:295
std::string m_format_string
Definition: file.hpp:83
file_format format() const noexcept
Definition: file.hpp:282
Definition: error.hpp:44
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:286
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:158
std::string m_filename
Definition: file.hpp:78
file_compression compression() const noexcept
Definition: file.hpp:291
size_t m_buffer_size
Definition: file.hpp:81
File & filename(const std::string &filename)
Definition: file.hpp:309
const std::string & filename() const noexcept
Definition: file.hpp:318
const char * m_buffer
Definition: file.hpp:80