blob: b6b2e015dff47782f151af1bd93adaeaf0a7d2e2 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonec39fbd2013-10-04 10:56:23 -07005 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07007 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008 */
9
10#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070011#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012
13#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070014#include <string>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070015#include <sstream>
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080016#include <string.h>
17#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070018
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070019namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070020
Jeff Thompsonc7d65502013-11-06 17:22:26 -080021/**
22 * A Name holds an array of Name::Component and represents an NDN name.
23 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070024class Name {
25public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070026 /**
Jeff Thompsonc7d65502013-11-06 17:22:26 -080027 * A Name::Component holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070028 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080029 class Component : private ConstBufferPtr
30 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070031 public:
32 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070033 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070034 */
35 Component()
36 {
37 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080038
39 // copy constructor OK
40
41 /**
42 * Create a new Name::Component, taking another pointer to the Blob value.
43 * @param value A blob with a pointer to an immutable array. The pointer is copied.
44 */
45 Component(const ConstBufferPtr &buffer)
46 : ConstBufferPtr (buffer)
47 {
48 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070049
50 /**
51 * Create a new Name::Component, copying the given value.
52 * @param value The value byte array.
53 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080054 Component(const Buffer& value)
55 : ConstBufferPtr (new Buffer(value))
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070056 {
57 }
58
59 /**
60 * Create a new Name::Component, copying the given value.
61 * @param value Pointer to the value byte array.
62 * @param valueLen Length of value.
63 */
Jeff Thompson97223af2013-09-24 17:01:27 -070064 Component(const uint8_t *value, size_t valueLen)
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080065 : ConstBufferPtr (new Buffer(value, valueLen))
66 {
67 }
68
69 template<class InputIterator>
70 Component(InputIterator begin, InputIterator end)
71 : ConstBufferPtr (new Buffer(begin, end))
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070072 {
73 }
Jeff Thompson0f743452013-09-12 14:23:18 -070074
Alexander Afanasyev848c61a2014-01-03 13:52:04 -080075 Component(const char *string)
76 : ConstBufferPtr (new Buffer(string, ::strlen(string)))
77 {
78 }
79
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080080 const Buffer&
81 getValue() const { return **this; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070082
83 /**
84 * Write this component value to result, escaping characters according to the NDN URI Scheme.
85 * This also adds "..." to a value with zero or more ".".
Jeff Thompson6653b0b2013-09-23 12:32:39 -070086 * @param result the string stream to write to.
87 */
88 void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080089 toEscapedString(std::ostream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070090 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080091 Name::toEscapedString(**this, result);
Jeff Thompson6653b0b2013-09-23 12:32:39 -070092 }
93
94 /**
95 * Convert this component value by escaping characters according to the NDN URI Scheme.
96 * This also adds "..." to a value with zero or more ".".
97 * @return The escaped string.
98 */
99 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -0700100 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700101 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800102 return Name::toEscapedString(**this);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700103 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -0700104
Jeff Thompson50b37912013-10-08 13:39:33 -0700105 /**
106 * Interpret this name component as a network-ordered number and return an integer.
107 * @return The integer number.
108 */
109 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700110 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700111
Jeff Thompson50b37912013-10-08 13:39:33 -0700112 /**
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 Thompson27cae532013-10-08 12:52:41 -0700154
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700155 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700156 * Create a component whose value is the network-ordered encoding of the number.
157 * Note: if the number is zero, the result is empty.
158 * @param number The number to be encoded.
159 * @return The component value.
160 */
161 static Component
162 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700163
164 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700165 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
166 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
167 * @param number The number to be encoded.
168 * @param marker The marker to use as the first byte of the component.
169 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700170 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700171 static Component
172 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompsona98000c2013-12-16 14:40:09 -0800173
174 /**
175 * Check if this is the same component as other.
176 * @param other The other Component to compare with.
177 * @return true if the components are equal, otherwise false.
178 */
179 bool
180 equals(const Component& other) const
181 {
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800182 return **this == *other;
183 }
184
185 bool
186 empty() const
187 {
188 return !*this || (*this)->empty();
Jeff Thompsona98000c2013-12-16 14:40:09 -0800189 }
190
191 /**
192 * Check if this is the same component as other.
193 * @param other The other Component to compare with.
194 * @return true if the components are equal, otherwise false.
195 */
196 bool
197 operator == (const Component& other) const { return equals(other); }
198
199 /**
200 * Check if this is not the same component as other.
201 * @param other The other Component to compare with.
202 * @return true if the components are not equal, otherwise false.
203 */
204 bool
205 operator != (const Component& other) const { return !equals(other); }
206
207 /**
208 * Compare this to the other Component using NDN canonical ordering.
209 * @param other The other Component to compare with.
210 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
211 * 1 if *this comes after other in the canonical ordering.
212 *
213 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
214 */
215 int
216 compare(const Component& other) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700217
Jeff Thompsona98000c2013-12-16 14:40:09 -0800218 /**
219 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
220 * @param other The other Component to compare with.
221 *
222 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
223 */
224 bool
225 operator <= (const Component& other) const { return compare(other) <= 0; }
226
227 /**
228 * Return true if this is less than the other Component in the NDN canonical ordering.
229 * @param other The other Component to compare with.
230 *
231 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
232 */
233 bool
234 operator < (const Component& other) const { return compare(other) < 0; }
235
236 /**
237 * Return true if this is less than or equal to the other Component in the NDN canonical ordering.
238 * @param other The other Component to compare with.
239 *
240 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
241 */
242 bool
243 operator >= (const Component& other) const { return compare(other) >= 0; }
244
245 /**
246 * Return true if this is greater than the other Component in the NDN canonical ordering.
247 * @param other The other Component to compare with.
248 *
249 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
250 */
251 bool
252 operator > (const Component& other) const { return compare(other) > 0; }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800253 };
Jeff Thompsona98000c2013-12-16 14:40:09 -0800254
Jeff Thompson443398d2013-07-02 19:45:46 -0700255 /**
256 * Create a new Name with no components.
257 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700258 Name() {
259 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700260
261 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700262 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700263 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700264 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700265 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700266 : components_(components)
267 {
268 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800269
270 Name(const Block &name)
271 {
272 for (Block::element_const_iterator i = name.getAll().begin();
273 i != name.getAll().end();
274 ++i)
275 {
276 append(Component(i->value_begin(), i->value_end()));
277 }
278 }
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700279
280 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700281 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700282 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700283 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700284 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700285 {
286 set(uri);
287 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700288
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700289 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700290 * Parse the uri according to the NDN URI Scheme and create the name with the components.
291 * @param uri The URI string.
292 */
293 Name(const std::string& uri)
294 {
295 set(uri.c_str());
296 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700297
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800298 const Block &
299 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800300
301 void
302 wireDecode(const Block &wire);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700303
304 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700305 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800306 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700307 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700308 void
309 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700310
311 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800312 * Parse the uri according to the NDN URI Scheme and set the name with the components.
313 * @param uri The URI string.
314 */
315 void
316 set(const std::string& uri) { set(uri.c_str()); }
317
318 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700319 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700320 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700321 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700322 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700323 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700324 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700325 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700326 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700327 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700328
329 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700330 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700331 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700332 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700333 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800334 append(const Buffer& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700335 {
336 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700337 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700338 }
339
Jeff Thompson26b0d792013-09-23 16:19:01 -0700340 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800341 append(const ConstBufferPtr &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700342 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700343 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700344 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700345 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700346
Jeff Thompson21eb7212013-09-26 09:05:40 -0700347 Name&
348 append(const Component &value)
349 {
350 components_.push_back(value);
351 return *this;
352 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800353
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800354 /**
355 * @brief Append name component that represented as a string
356 *
357 * Note that this method is necessary to ensure correctness and unambiguity of
358 * ``append("string")`` operations (both Component and Name can be implicitly
359 * converted from string, each having different outcomes
360 */
361 Name&
362 append(const char *value)
363 {
364 components_.push_back(Component(value));
365 return *this;
366 }
367
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800368 Name&
369 append(const Block &value)
370 {
371 components_.push_back(Component(value.begin(), value.end()));
372 return *this;
373 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700374
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700375 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700376 * Append the components of the given name to this name.
377 * @param name The Name with components to append.
378 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700379 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700380 Name&
381 append(const Name& name);
382
383 /**
384 * @deprecated Use append.
385 */
386 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700387 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700388 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700389 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700390 }
391
392 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700393 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700394 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700395 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800396 appendComponent(const Buffer& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700397 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700398 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700399 }
400
401 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700402 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700403 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700404 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800405 appendComponent(const ConstBufferPtr &value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700406 {
407 return append(value);
408 }
409
410 /**
411 * @deprecated Use append.
412 */
413 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700414 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700415 {
416 return append(value, valueLength);
417 }
418
419 /**
420 * @deprecated Use append.
421 */
422 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800423 addComponent(const Buffer& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700424 {
425 return append(value);
426 }
427
428 /**
429 * @deprecated Use append.
430 */
431 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800432 addComponent(const ConstBufferPtr &value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700433 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700434 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700435 }
436
437 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700438 * Clear all the components.
439 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700440 void
441 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700442 components_.clear();
443 }
444
445 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700446 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700447 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700448 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700449 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700450
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700451 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700452 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700453 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700454 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700455 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700456
Jeff Thompsone6063512013-07-01 15:11:28 -0700457 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700458 * Get a new name, constructed as a subset of components.
459 * @param iStartComponent The index if the first component to get.
460 * @param nComponents The number of components starting at iStartComponent.
461 * @return A new name.
462 */
463 Name
464 getSubName(size_t iStartComponent, size_t nComponents) const;
465
466 /**
467 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
468 * @param iStartComponent The index if the first component to get.
469 * @return A new name.
470 */
471 Name
472 getSubName(size_t iStartComponent) const;
473
474 /**
475 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800476 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
477 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700478 * @return A new Name.
479 */
480 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800481 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700482 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800483 if (nComponents < 0)
484 return getSubName(0, components_.size() + nComponents);
485 else
486 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700487 }
488
489 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700490 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700491 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700492 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700493 std::string
494 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700495
Jeff Thompson21844fc2013-08-08 14:52:51 -0700496 /**
Jeff Thompson8aac1992013-08-12 17:26:02 -0700497 * Append a component with the encoded segment number.
498 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700499 * @return This name so that you can chain calls to append.
500 */
501 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700502 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700503 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700504 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
505 return *this;
506 }
507
508 /**
509 * Append a component with the encoded version number.
510 * Note that this encodes the exact value of version without converting from a time representation.
511 * @param version The version number.
512 * @return This name so that you can chain calls to append.
513 */
514 Name&
515 appendVersion(uint64_t version)
516 {
517 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700518 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700519 }
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800520
521 /**
522 * @brief Append a component with the encoded version number.
523 *
524 * This version of the method creates version number based on the current timestamp
525 * @return This name so that you can chain calls to append.
526 */
527 Name&
528 appendVersion();
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700529
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700530 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700531 * Check if this name has the same component count and components as the given name.
532 * @param name The Name to check.
533 * @return true if the names are equal, otherwise false.
534 */
535 bool
536 equals(const Name& name) const;
537
538 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700539 * Check if the N components of this name are the same as the first N components of the given name.
540 * @param name The Name to check.
541 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
542 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700543 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800544 isPrefixOf(const Name& name) const;
545
546 bool
547 match(const Name& name) const
548 {
549 return isPrefixOf(name);
550 }
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700551
552 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700553 * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800554 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
555 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700556 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
557 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
558 * @param endOffset The offset in escapedString of the end of the portion to decode.
559 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
560 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800561 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700562 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
563
564 /**
565 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Jeff Thompson08493592013-11-07 17:44:41 -0800566 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
567 * which means the component should be skipped in a URI name.
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700568 * @param escapedString The null-terminated escaped string.
569 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
570 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800571 static Component
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700572 fromEscapedString(const char *escapedString);
573
574 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800575 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
576 * If the escaped string is "", "." or ".." then return a Blob with a null pointer,
577 * which means the component should be skipped in a URI name.
578 * @param escapedString The escaped string.
579 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
580 */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800581 static Component
Jeff Thompson7781b392013-12-17 11:45:59 -0800582 fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); }
583
584 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700585 * Write the value to result, escaping characters according to the NDN URI Scheme.
586 * This also adds "..." to a value with zero or more ".".
587 * @param value the buffer with the value to escape
588 * @param result the string stream to write to.
589 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700590 static void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800591 toEscapedString(const std::vector<uint8_t>& value, std::ostream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700592
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700593 /**
594 * Convert the value by escaping characters according to the NDN URI Scheme.
595 * This also adds "..." to a value with zero or more ".".
596 * @param value the buffer with the value to escape
597 * @return The escaped string.
598 */
599 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700600 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700601
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700602 //
603 // vector equivalent interface.
604 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800605
606 /**
607 * @brief Check if name is emtpy
608 */
609 bool
610 empty() const { return components_.empty(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700611
612 /**
613 * Get the number of components.
614 * @return The number of components.
615 */
616 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700617 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700618
619 /**
620 * Get the component at the given index.
621 * @param i The index of the component, starting from 0.
622 * @return The name component at the index.
623 */
624 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800625 get(ssize_t i) const
626 {
627 if (i >= 0)
628 return components_[i];
629 else
630 return components_[size() + i];
631 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700632
633
634 const Component&
635 operator [] (int i) const
636 {
637 return get(i);
638 }
639
640 /**
641 * Append the component
642 * @param component The component of type T.
643 */
644 template<class T> void
645 push_back(const T &component)
646 {
647 append(component);
648 }
649
Jeff Thompson91737f52013-10-04 11:07:24 -0700650 /**
651 * Check if this name has the same component count and components as the given name.
652 * @param name The Name to check.
653 * @return true if the names are equal, otherwise false.
654 */
655 bool
656 operator == (const Name &name) const { return equals(name); }
657
658 /**
659 * Check if this name has the same component count and components as the given name.
660 * @param name The Name to check.
661 * @return true if the names are not equal, otherwise false.
662 */
663 bool
664 operator != (const Name &name) const { return !equals(name); }
665
Jeff Thompson82568ad2013-12-17 15:17:40 -0800666 /**
667 * Compare two names for "less than" using breadth first. If the first components of each name are not equal,
668 * this returns true if the first comes before the second using the NDN canonical ordering for name components.
669 * If they are equal, this compares the second components of each name, etc. If both names are the same up to
670 * the size of the shorter name, this returns true if the first name is shorter than the second. For example, if you
671 * use breadthFirstLess in std::sort, it gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
672 * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
673 * according to NDN canonical ordering since it is shorter. Note: We don't define this directly as the Name
674 * less than operation because there are other valid ways to sort names.
675 * @param name1 The first name to compare.
676 * @param name2 The second name to compare.
677 * @return True if the first name is less than the second using breadth first comparison.
678 */
679 static bool
680 breadthFirstLess(const Name& name1, const Name& name2);
681
682 /**
683 * Name::BreadthFirstLess is a function object which calls breadthFirstLess, for use as the "less" operator in map, etc.
684 * For example, you can use Name as the key type in a map as follows: map<Name, int, Name::BreadthFirstLess>.
685 */
686 struct BreadthFirstLess {
687 bool operator() (const Name& name1, const Name& name2) const { return breadthFirstLess(name1, name2); }
688 };
689
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700690 //
691 // Iterator interface to name components.
692 //
693 typedef std::vector<Component>::iterator iterator;
694 typedef std::vector<Component>::const_iterator const_iterator;
695 typedef std::vector<Component>::reverse_iterator reverse_iterator;
696 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
697 typedef std::vector<Component>::reference reference;
698 typedef std::vector<Component>::const_reference const_reference;
699
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800700 typedef Component value_type;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700701
702 /**
703 * Begin iterator (const).
704 */
705 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700706 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700707
708 /**
709 * Begin iterator.
710 */
711 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700712 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700713
714 /**
715 * End iterator (const).
716 */
717 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700718 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700719
720 /**
721 * End iterator.
722 */
723 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700724 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700725
726 /**
727 * Reverse begin iterator (const).
728 */
729 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700730 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700731
732 /**
733 * Reverse begin iterator.
734 */
735 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700736 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700737
738 /**
739 * Reverse end iterator (const).
740 */
741 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700742 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700743
744 /**
745 * Reverse end iterator.
746 */
747 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700748 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700749
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700750private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700751 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700752
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800753 mutable Block wire_;
754};
755
756std::ostream &
757operator << (std::ostream &os, const Name &name);
758
759inline std::string
760Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700761{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800762 std::ostringstream os;
763 os << *this;
764 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700765}
766
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700767}
768
769#endif
770