1 #ifndef OSMIUM_OSM_TAG_HPP
2 #define OSMIUM_OSM_TAG_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2020 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 <osmium/memory/collection.hpp>
37 #include <osmium/memory/item.hpp>
38 #include <osmium/osm/item_type.hpp>
39 
40 #include <algorithm>
41 #include <cassert>
42 #include <cstring>
43 #include <iosfwd>
44 #include <iterator>
45 
46 namespace osmium {
47 
48     class Tag : public osmium::memory::detail::ItemHelper {
49 
50         template <typename TMember>
51         friend class osmium::memory::CollectionIterator;
52 
after_null(unsigned char * ptr)53         static unsigned char* after_null(unsigned char* ptr) noexcept {
54             return reinterpret_cast<unsigned char*>(std::strchr(reinterpret_cast<char*>(ptr), 0) + 1);
55         }
56 
after_null(const unsigned char * ptr)57         static const unsigned char* after_null(const unsigned char* ptr) noexcept {
58             return reinterpret_cast<const unsigned char*>(std::strchr(reinterpret_cast<const char*>(ptr), 0) + 1);
59         }
60 
next()61         unsigned char* next() noexcept {
62             return after_null(after_null(data()));
63         }
64 
next() const65         const unsigned char* next() const noexcept {
66             return after_null(after_null(data()));
67         }
68 
69     public:
70 
71         Tag(const Tag&) = delete;
72         Tag& operator=(const Tag&) = delete;
73 
74         Tag(Tag&&) = delete;
75         Tag& operator=(Tag&&) = delete;
76 
77         ~Tag() noexcept = default;
78 
79         static constexpr item_type collection_type = item_type::tag_list;
80 
81         /**
82          * Get a pointer to the C string containing the tag key.
83          *
84          * Complexity: Constant.
85          */
key() const86         const char* key() const noexcept {
87             return reinterpret_cast<const char*>(data());
88         }
89 
90         /**
91          * Get a pointer to the C string containing the tag value.
92          *
93          * Complexity: Linear on the number of characters in the key!
94          */
value() const95         const char* value() const noexcept {
96             return reinterpret_cast<const char*>(after_null(data()));
97         }
98 
99     }; // class Tag
100 
operator ==(const Tag & lhs,const Tag & rhs)101     inline bool operator==(const Tag& lhs, const Tag& rhs) noexcept {
102         return !std::strcmp(lhs.key(), rhs.key()) &&
103                !std::strcmp(lhs.value(), rhs.value());
104     }
105 
operator <(const Tag & lhs,const Tag & rhs)106     inline bool operator<(const Tag& lhs, const Tag& rhs) noexcept {
107         const auto c = std::strcmp(lhs.key(), rhs.key());
108         return (c == 0 ? std::strcmp(lhs.value(), rhs.value()) : c) < 0;
109     }
110 
111     /**
112      * Output a Tag to a stream.
113      */
114     template <typename TChar, typename TTraits>
operator <<(std::basic_ostream<TChar,TTraits> & out,const Tag & tag)115     inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
116         return out << tag.key() << '=' << tag.value();
117     }
118 
119     class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
120 
find_key(const char * key) const121         const_iterator find_key(const char* key) const noexcept {
122             return std::find_if(cbegin(), cend(), [key](const Tag& tag) {
123                 return !std::strcmp(tag.key(), key);
124             });
125         }
126 
127     public:
128 
129         TagList() noexcept = default;
130 
131         /**
132          * Get tag value for the given tag key. If the key is not set, returns
133          * the default_value.
134          *
135          * @pre @code key != nullptr @endcode
136          */
get_value_by_key(const char * key,const char * default_value=nullptr) const137         const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
138             assert(key);
139             const auto result = find_key(key);
140             return result == cend() ? default_value : result->value();
141         }
142 
143         /**
144          * Get tag value for the given tag key. If the key is not set, returns
145          * nullptr.
146          *
147          * @pre @code key != nullptr @endcode
148          */
operator [](const char * key) const149         const char* operator[](const char* key) const noexcept {
150             return get_value_by_key(key);
151         }
152 
153         /**
154          * Returns true if the tag with the given key is in the tag list.
155          *
156          * @pre @code key != nullptr @endcode
157          */
has_key(const char * key) const158         bool has_key(const char* key) const noexcept {
159             assert(key);
160             return find_key(key) != cend();
161         }
162 
163         /**
164          * Returns true if the tag with the given key and value is in the
165          * tag list.
166          *
167          * @pre @code key != nullptr && value != nullptr @endcode
168          */
has_tag(const char * key,const char * value) const169         bool has_tag(const char* key, const char* value) const noexcept {
170             assert(key);
171             assert(value);
172             const auto result = find_key(key);
173             return result != cend() && !std::strcmp(result->value(), value);
174         }
175 
176     }; // class TagList
177 
178     static_assert(sizeof(TagList) % osmium::memory::align_bytes == 0, "Class osmium::TagList has wrong size to be aligned properly!");
179 
180 } // namespace osmium
181 
182 #endif // OSMIUM_OSM_TAG_HPP
183