Libosmium  2.17.1
Fast and flexible C++ library for working with OpenStreetMap data
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 
37 #include <osmium/osm/item_type.hpp>
38 #include <osmium/osm/types.hpp>
40 
41 #include <cassert>
42 #include <cctype>
43 #include <cstdlib>
44 #include <limits>
45 #include <stdexcept>
46 #include <string>
47 #include <utility>
48 
49 namespace osmium {
50 
60  inline object_id_type string_to_object_id(const char* input) {
61  assert(input);
62  if (*input != '\0' && !std::isspace(*input)) {
63  char* end = nullptr;
64  const auto id = std::strtoll(input, &end, 10);
65  if (id != std::numeric_limits<long long>::min() && // NOLINT(google-runtime-int)
66  id != std::numeric_limits<long long>::max() && // NOLINT(google-runtime-int)
67  *end == '\0') {
68  return id;
69  }
70  }
71  throw std::range_error{std::string{"illegal id: '"} + input + "'"};
72  }
73 
90  inline std::pair<osmium::item_type, osmium::object_id_type>
91  string_to_object_id(const char* input,
94  assert(input);
95  assert(types != osmium::osm_entity_bits::nothing);
96  if (*input != '\0') {
97  if (std::isdigit(*input)) {
98  return std::make_pair(default_type, string_to_object_id(input));
99  }
102  return std::make_pair(t, string_to_object_id(input + 1));
103  }
104  }
105  throw std::range_error{std::string{"not a valid id: '"} + input + "'"};
106  }
107 
108  namespace detail {
109 
110  inline uint32_t string_to_ulong(const char* input, const char* name) {
111  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
112  return 0;
113  }
114  if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
115  char* end = nullptr;
116  const auto value = std::strtoul(input, &end, 10);
117  if (value < std::numeric_limits<uint32_t>::max() && *end == '\0') {
118  return static_cast<uint32_t>(value);
119  }
120  }
121  throw std::range_error{std::string{"illegal "} + name + ": '" + input + "'"};
122  }
123 
124  } // namespace detail
125 
135  inline object_version_type string_to_object_version(const char* input) {
136  assert(input);
137  return detail::string_to_ulong(input, "version");
138  }
139 
149  inline changeset_id_type string_to_changeset_id(const char* input) {
150  assert(input);
151  return detail::string_to_ulong(input, "changeset");
152  }
153 
166  assert(input);
167  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
168  return -1;
169  }
170  const auto value = detail::string_to_ulong(input, "user id");
171  if (value > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) {
172  throw std::range_error{"illegal user id"};
173  }
174  return static_cast<signed_user_id_type>(value);
175  }
176 
186  inline user_id_type string_to_uid(const char* input) {
187  assert(input);
188  return detail::string_to_ulong(input, "user id");
189  }
190 
200  inline num_changes_type string_to_num_changes(const char* input) {
201  assert(input);
202  return detail::string_to_ulong(input, "value for num changes");
203  }
204 
214  inline num_comments_type string_to_num_comments(const char* input) {
215  assert(input);
216  return detail::string_to_ulong(input, "value for num comments");
217  }
218 
219 } // namespace osmium
220 
221 #endif // OSMIUM_OSM_TYPES_FROM_STRING_HPP
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
Definition: attr.hpp:342
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
type
Definition: entity_bits.hpp:63
@ nothing
Definition: entity_bits.hpp:67
type from_item_type(osmium::item_type item_type) noexcept
Definition: entity_bits.hpp:108
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:149
user_id_type string_to_uid(const char *input)
Definition: types_from_string.hpp:186
item_type char_to_item_type(const char c) noexcept
Definition: item_type.hpp:90
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:60
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:214
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
OSMIUM_DEPRECATED signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:165
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
item_type
Definition: item_type.hpp:45
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:135
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:200
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51