summaryrefslogtreecommitdiffstats
path: root/c++/src/H5CommonFG.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c++/src/H5CommonFG.cpp')
-rw-r--r--c++/src/H5CommonFG.cpp106
1 files changed, 48 insertions, 58 deletions
diff --git a/c++/src/H5CommonFG.cpp b/c++/src/H5CommonFG.cpp
index 80efc5d..f9311c6 100644
--- a/c++/src/H5CommonFG.cpp
+++ b/c++/src/H5CommonFG.cpp
@@ -15,11 +15,6 @@
#include <string>
-// remove when done
-#include <iostream>
- using std::cerr;
- using std::endl;
-
#include "H5Include.h"
#include "H5Exception.h"
#include "H5IdComponent.h"
@@ -430,16 +425,37 @@ void CommonFG::getObjinfo( const H5std_string& name, H5G_stat_t& statbuf ) const
//--------------------------------------------------------------------------
H5std_string CommonFG::getLinkval( const char* name, size_t size ) const
{
- char* value_C = new char[size+1]; // temporary C-string for C API
+ H5L_info_t linkinfo;
+ char *value_C; // value in C string
+ size_t val_size = size;
+ H5std_string value = "";
+ herr_t ret_value;
- herr_t ret_value = H5Lget_val( getLocId(), name, value_C, size, H5P_DEFAULT );
- if( ret_value < 0 )
- {
- throwException("getLinkval", "H5Lget_val failed");
- }
- H5std_string value = H5std_string( value_C );
- delete []value_C;
- return( value );
+ // if user doesn't provide buffer size, determine it
+ if (size == 0)
+ {
+ ret_value = H5Lget_info(getLocId(), name, &linkinfo, H5P_DEFAULT);
+ if( ret_value < 0 )
+ {
+ throwException("getLinkval", "H5Lget_info to find buffer size failed");
+ }
+ val_size = linkinfo.u.val_size;
+ }
+
+ // if link has value, retrieve the value, otherwise, return null string
+ if (val_size > 0)
+ {
+ value_C = new char[val_size+1]; // temporary C-string for C API
+
+ ret_value = H5Lget_val(getLocId(), name, value_C, val_size, H5P_DEFAULT);
+ if( ret_value < 0 )
+ {
+ throwException("getLinkval", "H5Lget_val failed");
+ }
+ value = H5std_string(value_C);
+ delete []value_C;
+ }
+ return(value);
}
//--------------------------------------------------------------------------
@@ -529,33 +545,38 @@ void CommonFG::removeComment(const H5std_string& name) const
//--------------------------------------------------------------------------
// Function: CommonFG::getComment
-///\brief Retrieves comment for the specified object.
+///\brief Retrieves comment for the specified object and its comment's
+/// length.
///\param name - IN: Name of the object
+///\param bufsize - IN: Length of the comment to retrieve
///\return Comment string
///\exception H5::FileIException or H5::GroupIException
-// Programmer Binh-Minh Ribler - May 2005
+// Programmer Binh-Minh Ribler - 2000
// 2007: QAK modified to use H5O APIs; however the first parameter is
// no longer just file or group, this function should be moved
// to another class to accommodate attribute, dataset, and named
// datatype. - BMR
//--------------------------------------------------------------------------
-H5std_string CommonFG::getComment (const H5std_string& name) const
+H5std_string CommonFG::getComment( const char* name, size_t bufsize ) const
{
- size_t bufsize = 256; // anticipating the comment's length
+ // bufsize is default to 256
+ // temporary variable
hid_t loc_id = getLocId(); // temporary variable
- // temporary C-string for the object's comment
- char* comment_C = new char[bufsize+1];
- ssize_t ret_value = H5Oget_comment_by_name(loc_id, name.c_str(), comment_C, bufsize, H5P_DEFAULT);
+ // temporary C-string for the object's comment; bufsize already including
+ // null character
+ char* comment_C = new char[bufsize];
+ ssize_t ret_value = H5Oget_comment_by_name(loc_id, name, comment_C, bufsize, H5P_DEFAULT);
- // if the actual length of the comment is longer than the anticipated
- // value, then call H5Oget_comment_by_name again with the correct value
- if ((size_t)ret_value > bufsize)
+ // if the actual length of the comment is longer than bufsize and bufsize
+ // was the default value, i.e., not given by the user, then call
+ // H5Oget_comment_by_name again with the correct value
+ if ((size_t)ret_value > bufsize && bufsize == 256)
{
- bufsize = ret_value;
+ size_t new_size = ret_value;
delete []comment_C;
- comment_C = new char[bufsize+1];
- ret_value = H5Oget_comment_by_name(loc_id, name.c_str(), comment_C, bufsize, H5P_DEFAULT);
+ comment_C = new char[new_size]; // new_size including null terminator
+ ret_value = H5Oget_comment_by_name(loc_id, name, comment_C, new_size, H5P_DEFAULT);
}
// if H5Oget_comment_by_name returns SUCCEED, return the string comment,
@@ -571,37 +592,6 @@ H5std_string CommonFG::getComment (const H5std_string& name) const
//--------------------------------------------------------------------------
// Function: CommonFG::getComment
-///\brief Retrieves comment for the specified object and its comment's
-/// length.
-///\param name - IN: Name of the object
-///\param bufsize - IN: Length of the comment to retrieve
-///\return Comment string
-///\exception H5::FileIException or H5::GroupIException
-// Programmer Binh-Minh Ribler - 2000
-// 2007: QAK modified to use H5O APIs; however the first parameter is
-// no longer just file or group, this function should be moved
-// to another class to accommodate attribute, dataset, and named
-// datatype. - BMR
-//--------------------------------------------------------------------------
-H5std_string CommonFG::getComment( const char* name, size_t bufsize ) const
-{
- // temporary C-string for the object's comment
- char* comment_C = new char[bufsize+1];
-
- herr_t ret_value = H5Oget_comment_by_name( getLocId(), name, comment_C, bufsize, H5P_DEFAULT );
-
- // if H5Oget_comment_by_name returns SUCCEED, return the string comment
- if( ret_value < 0 )
- {
- throwException("getComment", "H5Oget_comment_by_name failed");
- }
- H5std_string comment = H5std_string(comment_C);
- delete []comment_C;
- return( comment );
-}
-
-//--------------------------------------------------------------------------
-// Function: CommonFG::getComment
///\brief This is an overloaded member function, provided for convenience.
/// It differs from the above function in that it takes an
/// \c std::string for \a name.