diff options
Diffstat (limited to 'c++/src/H5Location.cpp')
-rw-r--r-- | c++/src/H5Location.cpp | 134 |
1 files changed, 85 insertions, 49 deletions
diff --git a/c++/src/H5Location.cpp b/c++/src/H5Location.cpp index b413a17..fdbfd61 100644 --- a/c++/src/H5Location.cpp +++ b/c++/src/H5Location.cpp @@ -32,6 +32,7 @@ #include "H5File.h" #include "H5DataSet.h" #include "H5Attribute.h" +#include "H5private.h" // for HDmemset #ifndef H5_NO_NAMESPACE namespace H5 { @@ -355,7 +356,7 @@ void H5Location::flush(H5F_scope_t scope) const herr_t ret_value = H5Fflush(getId(), scope); if( ret_value < 0 ) { - throw Exception(inMemFunc("flush"), "H5Fflush failed"); + throw LocationException(inMemFunc("flush"), "H5Fflush failed"); } } @@ -363,7 +364,7 @@ void H5Location::flush(H5F_scope_t scope) const // Function: H5Location::getFileName ///\brief Gets the name of the file, in which this HDF5 object belongs. ///\return File name -///\exception H5::IdComponentException +///\exception H5::LocationException // Programmer Binh-Minh Ribler - Jul, 2004 //-------------------------------------------------------------------------- H5std_string H5Location::getFileName() const @@ -371,7 +372,7 @@ H5std_string H5Location::getFileName() const try { return(p_get_file_name()); } - catch (IdComponentException E) { + catch (LocationException E) { throw FileIException(inMemFunc("getFileName"), E.getDetailMsg()); } } @@ -381,7 +382,7 @@ H5std_string H5Location::getFileName() const ///\brief Sets or resets the comment for an object specified by its name. ///\param name - IN: Name of the object ///\param comment - IN: New comment -///\exception H5::Exception +///\exception H5::LocationException ///\par Description /// If \a comment is an empty string or a null pointer, the comment /// message is removed from the object. @@ -400,7 +401,7 @@ void H5Location::setComment(const char* name, const char* comment) const { herr_t ret_value = H5Oset_comment_by_name(getId(), name, comment, H5P_DEFAULT); if( ret_value < 0 ) - throw Exception(inMemFunc("setComment"), "H5Oset_comment_by_name failed"); + throw LocationException(inMemFunc("setComment"), "H5Oset_comment_by_name failed"); } //-------------------------------------------------------------------------- @@ -427,7 +428,7 @@ void H5Location::setComment(const char* comment) const { herr_t ret_value = H5Oset_comment_by_name(getId(), ".", comment, H5P_DEFAULT); if( ret_value < 0 ) - throw Exception(inMemFunc("setComment"), "H5Oset_comment_by_name failed"); + throw LocationException(inMemFunc("setComment"), "H5Oset_comment_by_name failed"); } //-------------------------------------------------------------------------- @@ -446,7 +447,7 @@ void H5Location::setComment(const H5std_string& comment) const // Function: H5Location::removeComment ///\brief Removes the comment from an object specified by its name. ///\param name - IN: Name of the object -///\exception H5::Exception +///\exception H5::LocationException // Programmer Binh-Minh Ribler - May 2005 (moved from CommonFG, Sep 2013) // 2007: QAK modified to use H5O APIs; however the first parameter is // no longer just file or group, this function should be moved @@ -457,7 +458,7 @@ void H5Location::removeComment(const char* name) const { herr_t ret_value = H5Oset_comment_by_name(getId(), name, NULL, H5P_DEFAULT); if( ret_value < 0 ) - throw Exception(inMemFunc("removeComment"), "H5Oset_comment_by_name failed"); + throw LocationException(inMemFunc("removeComment"), "H5Oset_comment_by_name failed"); } //-------------------------------------------------------------------------- @@ -474,52 +475,87 @@ void H5Location::removeComment(const H5std_string& name) const //-------------------------------------------------------------------------- // Function: H5Location::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 +///\brief Retrieves the comment for this location, returning its length. +///\param name - IN: Name of the object +///\param buf_size - IN: Length of the comment to retrieve +///\param comment - OUT: Retrieved comment +///\return Actual length of the comment +///\exception H5::LocationException +///\par Description +/// This function retrieves \a buf_size characters of the comment +/// including the null terminator. Thus, if the actual length +/// of the comment is more than buf_size-1, the retrieved comment +/// will be truncated to accommodate the null terminator. +// Programmer Binh-Minh Ribler - Mar 2014 +//-------------------------------------------------------------------------- +ssize_t H5Location::getComment(const char* name, const size_t buf_size, char* comment) const +{ + // H5Oget_comment_by_name will get buf_size chars of the comment including + // the null terminator + ssize_t comment_len; + comment_len = H5Oget_comment_by_name(getId(), name, comment, buf_size, H5P_DEFAULT); + + // If H5Oget_comment_by_name returns a negative value, raise an exception + if (comment_len < 0) + { + throw LocationException("H5Location::getComment", "H5Oget_comment_by_name failed"); + } + + // Return the comment length, which might be different from buf_size + return(comment_len); +} + +//-------------------------------------------------------------------------- +// Function: H5Location::getComment +///\brief Returns the comment as \a string for this location, +/// returning its length. +///\param name - IN: Name of the object +///\param buf_size - IN: Length of the comment to retrieve, default to 0 ///\return Comment string -///\exception H5::Exception +///\exception H5::LocationException // Programmer Binh-Minh Ribler - 2000 (moved from CommonFG, Sep 2013) -// 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 H5Location::getComment(const char* name, size_t bufsize) const +H5std_string H5Location::getComment(const char* name, const size_t buf_size) const { - // bufsize is default to 256 - // temporary variable - hid_t loc_id = getId(); // temporary variable + // Initialize string to "", so that if there is no comment, the returned + // string will be empty + H5std_string comment(""); - // 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); + // Preliminary call to get the comment's length + ssize_t comment_len = H5Oget_comment_by_name(getId(), name, NULL, (size_t)0, H5P_DEFAULT); - // 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 the call to H5Oget_comment_by_name returned an error, skip this block - // and throw an exception below. - if (ret_value >= 0 && (size_t)ret_value > bufsize && bufsize == 256) - { - size_t new_size = ret_value; - delete []comment_C; - 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 a negative value, raise an exception + if (comment_len < 0) + { + throw LocationException("H5Location::getComment", "H5Oget_comment_by_name failed"); + } - // if H5Oget_comment_by_name returns SUCCEED, return the string comment, - // otherwise, throw an exception - if (ret_value < 0) { - delete []comment_C; - throw Exception(inMemFunc("getComment"), "H5Oget_comment_by_name failed"); - } + // If comment exists, calls C routine again to get it + else if (comment_len > 0) + { + size_t tmp_len = buf_size; - H5std_string comment = H5std_string(comment_C); - delete []comment_C; - return (comment); + // If buffer size is not provided, use comment length + if (tmp_len == 0) + tmp_len = comment_len; + + // Temporary buffer for char* comment + char* comment_C = new char[tmp_len+1]; + HDmemset(comment_C, 0, tmp_len+1); // clear buffer + + // Used overloaded function + ssize_t comment_len = getComment(name, tmp_len, comment_C); + + // Convert the C comment to return + comment = comment_C; + + // Clean up resource + delete []comment_C; + } + // Otherwise, keep comment intact + + // Return the string comment + return(comment); } //-------------------------------------------------------------------------- @@ -529,9 +565,9 @@ H5std_string H5Location::getComment(const char* name, size_t bufsize) const /// \c H5std_string for \a name. // Programmer Binh-Minh Ribler - 2000 (moved from CommonFG, Sep 2013) //-------------------------------------------------------------------------- -H5std_string H5Location::getComment(const H5std_string& name, size_t bufsize) const +H5std_string H5Location::getComment(const H5std_string& name, const size_t buf_size) const { - return(getComment(name.c_str(), bufsize)); + return(getComment(name.c_str(), buf_size)); } #ifndef DOXYGEN_SHOULD_SKIP_THIS @@ -542,7 +578,7 @@ H5std_string H5Location::getComment(const H5std_string& name, size_t bufsize) co // name - IN: Name of the object to be referenced // dataspace - IN: Dataspace with selection // ref_type - IN: Type of reference; default to \c H5R_DATASET_REGION -// Exception H5::IdComponentException +// Exception H5::ReferenceException // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- void H5Location::p_reference(void* ref, const char* name, hid_t space_id, H5R_type_t ref_type) const |