blob: 302c7da0b1a2c42c7f31d9f49a7b856abc58efcf [file] [log] [blame]
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
7#ifndef NDN_NAME_HPP
8#define NDN_NAME_HPP
9
10#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070011#include <string>
Jeff Thompson016ed642013-07-02 14:39:06 -070012#include "c/Name.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070013#include "encoding/BinaryXMLWireFormat.hpp"
14
15namespace ndn {
16
Jeff Thompson016ed642013-07-02 14:39:06 -070017class NameComponent {
18public:
Jeff Thompson443398d2013-07-02 19:45:46 -070019 NameComponent()
20 {
21 }
22
Jeff Thompson016ed642013-07-02 14:39:06 -070023 NameComponent(unsigned char * value, unsigned int valueLen)
24 : value_(value, value + valueLen)
25 {
26 }
27
28 /**
29 * Set the componentStruct to point to this component, without copying any memory.
30 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
31 * @param componentStruct the C ndn_NameComponent struct to receive the pointer.
32 */
33 void get(struct ndn_NameComponent &componentStruct)
34 {
35 componentStruct.value = &value_[0];
36 componentStruct.valueLength = value_.size();
37 }
38
Jeff Thompson443398d2013-07-02 19:45:46 -070039 /**
40 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
41 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
42 * the component should be skipped in a URI name.
43 * @param first pointer to the beginning of the escaped string
44 * @param last pointer to the first character past the end of the escaped string
45 * @return true for success, false if escapedString is not a valid escaped component.
46 */
47 bool setFromEscapedString(const char *first, const char *last);
48
Jeff Thompson016ed642013-07-02 14:39:06 -070049 const std::vector<unsigned char> &getValue() const { return value_; }
50
51private:
52 std::vector<unsigned char> value_;
53};
54
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070055class Name {
56public:
Jeff Thompson443398d2013-07-02 19:45:46 -070057 /**
58 * Create a new Name with no components.
59 */
Jeff Thompson016ed642013-07-02 14:39:06 -070060 Name() {
61 }
Jeff Thompson443398d2013-07-02 19:45:46 -070062
63 /**
64 * Parse the uri according to the NDN URI Scheme and create the name with the components.
65 * @param uri the URI string.
66 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070067 Name(const char *uri);
68
Jeff Thompson2a749d12013-07-02 15:03:08 -070069 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat)
70 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070071 wireFormat.encodeName(*this, output);
72 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070073 void encode(std::vector<unsigned char> &output)
74 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070075 encode(output, BinaryXMLWireFormat::instance());
76 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070077 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
78 {
Jeff Thompson42380712013-06-28 10:59:33 -070079 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070080 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070081 void decode(const unsigned char *input, unsigned int inputLength)
82 {
Jeff Thompson42380712013-06-28 10:59:33 -070083 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070084 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070085 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
86 {
87 decode(&input[0], input.size(), wireFormat);
88 }
89 void decode(const std::vector<unsigned char> &input)
90 {
91 decode(&input[0], input.size());
92 }
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070093
Jeff Thompsone5f839b2013-06-28 12:50:38 -070094 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070095 * Set the nameStruct to point to the components in this name, without copying any memory.
96 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
97 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
98 */
99 void get(struct ndn_Name &nameStruct);
100
101 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700102 * Clear this name, and set the components by copying from the name struct.
103 * @param nameStruct a C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700104 */
105 void set(struct ndn_Name &nameStruct);
106
107 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700108 * Add a new component, copying from value of length valueLength.
109 */
110 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -0700111 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700112 }
113
114 /**
115 * Clear all the components.
116 */
117 void clear() {
118 components_.clear();
119 }
120
121 /**
122 * Get the number of components.
123 * @return the number of components
124 */
125 unsigned int getComponentCount() {
126 return components_.size();
127 }
128
Jeff Thompson443398d2013-07-02 19:45:46 -0700129 const NameComponent &getComponent(unsigned int i) const { return components_[i]; }
130
Jeff Thompsone6063512013-07-01 15:11:28 -0700131 /**
132 * Encode this name as a URI.
133 * @return the encoded URI.
134 */
135 std::string to_uri();
136
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700137private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700138 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700139};
140
141}
142
143#endif
144