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