Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2015 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 
44 #include <osmium/util/options.hpp>
46 
47 namespace osmium {
48 
52  namespace io {
53 
54  namespace detail {
55 
56  inline std::vector<std::string> split(const std::string& in, const char delim) {
57  std::vector<std::string> result;
58  std::stringstream ss(in);
59  std::string item;
60  while (std::getline(ss, item, delim)) {
61  result.push_back(item);
62  }
63  return result;
64  }
65 
66  } // namespace detail
67 
73  class File : public osmium::util::Options {
74 
75  private:
76 
77  std::string m_filename;
78 
79  const char* m_buffer;
80  size_t m_buffer_size;
81 
82  std::string m_format_string;
83 
85 
87 
89 
90  public:
91 
104  explicit File(const std::string& filename = "", const std::string& format = "") :
105  Options(),
106  m_filename(filename),
107  m_buffer(nullptr),
108  m_buffer_size(0),
109  m_format_string(format) {
110 
111  // stdin/stdout
112  if (m_filename == "-") {
113  m_filename = "";
114  }
115 
116  // if filename is a URL, default to XML format
117  std::string protocol = m_filename.substr(0, m_filename.find_first_of(':'));
118  if (protocol == "http" || protocol == "https") {
120  }
121 
122  if (format.empty()) {
123  detect_format_from_suffix(m_filename);
124  } else {
126  }
127  }
128 
138  explicit File(const char* buffer, size_t size, const std::string& format = "") :
139  Options(),
140  m_filename(),
141  m_buffer(buffer),
142  m_buffer_size(size),
143  m_format_string(format) {
144  if (format != "") {
146  }
147  }
148 
149  File(const File&) = default;
150  File& operator=(const File&) = default;
151 
152  File(File&&) = default;
153  File& operator=(File&&) = default;
154 
155  ~File() = default;
156 
157  const char* buffer() const noexcept {
158  return m_buffer;
159  }
160 
161  size_t buffer_size() const noexcept {
162  return m_buffer_size;
163  }
164 
165  void parse_format(const std::string& format) {
166  std::vector<std::string> options = detail::split(format, ',');
167 
168  // if the first item in the format list doesn't contain
169  // an equals sign, it is a format
170  if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
171  detect_format_from_suffix(options[0]);
172  options.erase(options.begin());
173  }
174 
175  for (auto& option : options) {
176  size_t pos = option.find_first_of('=');
177  if (pos == std::string::npos) {
178  set(option, true);
179  } else {
180  std::string value = option.substr(pos+1);
181  option.erase(pos);
182  set(option, value);
183  }
184  }
185 
186  if (get("history") == "true") {
188  } else if (get("history") == "false") {
190  }
191  }
192 
193  void detect_format_from_suffix(const std::string& name) {
194  std::vector<std::string> suffixes = detail::split(name, '.');
195 
196  if (suffixes.empty()) return;
197 
198  // if the last suffix is one of a known set of compressions,
199  // set that compression
200  if (suffixes.back() == "gz") {
202  suffixes.pop_back();
203  } else if (suffixes.back() == "bz2") {
205  suffixes.pop_back();
206  }
207 
208  if (suffixes.empty()) return;
209 
210  // if the last suffix is one of a known set of formats,
211  // set that format
212  if (suffixes.back() == "pbf") {
214  suffixes.pop_back();
215  } else if (suffixes.back() == "xml") {
217  suffixes.pop_back();
218  } else if (suffixes.back() == "opl") {
220  suffixes.pop_back();
221  } else if (suffixes.back() == "json") {
223  suffixes.pop_back();
224  } else if (suffixes.back() == "o5m") {
226  suffixes.pop_back();
227  } else if (suffixes.back() == "o5c") {
230  set("o5c_change_format", true);
231  suffixes.pop_back();
232  } else if (suffixes.back() == "debug") {
234  suffixes.pop_back();
235  }
236 
237  if (suffixes.empty()) return;
238 
239  if (suffixes.back() == "osm") {
241  suffixes.pop_back();
242  } else if (suffixes.back() == "osh") {
245  suffixes.pop_back();
246  } else if (suffixes.back() == "osc") {
249  set("xml_change_format", true);
250  suffixes.pop_back();
251  }
252  }
253 
260  const File& check() const {
262  std::string msg = "Could not detect file format";
263  if (!m_format_string.empty()) {
264  msg += " from format string '";
265  msg += m_format_string;
266  msg += "'";
267  }
268  if (m_filename.empty()) {
269  msg += " for stdin/stdout";
270  } else {
271  msg += " for filename '";
272  msg += m_filename;
273  msg += "'";
274  }
275  msg += ".";
276  throw std::runtime_error(msg);
277  }
278  return *this;
279  }
280 
281  file_format format() const noexcept {
282  return m_file_format;
283  }
284 
287  return *this;
288  }
289 
290  file_compression compression() const noexcept {
291  return m_file_compression;
292  }
293 
296  return *this;
297  }
298 
299  bool has_multiple_object_versions() const noexcept {
301  }
302 
303  File& set_has_multiple_object_versions(bool value) noexcept {
305  return *this;
306  }
307 
308  File& filename(const std::string& filename) {
309  if (filename == "-") {
310  m_filename = "";
311  } else {
312  m_filename = filename;
313  }
314  return *this;
315  }
316 
317  const std::string& filename() const noexcept {
318  return m_filename;
319  }
320 
321  }; // class File
322 
323  } // namespace io
324 
325 } // namespace osmium
326 
327 #endif // OSMIUM_IO_FILE_HPP
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:303
void parse_format(const std::string &format)
Definition: file.hpp:165
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:138
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:299
File(const std::string &filename="", const std::string &format="")
Definition: file.hpp:104
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:193
size_t buffer_size() const noexcept
Definition: file.hpp:161
bool m_has_multiple_object_versions
Definition: file.hpp:88
Definition: options.hpp:53
Definition: file.hpp:73
const File & check() const
Definition: file.hpp:260
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
file_compression m_file_compression
Definition: file.hpp:86
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:294
std::string m_format_string
Definition: file.hpp:82
file_format format() const noexcept
Definition: file.hpp:281
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:285
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:157
File & operator=(const File &)=default
file_format m_file_format
Definition: file.hpp:84
size_t size() const noexcept
Definition: options.hpp:121
std::string m_filename
Definition: file.hpp:77
file_compression compression() const noexcept
Definition: file.hpp:290
size_t m_buffer_size
Definition: file.hpp:80
File & filename(const std::string &filename)
Definition: file.hpp:308
const std::string & filename() const noexcept
Definition: file.hpp:317
void set(const std::string &key, const std::string &value)
Definition: options.hpp:78
const char * m_buffer
Definition: file.hpp:79