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 |
| 7 | #define NDN_NAME_HPP |
| 8 | |
| 9 | #include <vector> |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 10 | #include <string> |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 11 | #include "c/Name.h" |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | #include "encoding/BinaryXMLWireFormat.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 16 | class NameComponent { |
| 17 | public: |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 18 | /** |
| 19 | * Create a new NameComponent with an empty value. |
| 20 | */ |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 21 | NameComponent() |
| 22 | { |
| 23 | } |
| 24 | |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 25 | /** |
| 26 | * Create a new NameComponent, copying the given value. |
| 27 | * @param value The value byte array. |
| 28 | */ |
| 29 | NameComponent(const std::vector<unsigned char> &value) |
| 30 | : value_(value) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Create a new NameComponent, copying the given value. |
| 36 | * @param value Pointer to the value byte array. |
| 37 | * @param valueLen Length of value. |
| 38 | */ |
| 39 | NameComponent(unsigned char *value, unsigned int valueLen) |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 40 | : value_(value, value + valueLen) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the componentStruct to point to this component, without copying any memory. |
| 46 | * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 47 | * @param componentStruct The C ndn_NameComponent struct to receive the pointer. |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 48 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 49 | void get(struct ndn_NameComponent &componentStruct) const |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 50 | { |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 51 | componentStruct.value = (unsigned char *)&value_[0]; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 52 | componentStruct.valueLength = value_.size(); |
| 53 | } |
| 54 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme. |
| 57 | * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and |
| 58 | * the component should be skipped in a URI name. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 59 | * @param first Pointer to the beginning of the escaped string |
| 60 | * @param last Pointer to the first character past the end of the escaped string |
| 61 | * @return True for success, false if escapedString is not a valid escaped component. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 62 | */ |
| 63 | bool setFromEscapedString(const char *first, const char *last); |
| 64 | |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 65 | const std::vector<unsigned char> &getValue() const { return value_; } |
| 66 | |
| 67 | private: |
| 68 | std::vector<unsigned char> value_; |
| 69 | }; |
| 70 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 71 | class Name { |
| 72 | public: |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Create a new Name with no components. |
| 75 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 76 | Name() { |
| 77 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 80 | * Create a new Name, copying the name components. |
| 81 | * @param components A vector of NameComponent |
| 82 | */ |
| 83 | Name(const std::vector<NameComponent> &components) |
| 84 | : components_(components) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 89 | * 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^] | 90 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 91 | */ |
Jeff Thompson | a234d5d | 2013-07-26 16:46:22 -0700 | [diff] [blame] | 92 | Name(const char *uri); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 93 | /** |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 94 | * Set the nameStruct to point to the components in this name, without copying any memory. |
| 95 | * 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^] | 96 | * @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] | 97 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 98 | void get(struct ndn_Name &nameStruct) const; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Jeff Thompson | 8b27e3a | 2013-07-03 18:19:53 -0700 | [diff] [blame] | 101 | * 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^] | 102 | * @param nameStruct A C ndn_Name struct |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 103 | */ |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 104 | void set(const struct ndn_Name &nameStruct); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 105 | |
| 106 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 107 | * Add a new component, copying from value of length valueLength. |
| 108 | */ |
| 109 | void addComponent(unsigned char *value, unsigned int valueLength) { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 110 | components_.push_back(NameComponent(value, valueLength)); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Clear all the components. |
| 115 | */ |
| 116 | void clear() { |
| 117 | components_.clear(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the number of components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 122 | * @return The number of components. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 123 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 124 | unsigned int getComponentCount() const { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 125 | return components_.size(); |
| 126 | } |
| 127 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 128 | const NameComponent &getComponent(unsigned int i) const { return components_[i]; } |
| 129 | |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame^] | 132 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 133 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 134 | std::string to_uri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 135 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 136 | private: |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 137 | std::vector<NameComponent> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } |
| 141 | |
| 142 | #endif |
| 143 | |