Implement Name(const char *uri)
diff --git a/ndn-cpp/Name.cpp b/ndn-cpp/Name.cpp
index fbb2675..fca3e05 100644
--- a/ndn-cpp/Name.cpp
+++ b/ndn-cpp/Name.cpp
@@ -99,7 +99,7 @@
trimLeft(str);
trimRight(str);
}
-
+
/**
* Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
* @param c
@@ -147,6 +147,85 @@
return result.str();
}
+bool NameComponent::setFromEscapedString(const char *first, const char *last)
+{
+ string trimmedString(first, last);
+ trim(trimmedString);
+ string component = unescape(trimmedString);
+
+ if (component.find_first_not_of(".") == string::npos) {
+ // Special case for component of only periods.
+ if (component.size() <= 2)
+ // Zero, one or two periods is illegal. Ignore this component.
+ return false;
+ else {
+ // Remove 3 periods.
+ value_.clear();
+ value_.insert(value_.begin(), component.begin() + 3, component.end());
+ }
+ }
+ else {
+ value_.clear();
+ value_.insert(value_.begin(), component.begin(), component.end());
+ }
+
+ return true;
+}
+
+Name::Name(const char *uri_cstr)
+{
+ string uri = uri_cstr;
+ trim(uri);
+ if (uri.size() == 0)
+ return;
+
+ size_t iColon = uri.find(':');
+ if (iColon != string::npos) {
+ // Make sure the colon came before a '/'.
+ size_t iFirstSlash = uri.find('/');
+ if (iFirstSlash == string::npos || iColon < iFirstSlash) {
+ // Omit the leading protocol such as ndn:
+ uri.erase(0, iColon + 1);
+ trim(uri);
+ }
+ }
+
+ // Trim the leading slash and possibly the authority.
+ if (uri[0] == '/') {
+ if (uri.size() >= 2 && uri[1] == '/') {
+ // Strip the authority following "//".
+ size_t iAfterAuthority = uri.find('/', 2);
+ if (iAfterAuthority == string::npos)
+ // Unusual case: there was only an authority.
+ return;
+ else {
+ uri.erase(0, iAfterAuthority + 1);
+ trim(uri);
+ }
+ }
+ else {
+ uri.erase(0, 1);
+ trim(uri);
+ }
+ }
+
+ size_t iComponentStart = 0;
+
+ // Unescape the components.
+ while (iComponentStart < uri.size()) {
+ size_t iComponentEnd = uri.find("/", iComponentStart);
+ if (iComponentEnd == string::npos)
+ iComponentEnd = uri.size();
+
+ components_.push_back(NameComponent());
+ if (!components_[components_.size() - 1].setFromEscapedString(&uri[iComponentStart], &uri[iComponentEnd]))
+ // Ignore the illegal component. This also gets rid of a trailing '/'.
+ components_.pop_back();
+
+ iComponentStart = iComponentEnd + 1;
+ }
+}
+
void Name::get(struct ndn_Name &nameStruct)
{
if (nameStruct.maxComponents < components_.size())
diff --git a/ndn-cpp/Name.hpp b/ndn-cpp/Name.hpp
index 48dd706..bd89377 100644
--- a/ndn-cpp/Name.hpp
+++ b/ndn-cpp/Name.hpp
@@ -8,6 +8,7 @@
#define NDN_NAME_HPP
#include <vector>
+#include <string>
#include "common.h"
#include "c/Name.h"
#include "encoding/BinaryXMLWireFormat.hpp"
@@ -16,6 +17,10 @@
class NameComponent {
public:
+ NameComponent()
+ {
+ }
+
NameComponent(unsigned char * value, unsigned int valueLen)
: value_(value, value + valueLen)
{
@@ -32,6 +37,16 @@
componentStruct.valueLength = value_.size();
}
+ /**
+ * Set this component value by decoding the escapedString between first and last according to the NDN URI Scheme.
+ * If the escaped string is "", "." or ".." then return false, which means this component value was not changed, and
+ * the component should be skipped in a URI name.
+ * @param first pointer to the beginning of the escaped string
+ * @param last pointer to the first character past the end of the escaped string
+ * @return true for success, false if escapedString is not a valid escaped component.
+ */
+ bool setFromEscapedString(const char *first, const char *last);
+
const std::vector<unsigned char> &getValue() const { return value_; }
private:
@@ -40,8 +55,16 @@
class Name {
public:
+ /**
+ * Create a new Name with no components.
+ */
Name() {
}
+
+ /**
+ * Parse the uri according to the NDN URI Scheme and create the name with the components.
+ * @param uri the URI string.
+ */
Name(const char *uri);
void encode(std::vector<unsigned char> &output, WireFormat &wireFormat)
@@ -104,6 +127,8 @@
return components_.size();
}
+ const NameComponent &getComponent(unsigned int i) const { return components_[i]; }
+
/**
* Encode this name as a URI.
* @return the encoded URI.
diff --git a/test/test-encode-decode-interest.cpp b/test/test-encode-decode-interest.cpp
index d32d966..448f413 100644
--- a/test/test-encode-decode-interest.cpp
+++ b/test/test-encode-decode-interest.cpp
@@ -48,7 +48,6 @@
unsigned char *encodingBuffer = &encoding[0];
unsigned int encodingLength = encoding.size();
cout << "Name encoding length " << encodingLength << " vs. sizeof(Name1) " << sizeof(Name1) << endl;
-
} catch (exception &e) {
cout << "exception " << e.what() << endl;
}