Libosmium  2.11.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
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") {
119  }
120 
121  if (format.empty()) {
122  detect_format_from_suffix(m_filename);
123  } else {
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 != "") {
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") {
187  } else if (get("history") == "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()) return;
196 
197  // if the last suffix is one of a known set of compressions,
198  // set that compression
199  if (suffixes.back() == "gz") {
201  suffixes.pop_back();
202  } else if (suffixes.back() == "bz2") {
204  suffixes.pop_back();
205  }
206 
207  if (suffixes.empty()) return;
208 
209  // if the last suffix is one of a known set of formats,
210  // set that format
211  if (suffixes.back() == "pbf") {
213  suffixes.pop_back();
214  } else if (suffixes.back() == "xml") {
216  suffixes.pop_back();
217  } else if (suffixes.back() == "opl") {
219  suffixes.pop_back();
220  } else if (suffixes.back() == "json") {
222  suffixes.pop_back();
223  } else if (suffixes.back() == "o5m") {
225  suffixes.pop_back();
226  } else if (suffixes.back() == "o5c") {
229  set("o5c_change_format", true);
230  suffixes.pop_back();
231  } else if (suffixes.back() == "debug") {
233  suffixes.pop_back();
234  }
235 
236  if (suffixes.empty()) return;
237 
238  if (suffixes.back() == "osm") {
240  suffixes.pop_back();
241  } else if (suffixes.back() == "osh") {
244  suffixes.pop_back();
245  } else if (suffixes.back() == "osc") {
248  set("xml_change_format", true);
249  suffixes.pop_back();
250  }
251  }
252 
259  const File& check() const {
261  std::string msg = "Could not detect file format";
262  if (!m_format_string.empty()) {
263  msg += " from format string '";
264  msg += m_format_string;
265  msg += "'";
266  }
267  if (m_filename.empty()) {
268  msg += " for stdin/stdout";
269  } else {
270  msg += " for filename '";
271  msg += m_filename;
272  msg += "'";
273  }
274  msg += ".";
275  throw io_error(msg);
276  }
277  return *this;
278  }
279 
280  file_format format() const noexcept {
281  return m_file_format;
282  }
283 
286  return *this;
287  }
288 
289  file_compression compression() const noexcept {
290  return m_file_compression;
291  }
292 
295  return *this;
296  }
297 
298  bool has_multiple_object_versions() const noexcept {
300  }
301 
302  File& set_has_multiple_object_versions(bool value) noexcept {
304  return *this;
305  }
306 
307  File& filename(const std::string& filename) {
308  if (filename == "-") {
309  m_filename = "";
310  } else {
311  m_filename = filename;
312  }
313  return *this;
314  }
315 
316  const std::string& filename() const noexcept {
317  return m_filename;
318  }
319 
320  }; // class File
321 
322  } // namespace io
323 
324 } // namespace osmium
325 
326 #endif // OSMIUM_IO_FILE_HPP
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:302
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:298
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
bool m_has_multiple_object_versions
Definition: file.hpp:87
Definition: options.hpp:58
Definition: file.hpp:72
const File & check() const
Definition: file.hpp:259
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
file_compression m_file_compression
Definition: file.hpp:85
Definition: attr.hpp:333
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:293
std::string m_format_string
Definition: file.hpp:81
file_format format() const noexcept
Definition: file.hpp:280
Definition: error.hpp:44
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:284
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:156
File & operator=(const File &)=default
file_format m_file_format
Definition: file.hpp:83
size_t size() const noexcept
Definition: options.hpp:154
std::string m_filename
Definition: file.hpp:76
file_compression compression() const noexcept
Definition: file.hpp:289
size_t m_buffer_size
Definition: file.hpp:79
File & filename(const std::string &filename)
Definition: file.hpp:307
const std::string & filename() const noexcept
Definition: file.hpp:316
void set(const std::string &key, const std::string &value)
Definition: options.hpp:87
const char * m_buffer
Definition: file.hpp:78