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