blob: 5d6fb19d1e7a635a67c6edd7a6760c31c6ec5794 [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 Thompsona234d5d2013-07-26 16:46:22 -070099 Name(const char *uri);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700100
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700101 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700102 * Set the nameStruct to point to the components in this name, without copying any memory.
103 * 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 -0700104 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700105 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700106 void get(struct ndn_Name &nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700107
108 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700109 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700110 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700111 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700112 void set(const struct ndn_Name &nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700113
114 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700115 * Add a new component, copying from value of length valueLength.
116 */
117 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700118 components_.push_back(Component(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700119 }
120
121 /**
122 * Clear all the components.
123 */
124 void clear() {
125 components_.clear();
126 }
127
128 /**
129 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700130 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700131 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700132 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700133 return components_.size();
134 }
135
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700136 const Component &getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700137
Jeff Thompsone6063512013-07-01 15:11:28 -0700138 /**
139 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700140 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700141 */
Jeff Thompson21844fc2013-08-08 14:52:51 -0700142 std::string toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700143
Jeff Thompson21844fc2013-08-08 14:52:51 -0700144 /**
145 * @deprecated Use toUri().
146 */
147 std::string to_uri() const
148 {
149 return toUri();
150 }
Jeff Thompson8aac1992013-08-12 17:26:02 -0700151
152 /**
153 * Append a component with the encoded segment number.
154 * @param segment The segment number.
155 */
156 void appendSegment(unsigned long segment)
157 {
158 components_.push_back(Component());
159 components_.back().setSegment(segment);
160 }
Jeff Thompson21844fc2013-08-08 14:52:51 -0700161
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700162private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700163 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700164};
165
166}
167
168#endif
169