Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef NDN_NAME_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_NAME_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 8 | |
| 9 | #include <vector> |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 10 | #include <string> |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 11 | #include <sstream> |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 12 | #include "c/name.h" |
| 13 | #include "encoding/binary-xml-wire-format.hpp" |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 14 | #include "util/blob.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 17 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 18 | class Name { |
| 19 | public: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 20 | class Component { |
| 21 | public: |
| 22 | /** |
| 23 | * Create a new Name::Component with an empty value. |
| 24 | */ |
| 25 | Component() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Create a new Name::Component, copying the given value. |
| 31 | * @param value The value byte array. |
| 32 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 33 | Component(const std::vector<unsigned char>& value) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 34 | : value_(value) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Create a new Name::Component, copying the given value. |
| 40 | * @param value Pointer to the value byte array. |
| 41 | * @param valueLen Length of value. |
| 42 | */ |
Jeff Thompson | a166b73 | 2013-08-27 16:09:29 -0700 | [diff] [blame] | 43 | Component(const unsigned char *value, unsigned int valueLen) |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 44 | : value_(value, valueLen) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set the componentStruct to point to this component, without copying any memory. |
| 50 | * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory. |
| 51 | * @param componentStruct The C ndn_NameComponent struct to receive the pointer. |
| 52 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 53 | void get(struct ndn_NameComponent& componentStruct) const |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 55 | componentStruct.valueLength = value_.size(); |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 56 | if (value_.size() > 0) |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 57 | componentStruct.value = (unsigned char*)value_.buf(); |
Jeff Thompson | 05c8c1b | 2013-09-12 12:47:57 -0700 | [diff] [blame] | 58 | else |
| 59 | componentStruct.value = 0; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme. |
| 64 | * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and |
| 65 | * the component should be skipped in a URI name. |
| 66 | * @param first Pointer to the beginning of the escaped string |
| 67 | * @param last Pointer to the first character past the end of the escaped string |
| 68 | * @return True for success, false if escapedString is not a valid escaped component. |
| 69 | */ |
| 70 | bool setFromEscapedString(const char *first, const char *last); |
| 71 | |
Jeff Thompson | 9bdb3b2 | 2013-09-12 12:42:13 -0700 | [diff] [blame] | 72 | const Blob& getValue() const { return value_; } |
| 73 | |
| 74 | void setValue(const Blob& value) { value_ = value; } |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Set this component to the encoded segment number. |
| 78 | * @param segment The segment number. |
| 79 | */ |
| 80 | void setSegment(unsigned long segment); |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
Jeff Thompson | 995aba5 | 2013-09-12 12:04:52 -0700 | [diff] [blame] | 83 | Blob value_; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Create a new Name with no components. |
| 88 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 89 | Name() { |
| 90 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 93 | * Create a new Name, copying the name components. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 94 | * @param components A vector of Component |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 95 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 96 | Name(const std::vector<Component>& components) |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 97 | : components_(components) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 102 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 103 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 104 | */ |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 105 | Name(const char *uri) |
| 106 | { |
| 107 | set(uri); |
| 108 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 109 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 110 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 111 | * Set the nameStruct to point to the components in this name, without copying any memory. |
| 112 | * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 113 | * @param nameStruct A C ndn_Name struct where the components array is already allocated. |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 114 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 115 | void get(struct ndn_Name& nameStruct) const; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Jeff Thompson | 8b27e3a | 2013-07-03 18:19:53 -0700 | [diff] [blame] | 118 | * Clear this name, and set the components by copying from the name struct. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 119 | * @param nameStruct A C ndn_Name struct |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 120 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 121 | void set(const struct ndn_Name& nameStruct); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 124 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 125 | * @param uri The URI string. |
| 126 | */ |
| 127 | void set(const char *uri); |
| 128 | |
| 129 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 130 | * Add a new component, copying from value of length valueLength. |
| 131 | */ |
Jeff Thompson | a166b73 | 2013-08-27 16:09:29 -0700 | [diff] [blame] | 132 | void addComponent(const unsigned char *value, unsigned int valueLength) { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 133 | components_.push_back(Component(value, valueLength)); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 134 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Add a new component, copying from value. |
| 138 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 139 | void addComponent(const std::vector<unsigned char>& value) { |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 140 | components_.push_back(value); |
| 141 | } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Clear all the components. |
| 145 | */ |
| 146 | void clear() { |
| 147 | components_.clear(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the number of components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 152 | * @return The number of components. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 153 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 154 | unsigned int getComponentCount() const { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 155 | return components_.size(); |
| 156 | } |
| 157 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 158 | const Component& getComponent(unsigned int i) const { return components_[i]; } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 159 | |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 162 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 163 | */ |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 164 | std::string toUri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 165 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 166 | /** |
| 167 | * @deprecated Use toUri(). |
| 168 | */ |
| 169 | std::string to_uri() const |
| 170 | { |
| 171 | return toUri(); |
| 172 | } |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Append a component with the encoded segment number. |
| 176 | * @param segment The segment number. |
| 177 | */ |
| 178 | void appendSegment(unsigned long segment) |
| 179 | { |
| 180 | components_.push_back(Component()); |
| 181 | components_.back().setSegment(segment); |
| 182 | } |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 183 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Check if the N components of this name are the same as the first N components of the given name. |
| 186 | * @param name The Name to check. |
| 187 | * @return true if this matches the given name, otherwise false. This always returns true if this name is empty. |
| 188 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 189 | bool match(const Name& name) const; |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Write the value to result, escaping characters according to the NDN URI Scheme. |
| 193 | * This also adds "..." to a value with zero or more ".". |
| 194 | * @param value the buffer with the value to escape |
| 195 | * @param result the string stream to write to. |
| 196 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 197 | static void toEscapedString(const std::vector<unsigned char>& value, std::ostringstream& result); |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 198 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 199 | private: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 200 | std::vector<Component> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } |
| 204 | |
| 205 | #endif |
| 206 | |