Libosmium: include/osmium/osm/types_from_string.hpp Source File

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

Loading...
Searching...
No Matches
types_from_string.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_OSM_TYPES_FROM_STRING_HPP
2#define OSMIUM_OSM_TYPES_FROM_STRING_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 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/osm/entity_bits.hpp>
37#include <osmium/osm/item_type.hpp>
38#include <osmium/osm/types.hpp>
39
40#include <cassert>
41#include <cctype>
42#include <cstdlib>
43#include <limits>
44#include <stdexcept>
45#include <string>
46#include <utility>
47
48namespace osmium {
49
59 inline object_id_type string_to_object_id(const char* input) {
60 assert(input);
61 if (*input != '\0' && !std::isspace(*input)) {
62 char* end = nullptr;
63 const auto id = std::strtoll(input, &end, 10);
64 if (id != std::numeric_limits<long long>::min() && // NOLINT(google-runtime-int)
65 id != std::numeric_limits<long long>::max() && // NOLINT(google-runtime-int)
66 *end == '\0') {
67 return id;
68 }
69 }
70 throw std::range_error{std::string{"illegal id: '"} + input + "'"};
71 }
72
89 inline std::pair<osmium::item_type, osmium::object_id_type>
90 string_to_object_id(const char* input,
91 osmium::osm_entity_bits::type types,
92 osmium::item_type default_type = osmium::item_type::undefined) {
93 assert(input);
94 assert(types != osmium::osm_entity_bits::nothing);
95 if (*input != '\0') {
96 if (std::isdigit(*input)) {
97 return std::make_pair(default_type, string_to_object_id(input));
98 }
99 const osmium::item_type t = osmium::char_to_item_type(*input);
100 if (osmium::osm_entity_bits::from_item_type(t) & types) {
101 return std::make_pair(t, string_to_object_id(input + 1));
102 }
103 }
104 throw std::range_error{std::string{"not a valid id: '"} + input + "'"};
105 }
106
107 namespace detail {
108
109 inline uint32_t string_to_ulong(const char* input, const char* name) {
110 if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
111 return 0;
112 }
113 if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
114 char* end = nullptr;
115 const auto value = std::strtoul(input, &end, 10);
116 if (value < std::numeric_limits<uint32_t>::max() && *end == '\0') {
117 return static_cast<uint32_t>(value);
118 }
119 }
120 throw std::range_error{std::string{"illegal "} + name + ": '" + input + "'"};
121 }
122
123 } // namespace detail
124
134 inline object_version_type string_to_object_version(const char* input) {
135 assert(input);
136 return detail::string_to_ulong(input, "version");
137 }
138
148 inline changeset_id_type string_to_changeset_id(const char* input) {
149 assert(input);
150 return detail::string_to_ulong(input, "changeset");
151 }
152
162 inline user_id_type string_to_uid(const char* input) {
163 assert(input);
164 return detail::string_to_ulong(input, "user id");
165 }
166
176 inline num_changes_type string_to_num_changes(const char* input) {
177 assert(input);
178 return detail::string_to_ulong(input, "value for num changes");
179 }
180
190 inline num_comments_type string_to_num_comments(const char* input) {
191 assert(input);
192 return detail::string_to_ulong(input, "value for num comments");
193 }
194
195} // namespace osmium
196
197#endif // OSMIUM_OSM_TYPES_FROM_STRING_HPP
entity_bits.hpp
item_type.hpp
detail
Definition attr.hpp:342
osmium::io::end
InputIterator< Reader > end(Reader &)
Definition reader_iterator.hpp:47
osmium::osm_entity_bits::type
type
Definition entity_bits.hpp:65
osmium::osm_entity_bits::nothing
@ nothing
Definition entity_bits.hpp:69
osmium::osm_entity_bits::from_item_type
type from_item_type(osmium::item_type item_type) noexcept
Definition entity_bits.hpp:119
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::string_to_changeset_id
changeset_id_type string_to_changeset_id(const char *input)
Definition types_from_string.hpp:148
osmium::string_to_uid
user_id_type string_to_uid(const char *input)
Definition types_from_string.hpp:162
osmium::char_to_item_type
item_type char_to_item_type(const char c) noexcept
Definition item_type.hpp:90
osmium::string_to_object_id
object_id_type string_to_object_id(const char *input)
Definition types_from_string.hpp:59
osmium::string_to_num_comments
num_comments_type string_to_num_comments(const char *input)
Definition types_from_string.hpp:190
osmium::changeset_id_type
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition types.hpp:48
osmium::num_changes_type
uint32_t num_changes_type
Type for changeset num_changes.
Definition types.hpp:51
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::undefined
@ undefined
Definition item_type.hpp:47
osmium::string_to_object_version
object_version_type string_to_object_version(const char *input)
Definition types_from_string.hpp:134
osmium::num_comments_type
uint32_t num_comments_type
Type for changeset num_comments.
Definition types.hpp:52
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition types.hpp:45
osmium::object_version_type
uint32_t object_version_type
Type for OSM object version number.
Definition types.hpp:47
osmium::string_to_num_changes
num_changes_type string_to_num_changes(const char *input)
Definition types_from_string.hpp:176
osmium::user_id_type
uint32_t user_id_type
Type for OSM user IDs.
Definition types.hpp:49
types.hpp