blob: fbb28d0a73124affe6d081f55ce71e53af627dbf [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
7#define NDN_NAME_HPP
8
9#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070010#include <string>
Jeff Thompson016ed642013-07-02 14:39:06 -070011#include "c/Name.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012#include "encoding/BinaryXMLWireFormat.hpp"
13
14namespace ndn {
15
Jeff Thompson016ed642013-07-02 14:39:06 -070016class NameComponent {
17public:
Jeff Thompson443398d2013-07-02 19:45:46 -070018 NameComponent()
19 {
20 }
21
Jeff Thompson016ed642013-07-02 14:39:06 -070022 NameComponent(unsigned char * value, unsigned int valueLen)
23 : value_(value, value + valueLen)
24 {
25 }
26
27 /**
28 * Set the componentStruct to point to this component, without copying any memory.
29 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
30 * @param componentStruct the C ndn_NameComponent struct to receive the pointer.
31 */
32 void get(struct ndn_NameComponent &componentStruct)
33 {
34 componentStruct.value = &value_[0];
35 componentStruct.valueLength = value_.size();
36 }
37
Jeff Thompson443398d2013-07-02 19:45:46 -070038 /**
39 * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
40 * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
41 * the component should be skipped in a URI name.
42 * @param first pointer to the beginning of the escaped string
43 * @param last pointer to the first character past the end of the escaped string
44 * @return true for success, false if escapedString is not a valid escaped component.
45 */
46 bool setFromEscapedString(const char *first, const char *last);
47
Jeff Thompson016ed642013-07-02 14:39:06 -070048 const std::vector<unsigned char> &getValue() const { return value_; }
49
50private:
51 std::vector<unsigned char> value_;
52};
53
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070054class Name {
55public:
Jeff Thompson443398d2013-07-02 19:45:46 -070056 /**
57 * Create a new Name with no components.
58 */
Jeff Thompson016ed642013-07-02 14:39:06 -070059 Name() {
60 }
Jeff Thompson443398d2013-07-02 19:45:46 -070061
62 /**
63 * Parse the uri according to the NDN URI Scheme and create the name with the components.
64 * @param uri the URI string.
65 */
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070066 Name(const char *uri);
67
Jeff Thompson2a749d12013-07-02 15:03:08 -070068 void encode(std::vector<unsigned char> &output, WireFormat &wireFormat)
69 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070070 wireFormat.encodeName(*this, output);
71 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070072 void encode(std::vector<unsigned char> &output)
73 {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070074 encode(output, BinaryXMLWireFormat::instance());
75 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070076 void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat)
77 {
Jeff Thompson42380712013-06-28 10:59:33 -070078 wireFormat.decodeName(*this, input, inputLength);
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070079 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070080 void decode(const unsigned char *input, unsigned int inputLength)
81 {
Jeff Thompson42380712013-06-28 10:59:33 -070082 decode(input, inputLength, BinaryXMLWireFormat::instance());
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070083 }
Jeff Thompson2a749d12013-07-02 15:03:08 -070084 void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat)
85 {
86 decode(&input[0], input.size(), wireFormat);
87 }
88 void decode(const std::vector<unsigned char> &input)
89 {
90 decode(&input[0], input.size());
91 }
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070092
Jeff Thompsone5f839b2013-06-28 12:50:38 -070093 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070094 * Set the nameStruct to point to the components in this name, without copying any memory.
95 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
96 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
97 */
98 void get(struct ndn_Name &nameStruct);
99
100 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700101 * Clear this name, and set the components by copying from the name struct.
102 * @param nameStruct a C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700103 */
104 void set(struct ndn_Name &nameStruct);
105
106 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700107 * Add a new component, copying from value of length valueLength.
108 */
109 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -0700110 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700111 }
112
113 /**
114 * Clear all the components.
115 */
116 void clear() {
117 components_.clear();
118 }
119
120 /**
121 * Get the number of components.
122 * @return the number of components
123 */
124 unsigned int getComponentCount() {
125 return components_.size();
126 }
127
Jeff Thompson443398d2013-07-02 19:45:46 -0700128 const NameComponent &getComponent(unsigned int i) const { return components_[i]; }
129
Jeff Thompsone6063512013-07-01 15:11:28 -0700130 /**
131 * Encode this name as a URI.
132 * @return the encoded URI.
133 */
134 std::string to_uri();
135
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700136private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700137 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700138};
139
140}
141
142#endif
143