blob: 70792332477d7ee93a54d7dcf1a7c237c8f8f88b [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07004 */
5
6#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008
9#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070010#include <string>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070011#include <sstream>
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "c/name.h"
13#include "encoding/binary-xml-wire-format.hpp"
Jeff Thompson995aba52013-09-12 12:04:52 -070014#include "util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070015
16namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070017
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070018class Name {
19public:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070020 class Component {
21 public:
22 /**
23 * Create a new Name::Component with an empty value.
24 */
25 Component()
26 {
27 }
28
29 /**
30 * Create a new Name::Component, copying the given value.
31 * @param value The value byte array.
32 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070033 Component(const std::vector<unsigned char>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070034 : value_(value)
35 {
36 }
37
38 /**
39 * Create a new Name::Component, copying the given value.
40 * @param value Pointer to the value byte array.
41 * @param valueLen Length of value.
42 */
Jeff Thompsona166b732013-08-27 16:09:29 -070043 Component(const unsigned char *value, unsigned int valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070044 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070045 {
46 }
47
48 /**
49 * Set the componentStruct to point to this component, without copying any memory.
50 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
51 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
52 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070053 void get(struct ndn_NameComponent& componentStruct) const
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070054 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070055 componentStruct.valueLength = value_.size();
Jeff Thompson38d0e082013-08-12 18:07:44 -070056 if (value_.size() > 0)
Jeff Thompson995aba52013-09-12 12:04:52 -070057 componentStruct.value = (unsigned char*)value_.buf();
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070058 }
59
60 /**
61 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
62 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
63 * the component should be skipped in a URI name.
64 * @param first Pointer to the beginning of the escaped string
65 * @param last Pointer to the first character past the end of the escaped string
66 * @return True for success, false if escapedString is not a valid escaped component.
67 */
68 bool setFromEscapedString(const char *first, const char *last);
69
Jeff Thompson9bdb3b22013-09-12 12:42:13 -070070 const Blob& getValue() const { return value_; }
71
72 void setValue(const Blob& value) { value_ = value; }
Jeff Thompson8aac1992013-08-12 17:26:02 -070073
74 /**
75 * Set this component to the encoded segment number.
76 * @param segment The segment number.
77 */
78 void setSegment(unsigned long segment);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070079
80 private:
Jeff Thompson995aba52013-09-12 12:04:52 -070081 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070082 };
83
Jeff Thompson443398d2013-07-02 19:45:46 -070084 /**
85 * Create a new Name with no components.
86 */
Jeff Thompson016ed642013-07-02 14:39:06 -070087 Name() {
88 }
Jeff Thompson443398d2013-07-02 19:45:46 -070089
90 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -070091 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070092 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -070093 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070094 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -070095 : components_(components)
96 {
97 }
98
99 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700100 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700101 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700102 */
Jeff Thompson67515bd2013-08-15 17:43:22 -0700103 Name(const char *uri)
104 {
105 set(uri);
106 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700107
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700108 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700109 * Set the nameStruct to point to the components in this name, without copying any memory.
110 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700111 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700112 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700113 void get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700114
115 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700116 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700117 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700118 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700119 void set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700120
121 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700122 * Parse the uri according to the NDN URI Scheme and set the name with the components.
123 * @param uri The URI string.
124 */
125 void set(const char *uri);
126
127 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700128 * Add a new component, copying from value of length valueLength.
129 */
Jeff Thompsona166b732013-08-27 16:09:29 -0700130 void addComponent(const unsigned char *value, unsigned int valueLength) {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700131 components_.push_back(Component(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700132 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700133
134 /**
135 * Add a new component, copying from value.
136 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700137 void addComponent(const std::vector<unsigned char>& value) {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700138 components_.push_back(value);
139 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700140
141 /**
142 * Clear all the components.
143 */
144 void clear() {
145 components_.clear();
146 }
147
148 /**
149 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700150 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700151 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700152 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700153 return components_.size();
154 }
155
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700156 const Component& getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700157
Jeff Thompsone6063512013-07-01 15:11:28 -0700158 /**
159 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700160 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700161 */
Jeff Thompson21844fc2013-08-08 14:52:51 -0700162 std::string toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700163
Jeff Thompson21844fc2013-08-08 14:52:51 -0700164 /**
165 * @deprecated Use toUri().
166 */
167 std::string to_uri() const
168 {
169 return toUri();
170 }
Jeff Thompson8aac1992013-08-12 17:26:02 -0700171
172 /**
173 * Append a component with the encoded segment number.
174 * @param segment The segment number.
175 */
176 void appendSegment(unsigned long segment)
177 {
178 components_.push_back(Component());
179 components_.back().setSegment(segment);
180 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700181
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700182 /**
183 * Check if the N components of this name are the same as the first N components of the given name.
184 * @param name The Name to check.
185 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
186 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700187 bool match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700188
189 /**
190 * Write the value to result, escaping characters according to the NDN URI Scheme.
191 * This also adds "..." to a value with zero or more ".".
192 * @param value the buffer with the value to escape
193 * @param result the string stream to write to.
194 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700195 static void toEscapedString(const std::vector<unsigned char>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700196
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700197private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700198 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700199};
200
201}
202
203#endif
204