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 | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "c/name.h" |
| 12 | #include "encoding/binary-xml-wire-format.hpp" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 15 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 16 | class Name { |
| 17 | public: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 18 | 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 | { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 53 | componentStruct.valueLength = value_.size(); |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 54 | if (value_.size() > 0) |
| 55 | componentStruct.value = (unsigned char *)&value_[0]; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme. |
| 60 | * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and |
| 61 | * the component should be skipped in a URI name. |
| 62 | * @param first Pointer to the beginning of the escaped string |
| 63 | * @param last Pointer to the first character past the end of the escaped string |
| 64 | * @return True for success, false if escapedString is not a valid escaped component. |
| 65 | */ |
| 66 | bool setFromEscapedString(const char *first, const char *last); |
| 67 | |
| 68 | const std::vector<unsigned char> &getValue() const { return value_; } |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Set this component to the encoded segment number. |
| 72 | * @param segment The segment number. |
| 73 | */ |
| 74 | void setSegment(unsigned long segment); |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | std::vector<unsigned char> value_; |
| 78 | }; |
| 79 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Create a new Name with no components. |
| 82 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 83 | Name() { |
| 84 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 87 | * Create a new Name, copying the name components. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 88 | * @param components A vector of Component |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 89 | */ |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 90 | Name(const std::vector<Component> &components) |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 91 | : components_(components) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 96 | * 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] | 97 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 98 | */ |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 99 | Name(const char *uri) |
| 100 | { |
| 101 | set(uri); |
| 102 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 103 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 104 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 105 | * Set the nameStruct to point to the components in this name, without copying any memory. |
| 106 | * 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] | 107 | * @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] | 108 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 109 | void get(struct ndn_Name &nameStruct) const; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Jeff Thompson | 8b27e3a | 2013-07-03 18:19:53 -0700 | [diff] [blame] | 112 | * 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] | 113 | * @param nameStruct A C ndn_Name struct |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 114 | */ |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 115 | void set(const struct ndn_Name &nameStruct); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 118 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 119 | * @param uri The URI string. |
| 120 | */ |
| 121 | void set(const char *uri); |
| 122 | |
| 123 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 124 | * Add a new component, copying from value of length valueLength. |
| 125 | */ |
| 126 | void addComponent(unsigned char *value, unsigned int valueLength) { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 127 | components_.push_back(Component(value, valueLength)); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 128 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame^] | 129 | |
| 130 | /** |
| 131 | * Add a new component, copying from value. |
| 132 | */ |
| 133 | void addComponent(const std::vector<unsigned char> &value) { |
| 134 | components_.push_back(value); |
| 135 | } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Clear all the components. |
| 139 | */ |
| 140 | void clear() { |
| 141 | components_.clear(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the number of components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 146 | * @return The number of components. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 147 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 148 | unsigned int getComponentCount() const { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 149 | return components_.size(); |
| 150 | } |
| 151 | |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 152 | const Component &getComponent(unsigned int i) const { return components_[i]; } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 153 | |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 156 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 157 | */ |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 158 | std::string toUri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 159 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 160 | /** |
| 161 | * @deprecated Use toUri(). |
| 162 | */ |
| 163 | std::string to_uri() const |
| 164 | { |
| 165 | return toUri(); |
| 166 | } |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Append a component with the encoded segment number. |
| 170 | * @param segment The segment number. |
| 171 | */ |
| 172 | void appendSegment(unsigned long segment) |
| 173 | { |
| 174 | components_.push_back(Component()); |
| 175 | components_.back().setSegment(segment); |
| 176 | } |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 177 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 178 | private: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 179 | std::vector<Component> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } |
| 183 | |
| 184 | #endif |
| 185 | |