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