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