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 | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 18 | NameComponent() |
| 19 | { |
| 20 | } |
| 21 | |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 22 | NameComponent(unsigned char * value, unsigned int valueLen) |
| 23 | : value_(value, value + valueLen) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Set the componentStruct to point to this component, without copying any memory. |
| 29 | * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory. |
| 30 | * @param componentStruct the C ndn_NameComponent struct to receive the pointer. |
| 31 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 32 | void get(struct ndn_NameComponent &componentStruct) const |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 33 | { |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 34 | componentStruct.value = (unsigned char *)&value_[0]; |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 35 | componentStruct.valueLength = value_.size(); |
| 36 | } |
| 37 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme. |
| 40 | * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and |
| 41 | * the component should be skipped in a URI name. |
| 42 | * @param first pointer to the beginning of the escaped string |
| 43 | * @param last pointer to the first character past the end of the escaped string |
| 44 | * @return true for success, false if escapedString is not a valid escaped component. |
| 45 | */ |
| 46 | bool setFromEscapedString(const char *first, const char *last); |
| 47 | |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 48 | const std::vector<unsigned char> &getValue() const { return value_; } |
| 49 | |
| 50 | private: |
| 51 | std::vector<unsigned char> value_; |
| 52 | }; |
| 53 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 54 | class Name { |
| 55 | public: |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Create a new Name with no components. |
| 58 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 59 | Name() { |
| 60 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
| 64 | * @param uri the URI string. |
| 65 | */ |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 66 | Name(const char *uri); |
| 67 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 68 | void encode(std::vector<unsigned char> &output, WireFormat &wireFormat) const |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 69 | { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 70 | wireFormat.encodeName(*this, output); |
| 71 | } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 72 | void encode(std::vector<unsigned char> &output) const |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 73 | { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 74 | encode(output, BinaryXMLWireFormat::instance()); |
| 75 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 76 | void decode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
| 77 | { |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 78 | wireFormat.decodeName(*this, input, inputLength); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 79 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 80 | void decode(const unsigned char *input, unsigned int inputLength) |
| 81 | { |
Jeff Thompson | 4238071 | 2013-06-28 10:59:33 -0700 | [diff] [blame] | 82 | decode(input, inputLength, BinaryXMLWireFormat::instance()); |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 83 | } |
Jeff Thompson | 2a749d1 | 2013-07-02 15:03:08 -0700 | [diff] [blame] | 84 | void decode(const std::vector<unsigned char> &input, WireFormat &wireFormat) |
| 85 | { |
| 86 | decode(&input[0], input.size(), wireFormat); |
| 87 | } |
| 88 | void decode(const std::vector<unsigned char> &input) |
| 89 | { |
| 90 | decode(&input[0], input.size()); |
| 91 | } |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 92 | |
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. |
| 96 | * @param nameStruct a C ndn_Name struct where the components array is already allocated. |
| 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. |
| 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. |
| 122 | * @return the number of components |
| 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. |
| 132 | * @return the encoded URI. |
| 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 | |