Libosmium  2.20.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/io/error.hpp>
40
41#include <cstddef>
42#include <sstream>
43#include <string>
44#include <vector>
45
46namespace 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::Options {
73
74 private:
75
76 std::string m_filename{};
77
78 const char* m_buffer = nullptr;
79 size_t m_buffer_size = 0;
80
81 std::string m_format_string;
82
84
86
88
89 public:
90
103 explicit File(std::string filename = "", std::string format = "") :
104 m_filename(std::move(filename)),
105 m_format_string(std::move(format)) {
106
107 // stdin/stdout
108 if (m_filename == "-") {
109 m_filename = "";
110 }
111
112 // if filename is a URL, default to XML format
113 const std::string protocol{m_filename.substr(0, m_filename.find_first_of(':'))};
114 if (protocol == "http" || protocol == "https") {
116 }
117
118 if (m_format_string.empty()) {
120 } else {
122 }
123 }
124
134 explicit File(const char* buffer, size_t size, const std::string& format = "") :
136 m_buffer_size(size),
138 if (!format.empty()) {
140 }
141 }
142
143 const char* buffer() const noexcept {
144 return m_buffer;
145 }
146
147 size_t buffer_size() const noexcept {
148 return m_buffer_size;
149 }
150
151 void parse_format(const std::string& format) {
152 std::vector<std::string> options = detail::split(format, ',');
153
154 // if the first item in the format list doesn't contain
155 // an equals sign, it is a format
156 if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
157 detect_format_from_suffix(options[0]);
158 options.erase(options.begin());
159 }
160
161 for (auto& option : options) {
162 const size_t pos = option.find_first_of('=');
163 if (pos == std::string::npos) {
164 set(option, true);
165 } else {
166 const std::string value{option.substr(pos + 1)};
167 option.erase(pos);
168 set(option, value);
169 }
170 }
171
172 if (get("history") == "true") {
174 } else if (get("history") == "false") {
176 }
177 }
178
179 void detect_format_from_suffix(const std::string& name) {
180 std::vector<std::string> suffixes = detail::split(name, '.');
181
182 if (suffixes.empty()) {
183 return;
184 }
185
186 // if the last suffix is one of a known set of compressions,
187 // set that compression
188 if (suffixes.back() == "gz") {
190 suffixes.pop_back();
191 } else if (suffixes.back() == "bz2") {
193 suffixes.pop_back();
194 }
195
196 if (suffixes.empty()) {
197 return;
198 }
199
200 // if the last suffix is one of a known set of formats,
201 // set that format
202 if (suffixes.back() == "pbf") {
204 suffixes.pop_back();
205 } else if (suffixes.back() == "xml") {
207 suffixes.pop_back();
208 } else if (suffixes.back() == "opl") {
210 suffixes.pop_back();
211 } else if (suffixes.back() == "json") {
213 suffixes.pop_back();
214 } else if (suffixes.back() == "o5m") {
216 suffixes.pop_back();
217 } else if (suffixes.back() == "o5c") {
220 set("o5c_change_format", true);
221 suffixes.pop_back();
222 } else if (suffixes.back() == "debug") {
224 suffixes.pop_back();
225 } else if (suffixes.back() == "blackhole") {
227 suffixes.pop_back();
228 } else if (suffixes.back() == "ids") {
230 suffixes.pop_back();
231 }
232
233 if (suffixes.empty()) {
234 return;
235 }
236
237 if (suffixes.back() == "osm") {
240 }
241 suffixes.pop_back();
242 } else if (suffixes.back() == "osh") {
245 }
247 suffixes.pop_back();
248 } else if (suffixes.back() == "osc") {
251 }
253 set("xml_change_format", true);
254 suffixes.pop_back();
255 }
256 }
257
264 const File& check() const {
266 std::string msg{"Could not detect file format"};
267 if (!m_format_string.empty()) {
268 msg += " from format string '";
269 msg += m_format_string;
270 msg += "'";
271 }
272 if (m_filename.empty()) {
273 msg += " for stdin/stdout";
274 } else {
275 msg += " for filename '";
276 msg += m_filename;
277 msg += "'";
278 }
279 msg += ".";
280 throw io_error{msg};
281 }
282 return *this;
283 }
284
285 file_format format() const noexcept {
286 return m_file_format;
287 }
288
291 return *this;
292 }
293
294 file_compression compression() const noexcept {
295 return m_file_compression;
296 }
297
300 return *this;
301 }
302
303 bool has_multiple_object_versions() const noexcept {
305 }
306
309 return *this;
310 }
311
312 File& filename(const std::string& filename) {
313 if (filename == "-") {
314 m_filename = "";
315 } else {
317 }
318 return *this;
319 }
320
321 const std::string& filename() const noexcept {
322 return m_filename;
323 }
324
325 }; // class File
326
327 } // namespace io
328
329} // namespace osmium
330
331#endif // OSMIUM_IO_FILE_HPP
Definition: file.hpp:72
bool m_has_multiple_object_versions
Definition: file.hpp:87
std::string m_filename
Definition: file.hpp:76
size_t buffer_size() const noexcept
Definition: file.hpp:147
File(std::string filename="", std::string format="")
Definition: file.hpp:103
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:303
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:298
size_t m_buffer_size
Definition: file.hpp:79
file_compression m_file_compression
Definition: file.hpp:85
const File & check() const
Definition: file.hpp:264
File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:307
File & filename(const std::string &filename)
Definition: file.hpp:312
file_format m_file_format
Definition: file.hpp:83
File & set_format(file_format format) noexcept
Definition: file.hpp:289
std::string m_format_string
Definition: file.hpp:81
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:134
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:179
const std::string & filename() const noexcept
Definition: file.hpp:321
const char * m_buffer
Definition: file.hpp:78
void parse_format(const std::string &format)
Definition: file.hpp:151
file_compression compression() const noexcept
Definition: file.hpp:294
file_format format() const noexcept
Definition: file.hpp:285
const char * buffer() const noexcept
Definition: file.hpp:143
Definition: options.hpp:58
Definition: attr.hpp:342
file_format
Definition: file_format.hpp:42
file_compression
Definition: file_compression.hpp:42
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555
Definition: error.hpp:46