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