diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2009-10-20 18:14:34 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2009-10-20 18:14:34 (GMT) |
commit | fa406155ab6ab2903d856f8e20502caef2df0996 (patch) | |
tree | 007847dac319bc49648e739b11bbc9ef86fd8068 /c++/src | |
parent | a99a73d20ecd7ed2c9c65a2979f600f805d01099 (diff) | |
download | hdf5-fa406155ab6ab2903d856f8e20502caef2df0996.zip hdf5-fa406155ab6ab2903d856f8e20502caef2df0996.tar.gz hdf5-fa406155ab6ab2903d856f8e20502caef2df0996.tar.bz2 |
[svn-r17690] Purpose: Code improvement
Description:
Fixed CommonFG::getComment and CommonFG::getLinkval to provide
default values for buffer size to improve usability.
Added test file tlinks.cpp, which only contains test for getLinkval
and will expand when C++ wrappers for H5L functions are implemented.
Platforms tested:
Linux/32 2.6 (jam)
FreeBSD/64 6.3 (liberty)
SunOS 5.10 (linew)
Diffstat (limited to 'c++/src')
-rw-r--r-- | c++/src/H5CommonFG.cpp | 106 | ||||
-rw-r--r-- | c++/src/H5CommonFG.h | 11 |
2 files changed, 53 insertions, 64 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. diff --git a/c++/src/H5CommonFG.h b/c++/src/H5CommonFG.h index 87c0615..d472d42 100644 --- a/c++/src/H5CommonFG.h +++ b/c++/src/H5CommonFG.h @@ -50,9 +50,8 @@ class H5_DLLCPP CommonFG { DataSet openDataSet(const H5std_string& name) const; // Retrieves comment for the HDF5 object specified by its name. - H5std_string getComment(const H5std_string& name) const; - H5std_string getComment(const char* name, size_t bufsize) const; - H5std_string getComment(const H5std_string& name, size_t bufsize) const; + H5std_string getComment(const char* name, size_t bufsize=256) const; + H5std_string getComment(const H5std_string& name, size_t bufsize=256) const; // Removes the comment for the HDF5 object specified by its name. void removeComment(const char* name) const; @@ -62,9 +61,9 @@ class H5_DLLCPP CommonFG { void setComment(const char* name, const char* comment) const; void setComment(const H5std_string& name, const H5std_string& comment) const; - // Returns the name of the HDF5 object that the symbolic link points to. - H5std_string getLinkval(const char* name, size_t size) const; - H5std_string getLinkval(const H5std_string& name, size_t size) const; + // Returns the value of a symbolic link. + H5std_string getLinkval(const char* link_name, size_t size=0) const; + H5std_string getLinkval(const H5std_string& link_name, size_t size=0) const; // Returns the number of objects in this group. hsize_t getNumObjs() const; |