summaryrefslogtreecommitdiffstats
path: root/c++/src/H5Object.cpp
diff options
context:
space:
mode:
authorBinh-Minh Ribler <bmribler@hdfgroup.org>2014-04-06 22:36:15 (GMT)
committerBinh-Minh Ribler <bmribler@hdfgroup.org>2014-04-06 22:36:15 (GMT)
commita08f75b0737c86d51bbc91538e78b1455eb2cae7 (patch)
treea1e1c4f1cf8ddda6dd0a101af17dc8524ad23050 /c++/src/H5Object.cpp
parent70daa61a876274a92c0d43ec0116d68e35d0c2ce (diff)
downloadhdf5-a08f75b0737c86d51bbc91538e78b1455eb2cae7.zip
hdf5-a08f75b0737c86d51bbc91538e78b1455eb2cae7.tar.gz
hdf5-a08f75b0737c86d51bbc91538e78b1455eb2cae7.tar.bz2
[svn-r24969] Description:
- Added wrappers to H5Object for H5Iget_name() to get object's name ssize_t getObjName(char *obj_name, size_t buf_size = 0) const; ssize_t getObjName(H5std_string& obj_name, size_t len = 0) const; H5std_string getObjName() const; - Added tests tobject.cpp - Added to various cleanup_* functions in tests to remove generated files - Added an overload H5I_type_t getHDFObjType() to get object's type Platforms tested: Linux/ppc64 (ostrich) Linux/32 2.6 (jam) SunOS 5.11 (emu) with gmake
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.