blob: 2baee7dc13bead1861c82340675931d9039ad302 [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 {
53 componentStruct.value = (unsigned char *)&value_[0];
54 componentStruct.valueLength = value_.size();
55 }
56
57 /**
58 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
59 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
60 * the component should be skipped in a URI name.
61 * @param first Pointer to the beginning of the escaped string
62 * @param last Pointer to the first character past the end of the escaped string
63 * @return True for success, false if escapedString is not a valid escaped component.
64 */
65 bool setFromEscapedString(const char *first, const char *last);
66
67 const std::vector<unsigned char> &getValue() const { return value_; }
Jeff Thompson8aac1992013-08-12 17:26:02 -070068
69 /**
70 * Set this component to the encoded segment number.
71 * @param segment The segment number.
72 */
73 void setSegment(unsigned long segment);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070074
75 private:
76 std::vector<unsigned char> value_;
77 };
78
Jeff Thompson443398d2013-07-02 19:45:46 -070079 /**
80 * Create a new Name with no components.
81 */
Jeff Thompson016ed642013-07-02 14:39:06 -070082 Name() {
83 }
Jeff Thompson443398d2013-07-02 19:45:46 -070084
85 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -070086 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070087 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -070088 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070089 Name(const std::vector<Component> &components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -070090 : components_(components)
91 {
92 }
93
94 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070095 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070096 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070097 */
Jeff Thompsona234d5d2013-07-26 16:46:22 -070098 Name(const char *uri);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070099
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700100 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700101 * Set the nameStruct to point to the components in this name, without copying any memory.
102 * 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 -0700103 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700104 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700105 void get(struct ndn_Name &nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700106
107 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700108 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700109 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700110 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700111 void set(const struct ndn_Name &nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700112
113 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700114 * Add a new component, copying from value of length valueLength.
115 */
116 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700117 components_.push_back(Component(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700118 }
119
120 /**
121 * Clear all the components.
122 */
123 void clear() {
124 components_.clear();
125 }
126
127 /**
128 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700129 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700130 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700131 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700132 return components_.size();
133 }
134
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700135 const Component &getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700136
Jeff Thompsone6063512013-07-01 15:11:28 -0700137 /**
138 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700139 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700140 */
Jeff Thompson21844fc2013-08-08 14:52:51 -0700141 std::string toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700142
Jeff Thompson21844fc2013-08-08 14:52:51 -0700143 /**
144 * @deprecated Use toUri().
145 */
146 std::string to_uri() const
147 {
148 return toUri();
149 }
Jeff Thompson8aac1992013-08-12 17:26:02 -0700150
151 /**
152 * Append a component with the encoded segment number.
153 * @param segment The segment number.
154 */
155 void appendSegment(unsigned long segment)
156 {
157 components_.push_back(Component());
158 components_.back().setSegment(segment);
159 }
Jeff Thompson21844fc2013-08-08 14:52:51 -0700160
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700161private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700162 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700163};
164
165}
166
167#endif
168