summaryrefslogtreecommitdiffstats
path: root/c++/src/H5Object.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c++/src/H5Object.cpp')
-rw-r--r--c++/src/H5Object.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/c++/src/H5Object.cpp b/c++/src/H5Object.cpp
index 1d96f2e..94b03ab 100644
--- a/c++/src/H5Object.cpp
+++ b/c++/src/H5Object.cpp
@@ -31,6 +31,7 @@
#include "H5File.h"
#include "H5DataSet.h"
#include "H5Attribute.h"
+#include "H5private.h" // for HDmemset
#ifndef H5_NO_NAMESPACE
namespace H5 {
@@ -53,6 +54,117 @@ H5Object::H5Object() : H5Location() {}
H5Object::H5Object( const hid_t object_id ) : H5Location( object_id ) {}
//--------------------------------------------------------------------------
+// Function: getObjName
+///\brief Given an id, returns the type of the object.
+///\return The name of the object
+// Programmer Binh-Minh Ribler - Mar, 2014
+//--------------------------------------------------------------------------
+ssize_t H5Object::getObjName(char *obj_name, size_t buf_size) const
+{
+ // H5Iget_name will get buf_size-1 chars of the name to null terminate it
+ ssize_t name_size = H5Iget_name(getId(), obj_name, buf_size);
+
+ // If H5Iget_name returns a negative value, raise an exception
+ if (name_size < 0)
+ {
+ throw Exception(inMemFunc("getObjName"), "H5Iget_name failed");
+ }
+ else if (name_size == 0)
+ {
+ throw Exception(inMemFunc("getObjName"), "Object must have a name, but name length is 0");
+ }
+ // Return length of the name
+ return(name_size);
+}
+
+//--------------------------------------------------------------------------
+// Function: H5Object::getObjName
+///\brief Returns the name of this object as an \a H5std_string.
+///\return Name of the object
+///\exception H5::Exception
+// Programmer Binh-Minh Ribler - Mar, 2014
+// Modification
+//--------------------------------------------------------------------------
+H5std_string H5Object::getObjName() const
+{
+ H5std_string obj_name(""); // object name to return
+
+ // Preliminary call to get the size of the object name
+ ssize_t name_size = H5Iget_name(getId(), NULL, (size_t)0);
+
+ // If H5Iget_name failed, throw exception
+ if (name_size < 0)
+ {
+ throw Exception(inMemFunc("getObjName"), "H5Iget_name failed");
+ }
+ else if (name_size == 0)
+ {
+ throw Exception(inMemFunc("getObjName"), "Object must have a name, but name length is 0");
+ }
+ // Object's name exists, retrieve it
+ else if (name_size > 0)
+ {
+ char* name_C = new char[name_size+1]; // temporary C-string
+ HDmemset(name_C, 0, name_size+1); // clear buffer
+
+ // Use overloaded function
+ name_size = getObjName(name_C, name_size+1);
+
+ // Convert the C object name to return
+ obj_name = name_C;
+
+ // Clean up resource
+ delete []name_C;
+ }
+ // Return object's name
+ return(obj_name);
+}
+
+//--------------------------------------------------------------------------
+// Function: H5Object::getObjName
+///\brief Gets the name of this object, returning its length.
+///\param obj_name - OUT: Buffer for the name string as \a H5std_string
+///\param len - IN: Desired length of the name, default to 0
+///\return Actual length of the object name
+///\exception H5::Exception
+///\par Description
+/// This function retrieves the object's name as an std string.
+/// buf_size can specify a specific length or default to 0, in
+/// which case the entire name will be retrieved.
+// Programmer Binh-Minh Ribler - Mar, 2014
+//--------------------------------------------------------------------------
+ssize_t H5Object::getObjName(H5std_string& obj_name, size_t len) const
+{
+ ssize_t name_size = 0;
+
+ // If no length is provided, get the entire object name
+ if (len == 0)
+ {
+ obj_name = getObjName();
+ name_size = obj_name.length();
+ }
+ // If length is provided, get that number of characters in name
+ else
+ {
+ char* name_C = new char[len+1]; // temporary C-string
+ HDmemset(name_C, 0, len+1); // clear buffer
+
+ // Use overloaded function
+ name_size = getObjName(name_C, len+1);
+
+ // Convert the C object name to return
+ obj_name = name_C;
+
+ // Clean up resource
+ delete []name_C;
+ }
+ // Otherwise, keep obj_name intact
+
+ // Return name size
+ return(name_size);
+}
+
+//--------------------------------------------------------------------------
// Function: H5Object copy constructor
///\brief Copy constructor: makes a copy of the original H5Object
/// instance.