blob: b4a8dbe32fabba970549ef4960a4b9b70e76b8eb [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 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_; }
68
69 private:
70 std::vector<unsigned char> value_;
71 };
72
Jeff Thompson443398d2013-07-02 19:45:46 -070073 /**
74 * Create a new Name with no components.
75 */
Jeff Thompson016ed642013-07-02 14:39:06 -070076 Name() {
77 }
Jeff Thompson443398d2013-07-02 19:45:46 -070078
79 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -070080 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070081 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -070082 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070083 Name(const std::vector<Component> &components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -070084 : components_(components)
85 {
86 }
87
88 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070089 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070090 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070091 */
Jeff Thompsona234d5d2013-07-26 16:46:22 -070092 Name(const char *uri);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -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.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070097 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -070098 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -070099 void get(struct ndn_Name &nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700100
101 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700102 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700103 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700104 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -0700105 void set(const struct ndn_Name &nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700106
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 Thompson5a6b5ab2013-08-05 15:43:47 -0700111 components_.push_back(Component(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.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700123 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700124 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700125 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700126 return components_.size();
127 }
128
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700129 const Component &getComponent(unsigned int i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700130
Jeff Thompsone6063512013-07-01 15:11:28 -0700131 /**
132 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700133 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700134 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700135 std::string to_uri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700136
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700137private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700138 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700139};
140
141}
142
143#endif
144