Added get(struct ndn_Name &)
diff --git a/ndn-cpp/Name.cpp b/ndn-cpp/Name.cpp
index 5a62801..c777ebe 100644
--- a/ndn-cpp/Name.cpp
+++ b/ndn-cpp/Name.cpp
@@ -19,17 +19,29 @@
void Name::set(struct ndn_Name &nameStruct)
{
clear();
- for (int i = 0; i < nameStruct.nComponents; ++i)
+ for (unsigned int i = 0; i < nameStruct.nComponents; ++i)
addComponent(nameStruct.components[i].value, nameStruct.components[i].valueLength);
}
+void Name::get(struct ndn_Name &nameStruct)
+{
+ if (nameStruct.maxComponents < components_.size())
+ throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
+
+ nameStruct.nComponents = components_.size();
+ for (unsigned int i = 0; i < nameStruct.nComponents; ++i) {
+ nameStruct.components[i].value = &components_[i][0];
+ nameStruct.components[i].valueLength = components_[i].size();
+ }
+}
+
std::string Name::to_uri()
{
// TODO: implement fully.
ostringstream output;
- for (int i = 0; i < components_.size(); ++i) {
+ for (unsigned int i = 0; i < components_.size(); ++i) {
output << "/";
- for (int j = 0; j < components_[i].size(); ++j)
+ for (unsigned int j = 0; j < components_[i].size(); ++j)
output << components_[i][j];
}
diff --git a/ndn-cpp/Name.hpp b/ndn-cpp/Name.hpp
index eacd420..802aaae 100644
--- a/ndn-cpp/Name.hpp
+++ b/ndn-cpp/Name.hpp
@@ -40,6 +40,13 @@
void set(struct ndn_Name &nameStruct);
/**
+ * Set the nameStruct to point to the components in this name, without copying any memory.
+ * WARNING: The resulting pointers in nameStruct are invalid after a further use of this name which could reallocate the components memory.
+ * @param nameStruct a C ndn_Name struct where the components array is already allocated.
+ */
+ void get(struct ndn_Name &nameStruct);
+
+ /**
* Add a new component, copying from value of length valueLength.
*/
void addComponent(unsigned char *value, unsigned int valueLength) {