1 #ifndef OSMIUM_IO_FILE_HPP 2 #define OSMIUM_IO_FILE_HPP 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);
59 while (std::getline(ss, item, delim)) {
60 result.push_back(item);
72 class File :
public osmium::Options {
76 std::string m_filename{};
78 const char* m_buffer =
nullptr;
79 size_t m_buffer_size = 0;
87 bool m_has_multiple_object_versions =
false;
103 explicit File(std::string filename =
"", std::string format =
"") :
104 m_filename(
std::move(filename)),
105 m_format_string(
std::move(format)) {
108 if (m_filename ==
"-") {
113 const std::string protocol{m_filename.substr(0, m_filename.find_first_of(
':'))};
114 if (protocol ==
"http" || protocol ==
"https") {
118 if (m_format_string.empty()) {
119 detect_format_from_suffix(m_filename);
121 parse_format(m_format_string);
134 explicit File(
const char* buffer,
size_t size,
const std::string& format =
"") :
137 m_format_string(format) {
138 if (!format.empty()) {
139 parse_format(format);
148 return m_buffer_size;
152 std::vector<std::string> options = detail::split(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());
161 for (
auto& option : options) {
162 const size_t pos = option.find_first_of(
'=');
163 if (pos == std::string::npos) {
166 std::string value{option.substr(pos + 1)};
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;
180 std::vector<std::string> suffixes = detail::split(name,
'.');
182 if (suffixes.empty()) {
188 if (suffixes.back() ==
"gz") {
191 }
else if (suffixes.back() ==
"bz2") {
196 if (suffixes.empty()) {
202 if (suffixes.back() ==
"pbf") {
205 }
else if (suffixes.back() ==
"xml") {
208 }
else if (suffixes.back() ==
"opl") {
211 }
else if (suffixes.back() ==
"json") {
214 }
else if (suffixes.back() ==
"o5m") {
217 }
else if (suffixes.back() ==
"o5c") {
219 m_has_multiple_object_versions =
true;
220 set(
"o5c_change_format",
true);
222 }
else if (suffixes.back() ==
"debug") {
225 }
else if (suffixes.back() ==
"blackhole") {
230 if (suffixes.empty()) {
234 if (suffixes.back() ==
"osm") {
239 }
else if (suffixes.back() ==
"osh") {
243 m_has_multiple_object_versions =
true;
245 }
else if (suffixes.back() ==
"osc") {
249 m_has_multiple_object_versions =
true;
250 set(
"xml_change_format",
true);
263 std::string msg{
"Could not detect file format"};
264 if (!m_format_string.empty()) {
265 msg +=
" from format string '";
266 msg += m_format_string;
269 if (m_filename.empty()) {
270 msg +=
" for stdin/stdout";
272 msg +=
" for filename '";
283 return m_file_format;
287 m_file_format = format;
292 return m_file_compression;
296 m_file_compression = compression;
301 return m_has_multiple_object_versions;
305 m_has_multiple_object_versions = value;
310 if (filename ==
"-") {
313 m_filename = filename;
328 #endif // OSMIUM_IO_FILE_HPP File & set_has_multiple_object_versions(bool value) noexcept
Definition: file.hpp:304
void parse_format(const std::string &format)
Definition: file.hpp:151
File(const char *buffer, size_t size, const std::string &format="")
Definition: file.hpp:134
bool has_multiple_object_versions() const noexcept
Definition: file.hpp:300
Definition: location.hpp:550
void detect_format_from_suffix(const std::string &name)
Definition: file.hpp:179
size_t buffer_size() const noexcept
Definition: file.hpp:147
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
File & set_compression(file_compression compression) noexcept
Definition: file.hpp:295
std::string m_format_string
Definition: file.hpp:81
file_format format() const noexcept
Definition: file.hpp:282
file_compression
Definition: file_compression.hpp:42
File & set_format(file_format format) noexcept
Definition: file.hpp:286
File(std::string filename="", std::string format="")
Definition: file.hpp:103
file_format
Definition: file_format.hpp:42
const char * buffer() const noexcept
Definition: file.hpp:143
const File & check() const
Definition: file.hpp:261
file_compression compression() const noexcept
Definition: file.hpp:291
File & filename(const std::string &filename)
Definition: file.hpp:309
const std::string & filename() const noexcept
Definition: file.hpp:318