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