Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 13 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 14 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef NDN_NAME_COMPONENT_HPP |
| 18 | #define NDN_NAME_COMPONENT_HPP |
| 19 | |
| 20 | #include "common.hpp" |
| 21 | #include "encoding/block.hpp" |
| 22 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 23 | #include "util/string-helper.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace name { |
| 27 | |
| 28 | /** |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 29 | * @brief Component holds a read-only name component value. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 30 | */ |
| 31 | class Component : public Block |
| 32 | { |
| 33 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 35 | * @brief Error that can be thrown from name::Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 37 | class Error : public Block::Error |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | explicit |
| 41 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 42 | : Block::Error(what) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 47 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 48 | * Create a new name::Component with an empty value |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 49 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 50 | Component(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 52 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 53 | * @brief Create name::Component from a wire block |
| 54 | * |
| 55 | * @param wire Tlv::NameComponent Block from which to create name::Component |
| 56 | * @throws Error if wire.type() is not Tlv::NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 57 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 58 | * Any block can be implicitly converted to name::Component |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 59 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 60 | Component(const Block& wire); |
| 61 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 62 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 63 | * @brief Create a new name::Component from the buffer pointer (buffer pointer will be copied) |
| 64 | * |
| 65 | * @param buffer A pointer to an immutable buffer |
| 66 | * |
| 67 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 68 | * Note that this method **will not** allocate new memory for and copy the payload until |
| 69 | * toWire() method is called. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 70 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 72 | Component(const ConstBufferPtr& buffer); |
| 73 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 74 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 75 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 76 | * @param buffer A reference to the buffer |
| 77 | * |
| 78 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 79 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | explicit |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 82 | Component(const Buffer& buffer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 85 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 86 | * @param buffer A pointer to the first byte of the buffer |
| 87 | * @param bufferSize Size of the buffer |
| 88 | * |
| 89 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 90 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 91 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 92 | Component(const uint8_t* buffer, size_t bufferSize); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 94 | /** |
| 95 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 96 | * @param begin Iterator pointing to the beginning of the buffer |
| 97 | * @param end Iterator pointing to the ending of the buffer |
| 98 | * |
| 99 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 100 | * Note that this method **will** allocate new memory for and copy the payload. |
| 101 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 102 | template<class InputIterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 103 | Component(InputIterator begin, InputIterator end); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 105 | /** |
| 106 | * @brief Create a new name::Component from the C string (data from string will be copied) |
| 107 | * |
| 108 | * @param str Zero-ended string. Note that this string will be interpreted as is (i.e., |
| 109 | * it will not be interpreted as URI) |
| 110 | * |
| 111 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 112 | * Note that this method **will** allocate new memory for and copy the payload. |
| 113 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 114 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 115 | Component(const char* str); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 117 | /** |
| 118 | * @brief Create a new name::Component from the STL string (data from string will be copied) |
| 119 | * |
| 120 | * @param str Const reference to STL string. Note that this string will be interpreted |
| 121 | * as is (i.e., it will not be interpreted as URI) |
| 122 | * |
| 123 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 124 | * Note that this method **will** allocate new memory for and copy the payload. |
| 125 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 126 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 127 | Component(const std::string& str); |
| 128 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 129 | /** |
| 130 | * @brief Fast encoding or block size estimation |
| 131 | */ |
| 132 | template<bool T> |
| 133 | size_t |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 134 | wireEncode(EncodingImpl<T>& block) const; |
| 135 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 136 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | * @brief Encode to a wire format |
| 138 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 139 | const Block& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 140 | wireEncode() const; |
| 141 | |
| 142 | /** |
| 143 | * @brief Decode from the wire format |
| 144 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 145 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | wireDecode(const Block& wire); |
| 147 | |
| 148 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 149 | * @brief Create name::Component by decoding the escapedString between beginOffset and |
| 150 | * endOffset according to the NDN URI Scheme. |
| 151 | * |
| 152 | * If the escaped string is "", "." or ".." then return an empty name::Component. Note |
| 153 | * that an empty name::Component should not be added to Name and if attempted, an |
| 154 | * exception will be thrown. |
| 155 | * |
| 156 | * @param escapedString String containing NDN URI-encoded name |
| 157 | * component. [escapedString+beginOffset, beginOffset+endOffset) |
| 158 | * must be a valid memory buffer. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 159 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 160 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 161 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 162 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 163 | fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 166 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 167 | * |
| 168 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 169 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 170 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 171 | fromEscapedString(const char* escapedString) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 172 | { |
| 173 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 174 | } |
| 175 | |
| 176 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 177 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 178 | * |
| 179 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 180 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 181 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 182 | fromEscapedString(const std::string& escapedString) |
| 183 | { |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 184 | return fromEscapedString(escapedString.c_str(), 0, escapedString.size()); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 188 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 189 | * |
| 190 | * @deprecated Use toUri(std::ostream&) instead |
| 191 | * |
| 192 | * This also adds "..." to a value with zero or more "." |
| 193 | * |
| 194 | * @param os The output stream to where write the URI escaped version *this |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 195 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 196 | void |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 197 | toEscapedString(std::ostream& os) const; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 199 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 200 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 201 | * |
| 202 | * @deprecated Use toUri() instead |
| 203 | * |
| 204 | * This also adds "..." to a value with zero or more "." |
| 205 | * |
| 206 | * @return The escaped string |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 207 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 208 | std::string |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 209 | toEscapedString() const |
| 210 | { |
| 211 | std::ostringstream result; |
| 212 | toEscapedString(result); |
| 213 | return result.str(); |
| 214 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 215 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 216 | /** |
| 217 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 218 | * |
| 219 | * This also adds "..." to a value with zero or more "." |
| 220 | * |
| 221 | * @param os The output stream to where write the URI escaped version *this |
| 222 | */ |
| 223 | void |
| 224 | toUri(std::ostream& os) const |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 226 | return toEscapedString(os); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 229 | /** |
| 230 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 231 | * |
| 232 | * This also adds "..." to a value with zero or more "." |
| 233 | * |
| 234 | * @return The escaped string |
| 235 | */ |
| 236 | std::string |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 237 | toUri() const |
| 238 | { |
| 239 | return toEscapedString(); |
| 240 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 241 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 242 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 243 | * @brief Interpret this name component as nonNegativeInteger |
| 244 | * |
| 245 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 246 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 247 | * @return The integer number. |
| 248 | */ |
| 249 | uint64_t |
| 250 | toNumber() const; |
| 251 | |
| 252 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 253 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 254 | */ |
| 255 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 256 | toVersion() const; |
| 257 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 258 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 259 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 260 | */ |
| 261 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 262 | toSegment() const; |
| 263 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 264 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 265 | * @brief Create a component encoded as nonNegativeInteger |
| 266 | * |
| 267 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 268 | * |
| 269 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 270 | * @return The component value. |
| 271 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 272 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 273 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 274 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 275 | bool |
| 276 | empty() const |
| 277 | { |
| 278 | return !hasValue(); |
| 279 | } |
| 280 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 281 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 282 | * @brief Check if this is the same component as other |
| 283 | * |
| 284 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 285 | * @return true if the components are equal, otherwise false. |
| 286 | */ |
| 287 | bool |
| 288 | equals(const Component& other) const |
| 289 | { |
| 290 | if (value_size() != other.value_size()) |
| 291 | return false; |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 292 | if (value_size() == 0 /* == other.value_size()*/) |
| 293 | return true; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 294 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 295 | // somehow, behavior is wrong on OSX 10.9 when component is empty |
| 296 | // (probably some bug in STL...) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 297 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 298 | } |
| 299 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 300 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 301 | * @brief Compare this to the other Component using NDN canonical ordering |
| 302 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 303 | * @param other The other Component to compare with. |
| 304 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 305 | * 1 if *this comes after other in the canonical ordering. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 306 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 307 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 308 | */ |
| 309 | int |
| 310 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 311 | |
| 312 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 313 | * @brief Check if this is the same component as other |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 314 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 315 | * @param other The other Component to compare with. |
| 316 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 317 | */ |
| 318 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 319 | operator==(const Component& other) const |
| 320 | { |
| 321 | return equals(other); |
| 322 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 323 | |
| 324 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 325 | * @brief Check if this is not the same component as other |
| 326 | * @param other The other Component to compare with |
| 327 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 328 | */ |
| 329 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 330 | operator!=(const Component& other) const |
| 331 | { |
| 332 | return !equals(other); |
| 333 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 334 | |
| 335 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 336 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 337 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 338 | * |
| 339 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 340 | */ |
| 341 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 342 | operator<=(const Component& other) const |
| 343 | { |
| 344 | return compare(other) <= 0; |
| 345 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 346 | |
| 347 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 348 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 349 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 350 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 351 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 352 | */ |
| 353 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 354 | operator<(const Component& other) const |
| 355 | { |
| 356 | return compare(other) < 0; |
| 357 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 358 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 359 | /** |
| 360 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 361 | * @param other The other Component to compare with |
| 362 | * |
| 363 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 364 | */ |
| 365 | bool |
| 366 | operator>=(const Component& other) const |
| 367 | { |
| 368 | return compare(other) >= 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 373 | * @param other The other Component to compare with |
| 374 | * |
| 375 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 376 | */ |
| 377 | bool |
| 378 | operator>(const Component& other) const |
| 379 | { |
| 380 | return compare(other) > 0; |
| 381 | } |
| 382 | |
| 383 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 384 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 385 | // This class MUST NOT contain any data fields. |
| 386 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 387 | }; |
| 388 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 389 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 390 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 391 | { |
| 392 | component.toEscapedString(os); |
| 393 | return os; |
| 394 | } |
| 395 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 396 | inline |
| 397 | Component::Component() |
| 398 | : Block(Tlv::NameComponent) |
| 399 | { |
| 400 | } |
| 401 | |
| 402 | inline |
| 403 | Component::Component(const Block& wire) |
| 404 | : Block(wire) |
| 405 | { |
| 406 | if (type() != Tlv::NameComponent) |
| 407 | throw Error("Constructing name component from non name component TLV wire block"); |
| 408 | } |
| 409 | |
| 410 | inline |
| 411 | Component::Component(const ConstBufferPtr& buffer) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 412 | : Block(Tlv::NameComponent, buffer) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 413 | { |
| 414 | } |
| 415 | |
| 416 | inline |
| 417 | Component::Component(const Buffer& value) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 418 | : Block(dataBlock(Tlv::NameComponent, value.buf(), value.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 419 | { |
| 420 | } |
| 421 | |
| 422 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 423 | Component::Component(const uint8_t* value, size_t valueLen) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 424 | : Block(dataBlock(Tlv::NameComponent, value, valueLen)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 425 | { |
| 426 | } |
| 427 | |
| 428 | template<class InputIterator> |
| 429 | inline |
| 430 | Component::Component(InputIterator begin, InputIterator end) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 431 | : Block(dataBlock(Tlv::NameComponent, begin, end)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 432 | { |
| 433 | } |
| 434 | |
| 435 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 436 | Component::Component(const char* str) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 437 | : Block(dataBlock(Tlv::NameComponent, str, ::strlen(str))) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 438 | { |
| 439 | } |
| 440 | |
| 441 | inline |
| 442 | Component::Component(const std::string& str) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 443 | : Block(dataBlock(Tlv::NameComponent, str.c_str(), str.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 444 | { |
| 445 | } |
| 446 | |
| 447 | |
| 448 | inline Component |
| 449 | Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset) |
| 450 | { |
| 451 | std::string trimmedString(escapedString + beginOffset, escapedString + endOffset); |
| 452 | trim(trimmedString); |
| 453 | std::string value = unescape(trimmedString); |
| 454 | |
| 455 | if (value.find_first_not_of(".") == std::string::npos) { |
| 456 | // Special case for component of only periods. |
| 457 | if (value.size() <= 2) |
| 458 | // Zero, one or two periods is illegal. Ignore this component. |
| 459 | return Component(); |
| 460 | else |
| 461 | // Remove 3 periods. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 462 | return Component(reinterpret_cast<const uint8_t*>(&value[3]), value.size() - 3); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 463 | } |
| 464 | else |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 465 | return Component(reinterpret_cast<const uint8_t*>(&value[0]), value.size()); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
| 469 | inline void |
| 470 | Component::toEscapedString(std::ostream& result) const |
| 471 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 472 | const uint8_t* valuePtr = value(); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 473 | size_t valueSize = value_size(); |
| 474 | |
| 475 | bool gotNonDot = false; |
| 476 | for (unsigned i = 0; i < valueSize; ++i) { |
| 477 | if (valuePtr[i] != 0x2e) { |
| 478 | gotNonDot = true; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | if (!gotNonDot) { |
| 483 | // Special case for component of zero or more periods. Add 3 periods. |
| 484 | result << "..."; |
| 485 | for (size_t i = 0; i < valueSize; ++i) |
| 486 | result << '.'; |
| 487 | } |
| 488 | else { |
| 489 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 490 | std::ios::fmtflags saveFlags = result.flags(std::ios::hex | std::ios::uppercase); |
| 491 | |
| 492 | for (size_t i = 0; i < valueSize; ++i) { |
| 493 | uint8_t x = valuePtr[i]; |
| 494 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 495 | if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) || |
| 496 | (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d || |
| 497 | x == 0x2e || x == 0x5f) |
| 498 | result << x; |
| 499 | else { |
| 500 | result << '%'; |
| 501 | if (x < 16) |
| 502 | result << '0'; |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 503 | result << static_cast<unsigned int>(x); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | // Restore. |
| 508 | result.flags(saveFlags); |
| 509 | } |
| 510 | } |
| 511 | |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 512 | |
| 513 | inline Component |
| 514 | Component::fromNumber(uint64_t number) |
| 515 | { |
| 516 | /// \todo Change to Tlv::NumberComponent |
| 517 | return nonNegativeIntegerBlock(Tlv::NameComponent, number); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | inline uint64_t |
| 522 | Component::toNumber() const |
| 523 | { |
| 524 | /// \todo Check if Component is of Tlv::NumberComponent type |
| 525 | return readNonNegativeInteger(static_cast<const Block&>(*this)); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | inline uint64_t |
| 530 | Component::toVersion() const |
| 531 | { |
| 532 | return toNumber(); |
| 533 | } |
| 534 | |
| 535 | inline uint64_t |
| 536 | Component::toSegment() const |
| 537 | { |
| 538 | return toNumber(); |
| 539 | } |
| 540 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 541 | inline int |
| 542 | Component::compare(const Component& other) const |
| 543 | { |
| 544 | // Imitate ndn_Exclude_compareComponents. |
| 545 | if (value_size() < other.value_size()) |
| 546 | return -1; |
| 547 | if (value_size() > other.value_size()) |
| 548 | return 1; |
| 549 | |
| 550 | if (value_size() == 0) |
| 551 | return 0; |
| 552 | |
| 553 | // The components are equal length. Just do a byte compare. |
| 554 | return std::memcmp(value(), other.value(), value_size()); |
| 555 | } |
| 556 | |
| 557 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 558 | template<bool T> |
| 559 | inline size_t |
| 560 | Component::wireEncode(EncodingImpl<T>& block) const |
| 561 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 562 | size_t totalLength = 0; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 563 | if (value_size() > 0) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 564 | totalLength += block.prependByteArray(value(), value_size()); |
| 565 | totalLength += block.prependVarNumber(value_size()); |
| 566 | totalLength += block.prependVarNumber(Tlv::NameComponent); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 567 | return totalLength; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 570 | inline const Block& |
| 571 | Component::wireEncode() const |
| 572 | { |
| 573 | if (this->hasWire()) |
| 574 | return *this; |
| 575 | |
| 576 | EncodingEstimator estimator; |
| 577 | size_t estimatedSize = wireEncode(estimator); |
| 578 | |
| 579 | EncodingBuffer buffer(estimatedSize, 0); |
| 580 | wireEncode(buffer); |
| 581 | |
| 582 | const_cast<Component&>(*this) = buffer.block(); |
| 583 | return *this; |
| 584 | } |
| 585 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 586 | inline void |
| 587 | Component::wireDecode(const Block& wire) |
| 588 | { |
| 589 | if (wire.type() != Tlv::NameComponent) |
| 590 | throw Error("wireDecode name component from non name component TLV wire block"); |
| 591 | |
| 592 | *this = wire; |
| 593 | } |
| 594 | |
| 595 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 596 | } // namespace name |
| 597 | } // namespace ndn |
| 598 | |
| 599 | #endif // NDN_NAME_COMPONENT_HPP |