blob: 81299797e031fd1ee004b3b96f0d79d113eef4b6 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07005 */
6
7#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07009
10#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070011#include <string>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070012#include <sstream>
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "c/name.h"
14#include "encoding/binary-xml-wire-format.hpp"
Jeff Thompson995aba52013-09-12 12:04:52 -070015#include "util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070016
17namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070018
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070019class Name {
20public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070021 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070022 * A Name::Component is holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070023 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070024 class Component {
25 public:
26 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070027 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070028 */
29 Component()
30 {
31 }
32
33 /**
34 * Create a new Name::Component, copying the given value.
35 * @param value The value byte array.
36 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070037 Component(const std::vector<unsigned char>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070038 : value_(value)
39 {
40 }
41
42 /**
43 * Create a new Name::Component, copying the given value.
44 * @param value Pointer to the value byte array.
45 * @param valueLen Length of value.
46 */
Jeff Thompsona166b732013-08-27 16:09:29 -070047 Component(const unsigned char *value, unsigned int valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070048 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070049 {
50 }
Jeff Thompson0f743452013-09-12 14:23:18 -070051
52 /**
53 * Create a new Name::Component, taking another pointer to the Blob value.
54 * @param value A blob with a pointer to an immutable array. The pointer is copied.
55 */
56 Component(const Blob &value)
57 : value_(value)
58 {
59 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070060
61 /**
62 * Set the componentStruct to point to this component, without copying any memory.
63 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
64 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
65 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070066 void
67 get(struct ndn_NameComponent& componentStruct) const
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070068 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070069 componentStruct.valueLength = value_.size();
Jeff Thompson38d0e082013-08-12 18:07:44 -070070 if (value_.size() > 0)
Jeff Thompson995aba52013-09-12 12:04:52 -070071 componentStruct.value = (unsigned char*)value_.buf();
Jeff Thompson05c8c1b2013-09-12 12:47:57 -070072 else
73 componentStruct.value = 0;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070074 }
75
Jeff Thompson0050abe2013-09-17 12:50:25 -070076 const Blob&
77 getValue() const { return value_; }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -070078
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070079 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070080 * Make a component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
81 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070082 * the component should be skipped in a URI name.
83 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
84 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
85 * @param endOffset The offset in escapedString of the end of the portion to decode.
Jeff Thompson46411c92013-09-13 19:31:25 -070086 * @return The component value as a Blob, or a Blob with a null pointer if escapedString is not a valid escaped component.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070087 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070088 static Blob
89 makeFromEscapedString(const char *escapedString, unsigned int beginOffset, unsigned int endOffset);
Jeff Thompson8aac1992013-08-12 17:26:02 -070090
91 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070092 * Make a component as the encoded segment number.
Jeff Thompson8aac1992013-08-12 17:26:02 -070093 * @param segment The segment number.
Jeff Thompson46411c92013-09-13 19:31:25 -070094 * @return The component value as a Blob.
Jeff Thompson8aac1992013-08-12 17:26:02 -070095 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070096 static Blob
97 makeSegment(unsigned long segment);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070098
99 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700100 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700101 };
102
Jeff Thompson443398d2013-07-02 19:45:46 -0700103 /**
104 * Create a new Name with no components.
105 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700106 Name() {
107 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700108
109 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700110 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700111 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700112 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700113 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700114 : components_(components)
115 {
116 }
117
118 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700119 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700120 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700121 */
Jeff Thompson67515bd2013-08-15 17:43:22 -0700122 Name(const char *uri)
123 {
124 set(uri);
125 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700126
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700127 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700128 * Set the nameStruct to point to the components in this name, without copying any memory.
129 * 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 -0700130 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700131 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700132 void
133 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700134
135 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700136 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700137 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700138 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700139 void
140 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700141
142 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700143 * Parse the uri according to the NDN URI Scheme and set the name with the components.
144 * @param uri The URI string.
145 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700146 void
147 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700148
149 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700150 * Add a new component, copying from value of length valueLength.
151 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700152 void
153 addComponent(const unsigned char *value, unsigned int valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700154 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700155 components_.push_back(Component(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700156 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700157
158 /**
159 * Add a new component, copying from value.
160 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700161 void
162 addComponent(const std::vector<unsigned char>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700163 {
164 components_.push_back(value);
165 }
166
Jeff Thompson0050abe2013-09-17 12:50:25 -0700167 void
168 addComponent(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700169 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700170 components_.push_back(value);
171 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700172
173 /**
174 * Clear all the components.
175 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700176 void
177 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700178 components_.clear();
179 }
180
181 /**
182 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700183 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700184 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700185 unsigned int
186 getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700187 return components_.size();
188 }
189
Jeff Thompson0050abe2013-09-17 12:50:25 -0700190 const Component&
191 getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700192
Jeff Thompsone6063512013-07-01 15:11:28 -0700193 /**
194 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700195 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700196 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700197 std::string
198 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700199
Jeff Thompson21844fc2013-08-08 14:52:51 -0700200 /**
201 * @deprecated Use toUri().
202 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700203 std::string
204 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700205 {
206 return toUri();
207 }
Jeff Thompson8aac1992013-08-12 17:26:02 -0700208
209 /**
210 * Append a component with the encoded segment number.
211 * @param segment The segment number.
212 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700213 void
214 appendSegment(unsigned long segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700215 {
Jeff Thompson46411c92013-09-13 19:31:25 -0700216 components_.push_back(Component(Component::makeSegment(segment)));
Jeff Thompson8aac1992013-08-12 17:26:02 -0700217 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700218
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700219 /**
220 * Check if the N components of this name are the same as the first N components of the given name.
221 * @param name The Name to check.
222 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
223 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224 bool
225 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700226
227 /**
228 * Write the value to result, escaping characters according to the NDN URI Scheme.
229 * This also adds "..." to a value with zero or more ".".
230 * @param value the buffer with the value to escape
231 * @param result the string stream to write to.
232 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700233 static void
234 toEscapedString(const std::vector<unsigned char>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700235
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700236private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700237 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700238};
239
240}
241
242#endif
243