summaryrefslogtreecommitdiffstats
path: root/c++
diff options
context:
space:
mode:
Diffstat (limited to 'c++')
-rw-r--r--c++/src/H5Location.cpp33
-rw-r--r--c++/src/H5Location.h10
-rw-r--r--c++/test/tattr.cpp76
3 files changed, 112 insertions, 7 deletions
diff --git a/c++/src/H5Location.cpp b/c++/src/H5Location.cpp
index b93cd86..98878d7 100644
--- a/c++/src/H5Location.cpp
+++ b/c++/src/H5Location.cpp
@@ -250,6 +250,39 @@ int H5Location::getNumAttrs() const
}
//--------------------------------------------------------------------------
+// Function: H5Location::attrExists
+///\brief Checks whether the named attribute exists at this location.
+///\param name - IN: Name of the attribute to be queried
+///\exception H5::AttributeIException
+// Programmer Binh-Minh Ribler - 2013
+//--------------------------------------------------------------------------
+bool H5Location::attrExists(const char* name) const
+{
+ // Call C routine H5Aexists to determine whether an attribute exists
+ // at this location, which could be specified by a file, group, dataset,
+ // or named datatype.
+ herr_t ret_value = H5Aexists(getId(), name);
+ if( ret_value > 0 )
+ return true;
+ else if(ret_value == 0)
+ return false;
+ else // Raise exception when H5Aexists returns a negative value
+ throw AttributeIException(inMemFunc("attrExists"), "H5Aexists failed");
+}
+
+//--------------------------------------------------------------------------
+// Function: H5Location::attrExists
+///\brief This is an overloaded member function, provided for convenience.
+/// It differs from the above function in that it takes
+/// a reference to an \c H5std_string for \a name.
+// Programmer Binh-Minh Ribler - 2000
+//--------------------------------------------------------------------------
+bool H5Location::attrExists(const H5std_string& name) const
+{
+ return(attrExists(name.c_str()));
+}
+
+//--------------------------------------------------------------------------
// Function: H5Location::removeAttr
///\brief Removes the named attribute from this object.
///\param name - IN: Name of the attribute to be removed
diff --git a/c++/src/H5Location.h b/c++/src/H5Location.h
index d1dd892..8eae454 100644
--- a/c++/src/H5Location.h
+++ b/c++/src/H5Location.h
@@ -79,11 +79,15 @@ class H5_DLLCPP H5Location : public IdComponent {
// misleading, so getRefObjType is used in the new function instead.
// Iterate user's function over the attributes at this location.
- int iterateAttrs( attr_operator_t user_op, unsigned* idx = NULL, void* op_data = NULL );
+ int iterateAttrs(attr_operator_t user_op, unsigned* idx = NULL, void* op_data = NULL);
+
+ // Checks whether the named attribute exists at this location.
+ bool attrExists(const char* name) const;
+ bool attrExists(const H5std_string& name) const;
// Removes the named attribute from this location.
- void removeAttr( const char* name ) const;
- void removeAttr( const H5std_string& name ) const;
+ void removeAttr(const char* name) const;
+ void removeAttr(const H5std_string& name) const;
// Renames the named attribute to a new name.
void renameAttr(const char* oldname, const char* newname) const;
diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp
index 1a15aea..7e77e85 100644
--- a/c++/test/tattr.cpp
+++ b/c++/test/tattr.cpp
@@ -252,14 +252,19 @@ static void test_attr_rename()
int read_data1[ATTR1_DIM1]={0}; // Buffer for reading the attribute
int i;
- // Output message about test being performed
- SUBTEST("Rename Attribute Function");
+ // Output message about test being performed
+ SUBTEST("Checking for Existence and Renaming Attribute");
try {
// Open file
H5File fid1(FILE_BASIC, H5F_ACC_RDWR);
- // Check rename of attribute belonging to a file
+ // Check and rename attribute belonging to a file
+
+ // Check for existence of attribute
+ bool attr_exists = fid1.attrExists(FATTR1_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "Attribute should exist but does not");
// Change attribute name
fid1.renameAttr(FATTR1_NAME, FATTR_TMP_NAME);
@@ -280,7 +285,12 @@ static void test_attr_rename()
// Open the dataset
DataSet dataset = fid1.openDataSet(DSET1_NAME);
- // Check rename of attribute belonging to a dataset
+ // Check and rename attribute belonging to a dataset
+
+ // Check for existence of attribute
+ attr_exists = dataset.attrExists(ATTR1_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "Attribute should exist but does not");
// Change attribute name
dataset.renameAttr(ATTR1_NAME, ATTR_TMP_NAME);
@@ -303,6 +313,11 @@ static void test_attr_rename()
// Close attribute
attr1.close();
+ // Check for existence of second attribute
+ attr_exists = dataset.attrExists(ATTR2_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "Attribute should exist but does not");
+
// Open the second attribute
Attribute attr2(dataset.openAttribute(ATTR2_NAME));
@@ -324,6 +339,11 @@ static void test_attr_rename()
// Change first attribute back to the original name
dataset.renameAttr(ATTR_TMP_NAME, ATTR1_NAME);
+ // Check for existence of attribute after renaming
+ attr_exists = dataset.attrExists(ATTR1_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "Attribute should exist but does not");
+
PASSED();
} // end try block
@@ -1354,6 +1374,53 @@ static void test_string_attr()
/****************************************************************
**
+** test_attr_exists(): Test checking for attribute existence.
+** (additional attrExists tests are in test_attr_rename())
+**
+****************************************************************/
+static void test_attr_exists()
+{
+ // Output message about test being performed
+ SUBTEST("Check Attribute Existence");
+
+ try {
+ // Open file.
+ H5File fid1(FILE_BASIC, H5F_ACC_RDWR);
+
+ // Open the root group.
+ Group root = fid1.openGroup("/");
+
+ // Check for existence of attribute
+ bool attr_exists = fid1.attrExists(ATTR1_FL_STR_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "fid1, ATTR1_FL_STR_NAMEAttribute should exist but does not");
+
+ // Check for existence of attribute
+ attr_exists = fid1.attrExists(FATTR1_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "fid1,FATTR2_NAMEAttribute should exist but does not");
+
+ // Open a group.
+ Group group = fid1.openGroup(GROUP1_NAME);
+
+ // Check for existence of attribute
+ attr_exists = group.attrExists(ATTR2_NAME);
+ if (attr_exists == false)
+ throw InvalidActionException("H5File::attrExists", "group, ATTR2_NAMEAttribute should exist but does not");
+
+ PASSED();
+ } // end try block
+
+ catch (InvalidActionException E) {
+ issue_fail_msg("test_attr_exists()", __LINE__, __FILE__, E.getCDetailMsg());
+ }
+ catch (Exception E) {
+ issue_fail_msg("test_attr_exists()", __LINE__, __FILE__, E.getCDetailMsg());
+ }
+} // test_attr_exists()
+
+/****************************************************************
+**
** test_attr(): Main attribute testing routine.
**
****************************************************************/
@@ -1382,6 +1449,7 @@ void test_attr()
test_attr_dtype_shared(); // Test using shared datatypes in attributes
test_string_attr(); // Test read/write string attribute
+ test_attr_exists(); // Test H5Location::attrExists
} // test_attr()