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