Libosmium  2.13.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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <cassert>
37 #include <cctype>
38 #include <cstdlib>
39 #include <limits>
40 #include <stdexcept>
41 #include <string>
42 #include <utility>
43 
45 #include <osmium/osm/item_type.hpp>
46 #include <osmium/osm/types.hpp>
47 #include <osmium/util/cast.hpp>
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;
64  const auto id = std::strtoll(input, &end, 10);
65  if (id != std::numeric_limits<long long>::min() &&
66  id != std::numeric_limits<long long>::max() &&
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 unsigned long string_to_ulong(const char* input, const char* name) {
111  if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
112  char* end;
113  const auto value = std::strtoul(input, &end, 10);
114  if (value != std::numeric_limits<unsigned long>::max() && *end == '\0') {
115  return value;
116  }
117  }
118  throw std::range_error{std::string{"illegal "} + name + ": '" + input + "'"};
119  }
120 
121  } // namespace detail
122 
132  inline object_version_type string_to_object_version(const char* input) {
133  assert(input);
134  return static_cast_with_assert<object_version_type>(detail::string_to_ulong(input, "version"));
135  }
136 
146  inline changeset_id_type string_to_changeset_id(const char* input) {
147  assert(input);
148  return static_cast_with_assert<changeset_id_type>(detail::string_to_ulong(input, "changeset"));
149  }
150 
160  inline signed_user_id_type string_to_user_id(const char* input) {
161  assert(input);
162  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
163  return -1;
164  }
165  return static_cast_with_assert<signed_user_id_type>(detail::string_to_ulong(input, "user id"));
166  }
167 
177  inline num_changes_type string_to_num_changes(const char* input) {
178  assert(input);
179  return static_cast_with_assert<num_changes_type>(detail::string_to_ulong(input, "value for num changes"));
180  }
181 
191  inline num_comments_type string_to_num_comments(const char* input) {
192  assert(input);
193  return static_cast_with_assert<num_comments_type>(detail::string_to_ulong(input, "value for num comments"));
194  }
195 
196 } // namespace osmium
197 
198 #endif // OSMIUM_OSM_TYPES_FROM_STRING_HPP
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
type
Definition: entity_bits.hpp:63
item_type
Definition: item_type.hpp:43
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:132
type from_item_type(osmium::item_type item_type) noexcept
Definition: entity_bits.hpp:108
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:60
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:146
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:160
item_type char_to_item_type(const char c) noexcept
Definition: item_type.hpp:88
Definition: entity_bits.hpp:67
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:191
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:177