Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2015 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 
47 namespace osmium {
48 
49  inline object_id_type string_to_object_id(const char* input) {
50  assert(input);
51  if (*input != '\0' && !std::isspace(*input)) {
52  char* end;
53  auto id = std::strtoll(input, &end, 10);
54  if (id != std::numeric_limits<long long>::min() && id != std::numeric_limits<long long>::max() && *end == '\0') {
55  return id;
56  }
57  }
58  throw std::range_error(std::string("illegal id: '") + input + "'");
59  }
60 
61  inline std::pair<osmium::item_type, osmium::object_id_type> string_to_object_id(const char* input, osmium::osm_entity_bits::type types) {
62  assert(input);
63  assert(types != osmium::osm_entity_bits::nothing);
64  if (*input != '\0') {
65  if (std::isdigit(*input)) {
66  return std::make_pair(osmium::item_type::undefined, string_to_object_id(input));
67  }
70  return std::make_pair(t, string_to_object_id(input+1));
71  }
72  }
73  throw std::range_error(std::string("not a valid id: '") + input + "'");
74  }
75 
76  namespace detail {
77 
78  inline long string_to_ulong(const char* input, const char *name) {
79  if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
80  char* end;
81  auto value = std::strtoul(input, &end, 10);
82  if (value != std::numeric_limits<unsigned long>::max() && *end == '\0') {
83  return value;
84  }
85  }
86  throw std::range_error(std::string("illegal ") + name + ": '" + input + "'");
87  }
88 
89  } // namespace detail
90 
91  inline object_version_type string_to_object_version(const char* input) {
92  assert(input);
93  return static_cast<object_version_type>(detail::string_to_ulong(input, "version"));
94  }
95 
96  inline changeset_id_type string_to_changeset_id(const char* input) {
97  assert(input);
98  return static_cast<changeset_id_type>(detail::string_to_ulong(input, "changeset"));
99  }
100 
101  inline signed_user_id_type string_to_user_id(const char* input) {
102  assert(input);
103  if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
104  return -1;
105  }
106  return static_cast<signed_user_id_type>(detail::string_to_ulong(input, "user id"));
107  }
108 
109  inline num_changes_type string_to_num_changes(const char* input) {
110  assert(input);
111  return static_cast<num_changes_type>(detail::string_to_ulong(input, "value for num changes"));
112  }
113 
114 } // namespace osmium
115 
116 #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:91
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:49
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:55
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:96
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:101
item_type char_to_item_type(const char c) noexcept
Definition: item_type.hpp:79
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
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:109