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