Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 7 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef NDN_NAME_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 11 | #define NDN_NAME_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | |
| 13 | #include <vector> |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 14 | #include <string> |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 15 | #include <sstream> |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 16 | #include "util/blob.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 17 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 18 | struct ndn_NameComponent; |
| 19 | struct ndn_Name; |
| 20 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 21 | namespace ndn { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 22 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 23 | class Name { |
| 24 | public: |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 25 | /** |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 26 | * A Name::Component is holds a read-only name component value. |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 27 | */ |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 28 | class Component { |
| 29 | public: |
| 30 | /** |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 31 | * Create a new Name::Component with a null value. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 32 | */ |
| 33 | Component() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Create a new Name::Component, copying the given value. |
| 39 | * @param value The value byte array. |
| 40 | */ |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 41 | Component(const std::vector<uint8_t>& value) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 42 | : value_(value) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Create a new Name::Component, copying the given value. |
| 48 | * @param value Pointer to the value byte array. |
| 49 | * @param valueLen Length of value. |
| 50 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 51 | Component(const uint8_t *value, size_t valueLen) |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 52 | : value_(value, valueLen) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Create a new Name::Component, taking another pointer to the Blob value. |
| 58 | * @param value A blob with a pointer to an immutable array. The pointer is copied. |
| 59 | */ |
| 60 | Component(const Blob &value) |
| 61 | : value_(value) |
| 62 | { |
| 63 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Set the componentStruct to point to this component, without copying any memory. |
| 67 | * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory. |
| 68 | * @param componentStruct The C ndn_NameComponent struct to receive the pointer. |
| 69 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 70 | void |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 71 | get(struct ndn_NameComponent& componentStruct) const; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 72 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 73 | const Blob& |
| 74 | getValue() const { return value_; } |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Write this component value to result, escaping characters according to the NDN URI Scheme. |
| 78 | * This also adds "..." to a value with zero or more ".". |
| 79 | * @param value the buffer with the value to escape |
| 80 | * @param result the string stream to write to. |
| 81 | */ |
| 82 | void |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 83 | toEscapedString(std::ostringstream& result) const |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 84 | { |
| 85 | Name::toEscapedString(*value_, result); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Convert this component value by escaping characters according to the NDN URI Scheme. |
| 90 | * This also adds "..." to a value with zero or more ".". |
| 91 | * @return The escaped string. |
| 92 | */ |
| 93 | std::string |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 94 | toEscapedString() const |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 95 | { |
| 96 | return Name::toEscapedString(*value_); |
| 97 | } |
Jeff Thompson | 9bdb3b2 | 2013-09-12 12:42:13 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | 50b3791 | 2013-10-08 13:39:33 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Interpret this name component as a network-ordered number and return an integer. |
| 101 | * @return The integer number. |
| 102 | */ |
| 103 | uint64_t |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 104 | toNumber() const; |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | 50b3791 | 2013-10-08 13:39:33 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Interpret this name component as a network-ordered number with a marker and return an integer. |
| 108 | * @param marker The required first byte of the component. |
| 109 | * @return The integer number. |
| 110 | * @throw runtime_error If the first byte of the component does not equal the marker. |
| 111 | */ |
| 112 | uint64_t |
| 113 | toNumberWithMarker(uint8_t marker) const; |
| 114 | |
| 115 | /** |
| 116 | * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number |
| 117 | * where the first byte is the marker 0x00). |
| 118 | * @return The integer segment number. |
| 119 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 120 | */ |
| 121 | uint64_t |
| 122 | toSegment() const |
| 123 | { |
| 124 | return toNumberWithMarker(0x00); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @deprecated Use toSegment. |
| 129 | */ |
| 130 | uint64_t |
| 131 | toSeqNum() const |
| 132 | { |
| 133 | return toSegment(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Interpret this name component as a version number according to NDN name conventions (a network-ordered number |
| 138 | * where the first byte is the marker 0xFD). Note that this returns the exact number from the component |
| 139 | * without converting it to a time representation. |
| 140 | * @return The integer segment number. |
| 141 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 142 | */ |
| 143 | uint64_t |
| 144 | toVersion() const |
| 145 | { |
| 146 | return toNumberWithMarker(0xFD); |
| 147 | } |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 148 | |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 149 | /** |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 150 | * Make a component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme. |
| 151 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 152 | * the component should be skipped in a URI name. |
| 153 | * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset. |
| 154 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
| 155 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 156 | * @return The component value. If the escapedString is not a valid escaped component, then the component value is a null pointer. |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 157 | */ |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 158 | static Component |
| 159 | fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset); |
| 160 | |
| 161 | /** |
| 162 | * Create a component whose value is the network-ordered encoding of the number. |
| 163 | * Note: if the number is zero, the result is empty. |
| 164 | * @param number The number to be encoded. |
| 165 | * @return The component value. |
| 166 | */ |
| 167 | static Component |
| 168 | fromNumber(uint64_t number); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 171 | * Create a component whose value is the marker appended with the network-ordered encoding of the number. |
| 172 | * Note: if the number is zero, no bytes are used for the number - the result will have only the marker. |
| 173 | * @param number The number to be encoded. |
| 174 | * @param marker The marker to use as the first byte of the component. |
| 175 | * @return The component value. |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 176 | */ |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 177 | static Component |
| 178 | fromNumberWithMarker(uint64_t number, uint8_t marker); |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 179 | |
| 180 | private: |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 181 | Blob value_; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Create a new Name with no components. |
| 186 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 187 | Name() { |
| 188 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 189 | |
| 190 | /** |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 191 | * Create a new Name, copying the name components. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 192 | * @param components A vector of Component |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 193 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 194 | Name(const std::vector<Component>& components) |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 195 | : components_(components) |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 200 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 201 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 202 | */ |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 203 | Name(const char* uri) |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 204 | { |
| 205 | set(uri); |
| 206 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 207 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 208 | /** |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 209 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
| 210 | * @param uri The URI string. |
| 211 | */ |
| 212 | Name(const std::string& uri) |
| 213 | { |
| 214 | set(uri.c_str()); |
| 215 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 216 | |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 217 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 218 | * Set the nameStruct to point to the components in this name, without copying any memory. |
| 219 | * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 220 | * @param nameStruct A C ndn_Name struct where the components array is already allocated. |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 221 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 222 | void |
| 223 | get(struct ndn_Name& nameStruct) const; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
Jeff Thompson | 8b27e3a | 2013-07-03 18:19:53 -0700 | [diff] [blame] | 226 | * Clear this name, and set the components by copying from the name struct. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 227 | * @param nameStruct A C ndn_Name struct |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 228 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 229 | void |
| 230 | set(const struct ndn_Name& nameStruct); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 231 | |
| 232 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 233 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 234 | * @param uri The URI string. |
| 235 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 236 | void |
| 237 | set(const char *uri); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 238 | |
| 239 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 240 | * Append a new component, copying from value of length valueLength. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 241 | * @return This name so that you can chain calls to append. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 242 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 243 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 244 | append(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 245 | { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 246 | components_.push_back(Component(value, valueLength)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 247 | return *this; |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 248 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 249 | |
| 250 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 251 | * Append a new component, copying from value. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 252 | * @return This name so that you can chain calls to append. |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 253 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 254 | Name& |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 255 | append(const std::vector<uint8_t>& value) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 256 | { |
| 257 | components_.push_back(value); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 258 | return *this; |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 261 | Name& |
| 262 | append(const Blob &value) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 263 | { |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 264 | components_.push_back(value); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 265 | return *this; |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 266 | } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 267 | |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 268 | Name& |
| 269 | append(const Component &value) |
| 270 | { |
| 271 | components_.push_back(value); |
| 272 | return *this; |
| 273 | } |
Jeff Thompson | 16c56cc | 2013-10-11 14:51:52 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Parse the escaped string according to the NDN URI Scheme and append a component to this name. |
| 277 | * If escapedString has "/", then the value of the single appended component has "/" in it. |
| 278 | * (If you need to append a URI which is split into separate components, then call append(Name(uri)).) |
| 279 | * @param escapedString The escaped string representing a single component. |
| 280 | * @return This name so that you can chain calls to append. |
| 281 | */ |
| 282 | Name& |
| 283 | append(const char* escapedString) |
| 284 | { |
| 285 | components_.push_back(Component::fromEscapedString(escapedString, 0, ::strlen(escapedString))); |
| 286 | return *this; |
| 287 | } |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 288 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 289 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 290 | * Append the components of the given name to this name. |
| 291 | * @param name The Name with components to append. |
| 292 | * @return This name so that you can chain calls to append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 293 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 294 | Name& |
| 295 | append(const Name& name); |
| 296 | |
| 297 | /** |
| 298 | * @deprecated Use append. |
| 299 | */ |
| 300 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 301 | appendComponent(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 302 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 303 | return append(value, valueLength); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 307 | * @deprecated Use append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 308 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 309 | Name& |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 310 | appendComponent(const std::vector<uint8_t>& value) |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 311 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 312 | return append(value); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 316 | * @deprecated Use append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 317 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 318 | Name& |
| 319 | appendComponent(const Blob &value) |
| 320 | { |
| 321 | return append(value); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @deprecated Use append. |
| 326 | */ |
| 327 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 328 | addComponent(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 329 | { |
| 330 | return append(value, valueLength); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @deprecated Use append. |
| 335 | */ |
| 336 | Name& |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 337 | addComponent(const std::vector<uint8_t>& value) |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 338 | { |
| 339 | return append(value); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @deprecated Use append. |
| 344 | */ |
| 345 | Name& |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 346 | addComponent(const Blob &value) |
| 347 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 348 | return append(value); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 352 | * Clear all the components. |
| 353 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 354 | void |
| 355 | clear() { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 356 | components_.clear(); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Get the number of components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 361 | * @return The number of components. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 362 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 363 | size_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 364 | getComponentCount() const { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 365 | return components_.size(); |
| 366 | } |
| 367 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 368 | /** |
| 369 | * Get the component at the given index. |
| 370 | * @param i The index of the component, starting from 0. |
| 371 | * @return The name component at the index. |
| 372 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 373 | const Component& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 374 | getComponent(size_t i) const { return components_[i]; } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 375 | |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 376 | /** |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 377 | * Get a new name, constructed as a subset of components. |
| 378 | * @param iStartComponent The index if the first component to get. |
| 379 | * @param nComponents The number of components starting at iStartComponent. |
| 380 | * @return A new name. |
| 381 | */ |
| 382 | Name |
| 383 | getSubName(size_t iStartComponent, size_t nComponents) const; |
| 384 | |
| 385 | /** |
| 386 | * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name. |
| 387 | * @param iStartComponent The index if the first component to get. |
| 388 | * @return A new name. |
| 389 | */ |
| 390 | Name |
| 391 | getSubName(size_t iStartComponent) const; |
| 392 | |
| 393 | /** |
| 394 | * Return a new Name with the first nComponents components of this Name. |
| 395 | * @param nComponents The number of prefix components. |
| 396 | * @return A new Name. |
| 397 | */ |
| 398 | Name |
| 399 | getPrefix(size_t nComponents) const |
| 400 | { |
| 401 | return getSubName(0, nComponents); |
| 402 | } |
| 403 | |
| 404 | /** |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 405 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 406 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 407 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 408 | std::string |
| 409 | toUri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 410 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 411 | /** |
| 412 | * @deprecated Use toUri(). |
| 413 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 414 | std::string |
| 415 | to_uri() const |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 416 | { |
| 417 | return toUri(); |
| 418 | } |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 419 | |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 420 | /** |
| 421 | * Append a component with the encoded segment number. |
| 422 | * @param segment The segment number. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 423 | * @return This name so that you can chain calls to append. |
| 424 | */ |
| 425 | Name& |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 426 | appendSegment(uint64_t segment) |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 427 | { |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 428 | components_.push_back(Component::fromNumberWithMarker(segment, 0x00)); |
| 429 | return *this; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Append a component with the encoded version number. |
| 434 | * Note that this encodes the exact value of version without converting from a time representation. |
| 435 | * @param version The version number. |
| 436 | * @return This name so that you can chain calls to append. |
| 437 | */ |
| 438 | Name& |
| 439 | appendVersion(uint64_t version) |
| 440 | { |
| 441 | components_.push_back(Component::fromNumberWithMarker(version, 0xFD)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 442 | return *this; |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 443 | } |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 444 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 445 | /** |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 446 | * Check if this name has the same component count and components as the given name. |
| 447 | * @param name The Name to check. |
| 448 | * @return true if the names are equal, otherwise false. |
| 449 | */ |
| 450 | bool |
| 451 | equals(const Name& name) const; |
| 452 | |
| 453 | /** |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 454 | * Check if the N components of this name are the same as the first N components of the given name. |
| 455 | * @param name The Name to check. |
| 456 | * @return true if this matches the given name, otherwise false. This always returns true if this name is empty. |
| 457 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 458 | bool |
| 459 | match(const Name& name) const; |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * Write the value to result, escaping characters according to the NDN URI Scheme. |
| 463 | * This also adds "..." to a value with zero or more ".". |
| 464 | * @param value the buffer with the value to escape |
| 465 | * @param result the string stream to write to. |
| 466 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 467 | static void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 468 | toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result); |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 469 | |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 470 | /** |
| 471 | * Convert the value by escaping characters according to the NDN URI Scheme. |
| 472 | * This also adds "..." to a value with zero or more ".". |
| 473 | * @param value the buffer with the value to escape |
| 474 | * @return The escaped string. |
| 475 | */ |
| 476 | static std::string |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 477 | toEscapedString(const std::vector<uint8_t>& value); |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 478 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 479 | // |
| 480 | // vector equivalent interface. |
| 481 | // |
| 482 | |
| 483 | /** |
| 484 | * Get the number of components. |
| 485 | * @return The number of components. |
| 486 | */ |
| 487 | size_t |
| 488 | size() const { |
| 489 | return getComponentCount(); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Get the component at the given index. |
| 494 | * @param i The index of the component, starting from 0. |
| 495 | * @return The name component at the index. |
| 496 | */ |
| 497 | const Component& |
| 498 | get(size_t i) const { return getComponent(i); } |
| 499 | |
| 500 | |
| 501 | const Component& |
| 502 | operator [] (int i) const |
| 503 | { |
| 504 | return get(i); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Append the component |
| 509 | * @param component The component of type T. |
| 510 | */ |
| 511 | template<class T> void |
| 512 | push_back(const T &component) |
| 513 | { |
| 514 | append(component); |
| 515 | } |
| 516 | |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 517 | /** |
| 518 | * Check if this name has the same component count and components as the given name. |
| 519 | * @param name The Name to check. |
| 520 | * @return true if the names are equal, otherwise false. |
| 521 | */ |
| 522 | bool |
| 523 | operator == (const Name &name) const { return equals(name); } |
| 524 | |
| 525 | /** |
| 526 | * Check if this name has the same component count and components as the given name. |
| 527 | * @param name The Name to check. |
| 528 | * @return true if the names are not equal, otherwise false. |
| 529 | */ |
| 530 | bool |
| 531 | operator != (const Name &name) const { return !equals(name); } |
| 532 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 533 | // |
| 534 | // Iterator interface to name components. |
| 535 | // |
| 536 | typedef std::vector<Component>::iterator iterator; |
| 537 | typedef std::vector<Component>::const_iterator const_iterator; |
| 538 | typedef std::vector<Component>::reverse_iterator reverse_iterator; |
| 539 | typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator; |
| 540 | typedef std::vector<Component>::reference reference; |
| 541 | typedef std::vector<Component>::const_reference const_reference; |
| 542 | |
| 543 | typedef Component partial_type; |
| 544 | |
| 545 | /** |
| 546 | * Begin iterator (const). |
| 547 | */ |
| 548 | const_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 549 | begin() const { return components_.begin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * Begin iterator. |
| 553 | */ |
| 554 | iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 555 | begin() { return components_.begin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * End iterator (const). |
| 559 | */ |
| 560 | const_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 561 | end() const { return components_.end(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * End iterator. |
| 565 | */ |
| 566 | iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 567 | end() { return components_.end(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * Reverse begin iterator (const). |
| 571 | */ |
| 572 | const_reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 573 | rbegin() const { return components_.rbegin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 574 | |
| 575 | /** |
| 576 | * Reverse begin iterator. |
| 577 | */ |
| 578 | reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 579 | rbegin() { return components_.rbegin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 580 | |
| 581 | /** |
| 582 | * Reverse end iterator (const). |
| 583 | */ |
| 584 | const_reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 585 | rend() const { return components_.rend(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * Reverse end iterator. |
| 589 | */ |
| 590 | reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 591 | rend() { return components_.rend(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 592 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 593 | private: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 594 | std::vector<Component> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 595 | }; |
| 596 | |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 597 | inline std::ostream& |
| 598 | operator << (std::ostream& os, const Name& name) |
| 599 | { |
| 600 | os << name.toUri(); |
| 601 | return os; |
| 602 | } |
| 603 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | #endif |
| 607 | |