types_from_string.hpp
Go to the documentation of this file.
#ifndef OSMIUM_OSM_TYPES_FROM_STRING_HPP
#define OSMIUM_OSM_TYPES_FROM_STRING_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <string>
#include <utility>
#include <osmium/osm/entity_bits.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/util/cast.hpp>
namespace osmium {
inline object_id_type string_to_object_id(const char* input) {
assert(input);
if (*input != '\0' && !std::isspace(*input)) {
char* end;
auto id = std::strtoll(input, &end, 10);
if (id != std::numeric_limits<long long>::min() && id != std::numeric_limits<long long>::max() && *end == '\0') {
return id;
}
}
throw std::range_error(std::string("illegal id: '") + input + "'");
}
inline std::pair<osmium::item_type, osmium::object_id_type> string_to_object_id(const char* input, osmium::osm_entity_bits::type types) {
assert(input);
assert(types != osmium::osm_entity_bits::nothing);
if (*input != '\0') {
if (std::isdigit(*input)) {
return std::make_pair(osmium::item_type::undefined, string_to_object_id(input));
}
osmium::item_type t = osmium::char_to_item_type(*input);
if (osmium::osm_entity_bits::from_item_type(t) & types) {
return std::make_pair(t, string_to_object_id(input+1));
}
}
throw std::range_error(std::string("not a valid id: '") + input + "'");
}
namespace detail {
inline unsigned long string_to_ulong(const char* input, const char *name) {
if (*input != '\0' && *input != '-' && !std::isspace(*input)) {
char* end;
auto value = std::strtoul(input, &end, 10);
if (value != std::numeric_limits<unsigned long>::max() && *end == '\0') {
return value;
}
}
throw std::range_error(std::string("illegal ") + name + ": '" + input + "'");
}
} // namespace detail
inline object_version_type string_to_object_version(const char* input) {
assert(input);
return static_cast_with_assert<object_version_type>(detail::string_to_ulong(input, "version"));
}
inline changeset_id_type string_to_changeset_id(const char* input) {
assert(input);
return static_cast_with_assert<changeset_id_type>(detail::string_to_ulong(input, "changeset"));
}
inline signed_user_id_type string_to_user_id(const char* input) {
assert(input);
if (input[0] == '-' && input[1] == '1' && input[2] == '\0') {
return -1;
}
return static_cast_with_assert<signed_user_id_type>(detail::string_to_ulong(input, "user id"));
}
inline num_changes_type string_to_num_changes(const char* input) {
assert(input);
return static_cast_with_assert<num_changes_type>(detail::string_to_ulong(input, "value for num changes"));
}
inline num_comments_type string_to_num_comments(const char* input) {
assert(input);
return static_cast_with_assert<num_comments_type>(detail::string_to_ulong(input, "value for num comments"));
}
} // namespace osmium
#endif // OSMIUM_OSM_TYPES_FROM_STRING_HPP
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:60
entity_bits.hpp
osmium::item_type
item_type
Definition: item_type.hpp:43
types.hpp
osmium::string_to_object_version
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:123
osmium::osm_entity_bits::from_item_type
type from_item_type(osmium::item_type item_type) noexcept
Definition: entity_bits.hpp:97
osmium::item_type::undefined
osmium::string_to_object_id
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:59
osmium::object_id_type
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
detail
Definition: attr.hpp:298
std::end
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
osmium::string_to_changeset_id
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:137
osmium::signed_user_id_type
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
cast.hpp
osmium::string_to_user_id
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:151
osmium::char_to_item_type
item_type char_to_item_type(const char c) noexcept
Definition: item_type.hpp:88
osmium::osm_entity_bits::nothing
Definition: entity_bits.hpp:62
osmium::object_version_type
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
osmium::num_changes_type
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
osmium::changeset_id_type
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
osmium::num_comments_type
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
osmium::string_to_num_comments
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:182
osmium::string_to_num_changes
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:168