blob: 67ed2b6036b90664cfc3c22df403cb30351a7e02 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07004 */
5
6#ifndef NDN_NAME_HPP
7#define NDN_NAME_HPP
8
9#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070010#include <string>
Jeff Thompson016ed642013-07-02 14:39:06 -070011#include "c/Name.h"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012#include "encoding/BinaryXMLWireFormat.hpp"
13
14namespace ndn {
15
Jeff Thompson016ed642013-07-02 14:39:06 -070016class NameComponent {
17public:
Jeff Thompson443398d2013-07-02 19:45:46 -070018 NameComponent()
19 {
20 }
21
Jeff Thompson016ed642013-07-02 14:39:06 -070022 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 Thompsond345a5b2013-07-08 16:18:23 -070032 void get(struct ndn_NameComponent &componentStruct) const
Jeff Thompson016ed642013-07-02 14:39:06 -070033 {
Jeff Thompsond345a5b2013-07-08 16:18:23 -070034 componentStruct.value = (unsigned char *)&value_[0];
Jeff Thompson016ed642013-07-02 14:39:06 -070035 componentStruct.valueLength = value_.size();
36 }
37
Jeff Thompson443398d2013-07-02 19:45:46 -070038 /**
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 Thompson016ed642013-07-02 14:39:06 -070048 const std::vector<unsigned char> &getValue() const { return value_; }
49
50private:
51 std::vector<unsigned char> value_;
52};
53
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070054class Name {
55public:
Jeff Thompson443398d2013-07-02 19:45:46 -070056 /**
57 * Create a new Name with no components.
58 */
Jeff Thompson016ed642013-07-02 14:39:06 -070059 Name() {
60 }
Jeff Thompson443398d2013-07-02 19:45:46 -070061
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 Thompsona234d5d2013-07-26 16:46:22 -070066 Name(const char *uri);
Jeff Thompsone5f839b2013-06-28 12:50:38 -070067 /**
Jeff Thompson016ed642013-07-02 14:39:06 -070068 * Set the nameStruct to point to the components in this name, without copying any memory.
69 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
70 * @param nameStruct a C ndn_Name struct where the components array is already allocated.
71 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -070072 void get(struct ndn_Name &nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -070073
74 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -070075 * Clear this name, and set the components by copying from the name struct.
76 * @param nameStruct a C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -070077 */
Jeff Thompsondd3d2292013-07-10 11:59:44 -070078 void set(const struct ndn_Name &nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -070079
80 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -070081 * Add a new component, copying from value of length valueLength.
82 */
83 void addComponent(unsigned char *value, unsigned int valueLength) {
Jeff Thompson016ed642013-07-02 14:39:06 -070084 components_.push_back(NameComponent(value, valueLength));
Jeff Thompsone5f839b2013-06-28 12:50:38 -070085 }
86
87 /**
88 * Clear all the components.
89 */
90 void clear() {
91 components_.clear();
92 }
93
94 /**
95 * Get the number of components.
96 * @return the number of components
97 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -070098 unsigned int getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -070099 return components_.size();
100 }
101
Jeff Thompson443398d2013-07-02 19:45:46 -0700102 const NameComponent &getComponent(unsigned int i) const { return components_[i]; }
103
Jeff Thompsone6063512013-07-01 15:11:28 -0700104 /**
105 * Encode this name as a URI.
106 * @return the encoded URI.
107 */
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700108 std::string to_uri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700109
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700110private:
Jeff Thompson016ed642013-07-02 14:39:06 -0700111 std::vector<NameComponent> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700112};
113
114}
115
116#endif
117