Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
#ifndef OSMIUM_IO_FILE_HPP
#define OSMIUM_IO_FILE_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/io/error.hpp>
#include <osmium/io/file_compression.hpp>
#include <osmium/io/file_format.hpp>
#include <osmium/util/options.hpp>
#include <cstddef>
#include <sstream>
#include <string>
#include <vector>
namespace osmium {
namespace io {
namespace detail {
inline std::vector<std::string> split(const std::string& in, const char delim) {
std::vector<std::string> result;
std::stringstream ss(in);
std::string item;
while (std::getline(ss, item, delim)) {
result.push_back(item);
}
return result;
}
} // namespace detail
class File : public osmium::Options {
private:
std::string m_filename;
const char* m_buffer = nullptr;
size_t m_buffer_size = 0;
std::string m_format_string;
file_format m_file_format = file_format::unknown;
file_compression m_file_compression = file_compression::none;
bool m_has_multiple_object_versions = false;
public:
explicit File(std::string filename = "", std::string format = "") :
m_filename(std::move(filename)),
m_format_string(std::move(format)) {
// stdin/stdout
if (m_filename == "-") {
m_filename = "";
}
// if filename is a URL, default to XML format
const std::string protocol{m_filename.substr(0, m_filename.find_first_of(':'))};
if (protocol == "http" || protocol == "https") {
m_file_format = file_format::xml;
}
if (m_format_string.empty()) {
detect_format_from_suffix(m_filename);
} else {
parse_format(m_format_string);
}
}
explicit File(const char* buffer, size_t size, const std::string& format = "") :
m_buffer(buffer),
m_buffer_size(size),
m_format_string(format) {
if (!format.empty()) {
parse_format(format);
}
}
const char* buffer() const noexcept {
return m_buffer;
}
size_t buffer_size() const noexcept {
return m_buffer_size;
}
void parse_format(const std::string& format) {
std::vector<std::string> options = detail::split(format, ',');
// if the first item in the format list doesn't contain
// an equals sign, it is a format
if (!options.empty() && options[0].find_first_of('=') == std::string::npos) {
detect_format_from_suffix(options[0]);
options.erase(options.begin());
}
for (auto& option : options) {
const size_t pos = option.find_first_of('=');
if (pos == std::string::npos) {
set(option, true);
} else {
const std::string value{option.substr(pos + 1)};
option.erase(pos);
set(option, value);
}
}
if (get("history") == "true") {
m_has_multiple_object_versions = true;
} else if (get("history") == "false") {
m_has_multiple_object_versions = false;
}
}
void detect_format_from_suffix(const std::string& name) {
std::vector<std::string> suffixes = detail::split(name, '.');
if (suffixes.empty()) {
return;
}
// if the last suffix is one of a known set of compressions,
// set that compression
if (suffixes.back() == "gz") {
m_file_compression = file_compression::gzip;
suffixes.pop_back();
} else if (suffixes.back() == "bz2") {
m_file_compression = file_compression::bzip2;
suffixes.pop_back();
}
if (suffixes.empty()) {
return;
}
// if the last suffix is one of a known set of formats,
// set that format
if (suffixes.back() == "pbf") {
m_file_format = file_format::pbf;
suffixes.pop_back();
} else if (suffixes.back() == "xml") {
m_file_format = file_format::xml;
suffixes.pop_back();
} else if (suffixes.back() == "opl") {
m_file_format = file_format::opl;
suffixes.pop_back();
} else if (suffixes.back() == "json") {
m_file_format = file_format::json;
suffixes.pop_back();
} else if (suffixes.back() == "o5m") {
m_file_format = file_format::o5m;
suffixes.pop_back();
} else if (suffixes.back() == "o5c") {
m_file_format = file_format::o5m;
m_has_multiple_object_versions = true;
set("o5c_change_format", true);
suffixes.pop_back();
} else if (suffixes.back() == "debug") {
m_file_format = file_format::debug;
suffixes.pop_back();
} else if (suffixes.back() == "blackhole") {
m_file_format = file_format::blackhole;
suffixes.pop_back();
} else if (suffixes.back() == "ids") {
m_file_format = file_format::ids;
suffixes.pop_back();
}
if (suffixes.empty()) {
return;
}
if (suffixes.back() == "osm") {
if (m_file_format == file_format::unknown) {
m_file_format = file_format::xml;
}
suffixes.pop_back();
} else if (suffixes.back() == "osh") {
if (m_file_format == file_format::unknown) {
m_file_format = file_format::xml;
}
m_has_multiple_object_versions = true;
suffixes.pop_back();
} else if (suffixes.back() == "osc") {
if (m_file_format == file_format::unknown) {
m_file_format = file_format::xml;
}
m_has_multiple_object_versions = true;
set("xml_change_format", true);
suffixes.pop_back();
}
}
const File& check() const {
if (m_file_format == file_format::unknown) {
std::string msg{"Could not detect file format"};
if (!m_format_string.empty()) {
msg += " from format string '";
msg += m_format_string;
msg += "'";
}
if (m_filename.empty()) {
msg += " for stdin/stdout";
} else {
msg += " for filename '";
msg += m_filename;
msg += "'";
}
msg += ".";
throw io_error{msg};
}
return *this;
}
file_format format() const noexcept {
return m_file_format;
}
File& set_format(file_format format) noexcept {
m_file_format = format;
return *this;
}
file_compression compression() const noexcept {
return m_file_compression;
}
File& set_compression(file_compression compression) noexcept {
m_file_compression = compression;
return *this;
}
bool has_multiple_object_versions() const noexcept {
return m_has_multiple_object_versions;
}
File& set_has_multiple_object_versions(bool value) noexcept {
m_has_multiple_object_versions = value;
return *this;
}
File& filename(const std::string& filename) {
if (filename == "-") {
m_filename = "";
} else {
m_filename = filename;
}
return *this;
}
const std::string& filename() const noexcept {
return m_filename;
}
}; // class File
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_FILE_HPP
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
osmium::io
Everything related to input and output of OSM data.
Definition bzip2_compression.hpp:86
osmium::io::file_format
file_format
Definition file_format.hpp:42
osmium::io::file_format::xml
@ xml
Definition file_format.hpp:44
osmium::io::file_format::json
@ json
Definition file_format.hpp:47
osmium::io::file_format::pbf
@ pbf
Definition file_format.hpp:45
osmium::io::file_format::opl
@ opl
Definition file_format.hpp:46
osmium::io::file_format::blackhole
@ blackhole
Definition file_format.hpp:50
osmium::io::file_format::debug
@ debug
Definition file_format.hpp:49
osmium::io::file_format::unknown
@ unknown
Definition file_format.hpp:43
osmium::io::file_format::o5m
@ o5m
Definition file_format.hpp:48
osmium::io::file_format::ids
@ ids
Definition file_format.hpp:51
osmium::io::file_compression
file_compression
Definition file_compression.hpp:42
osmium::io::file_compression::bzip2
@ bzip2
Definition file_compression.hpp:45
osmium::io::file_compression::none
@ none
Definition file_compression.hpp:43
osmium::io::file_compression::gzip
@ gzip
Definition file_compression.hpp:44
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:654
options.hpp
osmium::io_error
Definition error.hpp:46