blob: 58b78bdb16127dc17e18203905fe1f65b8f25d79 [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 Thompson9c41dfe2013-06-27 12:10:25 -070012#include "common.h"
Jeff Thompson016ed642013-07-02 14:39:06 -070013#include "c/Name.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070014#include "encoding/BinaryXMLWireFormat.hpp"
15
16namespace ndn {
17
Jeff Thompson016ed642013-07-02 14:39:06 -070018class NameComponent {
19public:
Jeff Thompson443398d2013-07-02 19:45:46 -070020 NameComponent()
21 {
22 }
23
Jeff Thompson016ed642013-07-02 14:39:06 -070024 NameComponent(unsigned char * value, unsigned int valueLen)
25 : value_(value, value + valueLen)
26 {
27 }
28
29 /**
30 * Set the componentStruct to point to this component, without copying any memory.
31 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
32 * @param componentStruct the C ndn_NameComponent struct to receive the pointer.
33 */
34 void get(struct ndn_NameComponent &componentStruct)
35 {
36 componentStruct.value = &value_[0];
37 componentStruct.valueLength = value_.size();
38 }
39
Jeff Thompson443398d2013-07-02 19:45:46 -070040 /**
41 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
42 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
43 * the component should be skipped in a URI name.
44 * @param first pointer to the beginning of the escaped string
45 * @param last pointer to the first character past the end of the escaped string
46 * @return true for success, false if escapedString is not a valid escaped component.
47 */
48 bool setFromEscapedString(const char *first, const char *last);
49
Jeff Thompson016ed642013-07-02 14:39:06 -070050 const std::vector<unsigned char> &getValue() const { return value_; }
51
52private:
53 std::vector<unsigned char> value_;
54};
55
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070056class Name {
57public:
Jeff Thompson443398d2013-07-02 19:45:46 -070058 /**
59 * Create a new Name with no components.
60 */
Jeff Thompson016ed642013-07-02 14:39:06 -070061 Name() {
62 }
Jeff Thompson443398d2013-07-02 19:45:46 -070063
64 /**
65 * Parse the uri according to the NDN URI Scheme and create the name with the components.
66 * @param uri the URI string.
67 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070068 Name(const char *uri);
69
Jeff Thompson2a749d12013-07-02 15:03:08 -070070 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat)
71 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070072 wireFormat.encodeName(*this, output);
73 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070074 void encode(std::vector<unsigned char> &output)
75 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070076 encode(output, BinaryXMLWireFormat::instance());
77 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070078 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
79 {
Jeff Thompson42380712013-06-28 10:59:33 -070080 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070081 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070082 void decode(const unsigned char *input, unsigned int inputLength)
83 {
Jeff Thompson42380712013-06-28 10:59:33 -070084 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070085 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070086 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
87 {
88 decode(&input[0], input.size(), wireFormat);
89 }
90 void decode(const std::vector<unsigned char> &input)
91 {
92 decode(&input[0], input.size());
93 }
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070094
Jeff Thompsone5f839b2013-06-28 12:50:38 -070095 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070096 * Set the nameStruct to point to the components in this name, without copying any memory.
97 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
98 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
99 */
100 void get(struct ndn_Name &nameStruct);
101
102 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700103 * Clear this name, and set the components by copying from the name struct.
104 * @param nameStruct a C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700105 */
106 void set(struct ndn_Name &nameStruct);
107
108 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700109 * Add a new component, copying from value of length valueLength.
110 */
111 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -0700112 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700113 }
114
115 /**
116 * Clear all the components.
117 */
118 void clear() {
119 components_.clear();
120 }
121
122 /**
123 * Get the number of components.
124 * @return the number of components
125 */
126 unsigned int getComponentCount() {
127 return components_.size();
128 }
129
Jeff Thompson443398d2013-07-02 19:45:46 -0700130 const NameComponent &getComponent(unsigned int i) const { return components_[i]; }
131
Jeff Thompsone6063512013-07-01 15:11:28 -0700132 /**
133 * Encode this name as a URI.
134 * @return the encoded URI.
135 */
136 std::string to_uri();
137
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700138private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700139 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700140};
141
142}
143
144#endif
145