blob: e02d293f4c08765c74ef720649854230f69e3697 [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 Thompson53412192013-08-06 13:35:50 -070011#include "c/name.h"
12#include "encoding/binary-xml-wire-format.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013
14namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070015
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070016class Name {
17public:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070018 class Component {
19 public:
20 /**
21 * Create a new Name::Component with an empty value.
22 */
23 Component()
24 {
25 }
26
27 /**
28 * Create a new Name::Component, copying the given value.
29 * @param value The value byte array.
30 */
31 Component(const std::vector<unsigned char> &value)
32 : value_(value)
33 {
34 }
35
36 /**
37 * Create a new Name::Component, copying the given value.
38 * @param value Pointer to the value byte array.
39 * @param valueLen Length of value.
40 */
41 Component(unsigned char *value, unsigned int valueLen)
42 : value_(value, value + valueLen)
43 {
44 }
45
46 /**
47 * Set the componentStruct to point to this component, without copying any memory.
48 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
49 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
50 */
51 void get(struct ndn_NameComponent &componentStruct) const
52 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070053 componentStruct.valueLength = value_.size();
Jeff Thompson38d0e082013-08-12 18:07:44 -070054 if (value_.size() > 0)
55 componentStruct.value = (unsigned char *)&value_[0];
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070056 }
57
58 /**
59 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
60 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
61 * the component should be skipped in a URI name.
62 * @param first Pointer to the beginning of the escaped string
63 * @param last Pointer to the first character past the end of the escaped string
64 * @return True for success, false if escapedString is not a valid escaped component.
65 */
66 bool setFromEscapedString(const char *first, const char *last);
67
68 const std::vector<unsigned char> &getValue() const { return value_; }
Jeff Thompson8aac1992013-08-12 17:26:02 -070069
70 /**
71 * Set this component to the encoded segment number.
72 * @param segment The segment number.
73 */
74 void setSegment(unsigned long segment);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070075
76 private:
77 std::vector<unsigned char> value_;
78 };
79
Jeff Thompson443398d2013-07-02 19:45:46 -070080 /**
81 * Create a new Name with no components.
82 */
Jeff Thompson016ed642013-07-02 14:39:06 -070083 Name() {
84 }
Jeff Thompson443398d2013-07-02 19:45:46 -070085
86 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -070087 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070088 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -070089 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070090 Name(const std::vector<Component> &components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -070091 : components_(components)
92 {
93 }
94
95 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070096 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070097 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070098 */
Jeff Thompson67515bd2013-08-15 17:43:22 -070099 Name(const char *uri)
100 {
101 set(uri);
102 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700103
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700104 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700105 * Set the nameStruct to point to the components in this name, without copying any memory.
106 * 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 -0700107 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700108 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700109 void get(struct ndn_Name &nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700110
111 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700112 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700113 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700114 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700115 void set(const struct ndn_Name &nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700116
117 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700118 * Parse the uri according to the NDN URI Scheme and set the name with the components.
119 * @param uri The URI string.
120 */
121 void set(const char *uri);
122
123 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700124 * Add a new component, copying from value of length valueLength.
125 */
126 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700127 components_.push_back(Component(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700128 }
129
130 /**
131 * Clear all the components.
132 */
133 void clear() {
134 components_.clear();
135 }
136
137 /**
138 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700139 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700140 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700141 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700142 return components_.size();
143 }
144
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700145 const Component &getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700146
Jeff Thompsone6063512013-07-01 15:11:28 -0700147 /**
148 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700149 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700150 */
Jeff Thompson21844fc2013-08-08 14:52:51 -0700151 std::string toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700152
Jeff Thompson21844fc2013-08-08 14:52:51 -0700153 /**
154 * @deprecated Use toUri().
155 */
156 std::string to_uri() const
157 {
158 return toUri();
159 }
Jeff Thompson8aac1992013-08-12 17:26:02 -0700160
161 /**
162 * Append a component with the encoded segment number.
163 * @param segment The segment number.
164 */
165 void appendSegment(unsigned long segment)
166 {
167 components_.push_back(Component());
168 components_.back().setSegment(segment);
169 }
Jeff Thompson21844fc2013-08-08 14:52:51 -0700170
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700171private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700172 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700173};
174
175}
176
177#endif
178