Libosmium  2.11.0
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  auto id = std::strtoll(input, &end, 10);
65  if (id != std::numeric_limits<long long>::min() && id != std::numeric_limits<long long>::max() && *end == '\0') {
66  return id;
67  }
68  }
69  throw std::range_error(std::string("illegal id: '") + input + "'");
70  }
71 
88  inline std::pair<osmium::item_type, osmium::object_id_type>
89  string_to_object_id(const char* input,
92  assert(input);
93  assert(types != osmium::osm_entity_bits::nothing);
94  if (*input != '\0') {
95  if (std::isdigit(*input)) {
96  return std::make_pair(default_type, string_to_object_id(input));
97  }
100  return std::make_pair(t, string_to_object_id(input + 1));
101  }
102  }
103  throw std::range_error(std::string("not a valid id: '") + input + "'");
104  }
105 
106  namespace detail {
107 
108  inline unsigned long string_to_ulong(const char* input, const char* name) {
109  if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
110  char* end;
111  auto value = std::strtoul(input, &end, 10);
112  if (value != std::numeric_limits<unsigned long>::max() && *end == '\0') {
113  return value;
114  }
115  }
116  throw std::range_error(std::string("illegal ") + name + ": '" + input + "'");
117  }
118 
119  } // namespace detail
120 
130  inline object_version_type string_to_object_version(const char* input) {
131  assert(input);
132  return static_cast_with_assert<object_version_type>(detail::string_to_ulong(input, "version"));
133  }
134 
144  inline changeset_id_type string_to_changeset_id(const char* input) {
145  assert(input);
146  return static_cast_with_assert<changeset_id_type>(detail::string_to_ulong(input, "changeset"));
147  }
148 
158  inline signed_user_id_type string_to_user_id(const char* input) {
159  assert(input);
160  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
161  return -1;
162  }
163  return static_cast_with_assert<signed_user_id_type>(detail::string_to_ulong(input, "user id"));
164  }
165 
175  inline num_changes_type string_to_num_changes(const char* input) {
176  assert(input);
177  return static_cast_with_assert<num_changes_type>(detail::string_to_ulong(input, "value for num changes"));
178  }
179 
189  inline num_comments_type string_to_num_comments(const char* input) {
190  assert(input);
191  return static_cast_with_assert<num_comments_type>(detail::string_to_ulong(input, "value for num comments"));
192  }
193 
194 } // namespace osmium
195 
196 #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:130
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:73
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:144
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:158
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:189
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:175