Libosmium  2.6.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-2016 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 <cstdint>
39 #include <cstdlib>
40 #include <limits>
41 #include <string>
42 #include <utility>
43 
45 #include <osmium/osm/types.hpp>
46 #include <osmium/util/cast.hpp>
47 
48 namespace 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;
63  auto id = std::strtoll(input, &end, 10);
64  if (id != std::numeric_limits<long long>::min() && id != std::numeric_limits<long long>::max() && *end == '\0') {
65  return id;
66  }
67  }
68  throw std::range_error(std::string("illegal id: '") + input + "'");
69  }
70 
84  inline std::pair<osmium::item_type, osmium::object_id_type> string_to_object_id(const char* input, osmium::osm_entity_bits::type types) {
85  assert(input);
86  assert(types != osmium::osm_entity_bits::nothing);
87  if (*input != '\0') {
88  if (std::isdigit(*input)) {
89  return std::make_pair(osmium::item_type::undefined, string_to_object_id(input));
90  }
93  return std::make_pair(t, string_to_object_id(input+1));
94  }
95  }
96  throw std::range_error(std::string("not a valid id: '") + input + "'");
97  }
98 
99  namespace detail {
100 
101  inline unsigned long string_to_ulong(const char* input, const char *name) {
102  if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
103  char* end;
104  auto value = std::strtoul(input, &end, 10);
105  if (value != std::numeric_limits<unsigned long>::max() && *end == '\0') {
106  return value;
107  }
108  }
109  throw std::range_error(std::string("illegal ") + name + ": '" + input + "'");
110  }
111 
112  } // namespace detail
113 
123  inline object_version_type string_to_object_version(const char* input) {
124  assert(input);
125  return static_cast_with_assert<object_version_type>(detail::string_to_ulong(input, "version"));
126  }
127 
137  inline changeset_id_type string_to_changeset_id(const char* input) {
138  assert(input);
139  return static_cast_with_assert<changeset_id_type>(detail::string_to_ulong(input, "changeset"));
140  }
141 
151  inline signed_user_id_type string_to_user_id(const char* input) {
152  assert(input);
153  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
154  return -1;
155  }
156  return static_cast_with_assert<signed_user_id_type>(detail::string_to_ulong(input, "user id"));
157  }
158 
168  inline num_changes_type string_to_num_changes(const char* input) {
169  assert(input);
170  return static_cast_with_assert<num_changes_type>(detail::string_to_ulong(input, "value for num changes"));
171  }
172 
182  inline num_comments_type string_to_num_comments(const char* input) {
183  assert(input);
184  return static_cast_with_assert<num_comments_type>(detail::string_to_ulong(input, "value for num comments"));
185  }
186 
187 } // namespace osmium
188 
189 #endif // OSMIUM_OSM_TYPES_FROM_STRING_HPP
type
Definition: entity_bits.hpp:60
item_type
Definition: item_type.hpp:43
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:123
type from_item_type(osmium::item_type item_type) noexcept
Definition: entity_bits.hpp:97
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:59
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: attr.hpp:298
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:137
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:151
item_type char_to_item_type(const char c) noexcept
Definition: item_type.hpp:88
Definition: entity_bits.hpp:62
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:182
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:168