Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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 (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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 <osmium/io/error.hpp>
37#include <osmium/io/file_compression.hpp>
38#include <osmium/io/file_format.hpp>
39#include <osmium/util/options.hpp>
40
41#include <cstddef>
42#include <sstream>
43#include <string>
44#include <vector>
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::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
83 file_format m_file_format = file_format::unknown;
84
85 file_compression m_file_compression = file_compression::none;
86
87 bool m_has_multiple_object_versions = false;
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") {
115 m_file_format = file_format::xml;
116 }
117
118 if (m_format_string.empty()) {
119 detect_format_from_suffix(m_filename);
120 } else {
121 parse_format(m_format_string);
122 }
123 }
124
134 explicit File(const char* buffer, size_t size, const std::string& format = "") :
135 m_buffer(buffer),
136 m_buffer_size(size),
137 m_format_string(format) {
138 if (!format.empty()) {
139 parse_format(format);
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") {
173 m_has_multiple_object_versions = true;
174 } else if (get("history") == "false") {
175 m_has_multiple_object_versions = 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") {
189 m_file_compression = file_compression::gzip;
190 suffixes.pop_back();
191 } else if (suffixes.back() == "bz2") {
192 m_file_compression = file_compression::bzip2;
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") {
203 m_file_format = file_format::pbf;
204 suffixes.pop_back();
205 } else if (suffixes.back() == "xml") {
206 m_file_format = file_format::xml;
207 suffixes.pop_back();
208 } else if (suffixes.back() == "opl") {
209 m_file_format = file_format::opl;
210 suffixes.pop_back();
211 } else if (suffixes.back() == "json") {
212 m_file_format = file_format::json;
213 suffixes.pop_back();
214 } else if (suffixes.back() == "o5m") {
215 m_file_format = file_format::o5m;
216 suffixes.pop_back();
217 } else if (suffixes.back() == "o5c") {
218 m_file_format = file_format::o5m;
219 m_has_multiple_object_versions = true;
220 set("o5c_change_format", true);
221 suffixes.pop_back();
222 } else if (suffixes.back() == "debug") {
223 m_file_format = file_format::debug;
224 suffixes.pop_back();
225 } else if (suffixes.back() == "blackhole") {
226 m_file_format = file_format::blackhole;
227 suffixes.pop_back();
228 } else if (suffixes.back() == "ids") {
229 m_file_format = file_format::ids;
230 suffixes.pop_back();
231 }
232
233 if (suffixes.empty()) {
234 return;
235 }
236
237 if (suffixes.back() == "osm") {
238 if (m_file_format == file_format::unknown) {
239 m_file_format = file_format::xml;
240 }
241 suffixes.pop_back();
242 } else if (suffixes.back() == "osh") {
243 if (m_file_format == file_format::unknown) {
244 m_file_format = file_format::xml;
245 }
246 m_has_multiple_object_versions = true;
247 suffixes.pop_back();
248 } else if (suffixes.back() == "osc") {
249 if (m_file_format == file_format::unknown) {
250 m_file_format = file_format::xml;
251 }
252 m_has_multiple_object_versions = true;
253 set("xml_change_format", true);
254 suffixes.pop_back();
255 }
256 }
257
264 const File& check() const {
265 if (m_file_format == file_format::unknown) {
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
289 File& set_format(file_format format) noexcept {
290 m_file_format = format;
291 return *this;
292 }
293
294 file_compression compression() const noexcept {
295 return m_file_compression;
296 }
297
298 File& set_compression(file_compression compression) noexcept {
299 m_file_compression = compression;
300 return *this;
301 }
302
303 bool has_multiple_object_versions() const noexcept {
304 return m_has_multiple_object_versions;
305 }
306
307 File& set_has_multiple_object_versions(bool value) noexcept {
308 m_has_multiple_object_versions = value;
309 return *this;
310 }
311
312 File& filename(const std::string& filename) {
313 if (filename == "-") {
314 m_filename = "";
315 } else {
316 m_filename = filename;
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
osmium::io::File
Definition file.hpp:72
osmium::io::File::m_has_multiple_object_versions
bool m_has_multiple_object_versions
Definition file.hpp:87
osmium::io::File::m_filename
std::string m_filename
Definition file.hpp:76
osmium::io::File::buffer_size
size_t buffer_size() const noexcept
Definition file.hpp:147
osmium::io::File::File
File(std::string filename="", std::string format="")
Definition file.hpp:103
osmium::io::File::has_multiple_object_versions
bool has_multiple_object_versions() const noexcept
Definition file.hpp:303
osmium::io::File::set_compression
File & set_compression(file_compression compression) noexcept
Definition file.hpp:298
osmium::io::File::m_buffer_size
size_t m_buffer_size
Definition file.hpp:79
osmium::io::File::m_file_compression
file_compression m_file_compression
Definition file.hpp:85
osmium::io::File::check
const File & check() const
Definition file.hpp:264
osmium::io::File::set_has_multiple_object_versions
File & set_has_multiple_object_versions(bool value) noexcept
Definition file.hpp:307
osmium::io::File::filename
File & filename(const std::string &filename)
Definition file.hpp:312
osmium::io::File::m_file_format
file_format m_file_format
Definition file.hpp:83
osmium::io::File::set_format
File & set_format(file_format format) noexcept
Definition file.hpp:289
osmium::io::File::m_format_string
std::string m_format_string
Definition file.hpp:81
osmium::io::File::File
File(const char *buffer, size_t size, const std::string &format="")
Definition file.hpp:134
osmium::io::File::detect_format_from_suffix
void detect_format_from_suffix(const std::string &name)
Definition file.hpp:179
osmium::io::File::filename
const std::string & filename() const noexcept
Definition file.hpp:321
osmium::io::File::m_buffer
const char * m_buffer
Definition file.hpp:78
osmium::io::File::parse_format
void parse_format(const std::string &format)
Definition file.hpp:151
osmium::io::File::compression
file_compression compression() const noexcept
Definition file.hpp:294
osmium::io::File::format
file_format format() const noexcept
Definition file.hpp:285
osmium::io::File::buffer
const char * buffer() const noexcept
Definition file.hpp:143
osmium::Options
Definition options.hpp:58
error.hpp
file_compression.hpp
file_format.hpp
detail
Definition attr.hpp:342
osmium::io::file_format
file_format
Definition file_format.hpp:42
osmium::io::file_format::xml
@ xml
osmium::io::file_format::json
@ json
osmium::io::file_format::pbf
@ pbf
osmium::io::file_format::opl
@ opl
osmium::io::file_format::blackhole
@ blackhole
osmium::io::file_format::debug
@ debug
osmium::io::file_format::unknown
@ unknown
osmium::io::file_format::o5m
@ o5m
osmium::io::file_format::ids
@ ids
osmium::io::file_compression
file_compression
Definition file_compression.hpp:42
osmium::io::file_compression::bzip2
@ bzip2
osmium::io::file_compression::none
@ none
osmium::io::file_compression::gzip
@ gzip
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555
options.hpp
osmium::io_error
Definition error.hpp:46