summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--c++/src/H5File.cpp26
-rw-r--r--c++/src/H5File.h4
-rw-r--r--c++/test/tfile.cpp26
3 files changed, 56 insertions, 0 deletions
diff --git a/c++/src/H5File.cpp b/c++/src/H5File.cpp
index e0a0da5..7ca126f 100644
--- a/c++/src/H5File.cpp
+++ b/c++/src/H5File.cpp
@@ -158,6 +158,25 @@ void H5File::p_get_file(const char* name, unsigned int flags, const FileCreatPro
#endif // DOXYGEN_SHOULD_SKIP_THIS
//--------------------------------------------------------------------------
+// Function: H5File overloaded constructor
+///\brief Creates an H5File object using an existing file id.
+///\param existing_id - IN: Id of an existing file
+// Programmer Binh-Minh Ribler - 2015
+// Description
+// Mar 29, 2015
+// Added in responding to a request from user Jason Newton.
+// However, it is not recommended to use the private member "id"
+// in applications. Unlike other situations, where similar
+// constructor is needed by the library in order to return
+// an object, H5File doesn't need it. -BMR (HDFFV-8766 partially)
+//--------------------------------------------------------------------------
+H5File::H5File(hid_t existing_id) : H5Location(), CommonFG()
+{
+ id = existing_id;
+ incRefCount(); // increment number of references to this id
+}
+
+//--------------------------------------------------------------------------
// Function: H5File copy constructor
///\brief Copy constructor: makes a copy of the original
/// H5File object.
@@ -225,6 +244,13 @@ bool H5File::isHdf5(const H5std_string& name )
//--------------------------------------------------------------------------
void H5File::openFile(const char* name, unsigned int flags, const FileAccPropList& access_plist)
{
+ try {
+ close();
+ }
+ catch (Exception close_error) {
+ throw FileIException("H5File::openFile", close_error.getDetailMsg());
+ }
+
hid_t access_plist_id = access_plist.getId();
id = H5Fopen (name, flags, access_plist_id);
if (id < 0) // throw an exception when open fails
diff --git a/c++/src/H5File.h b/c++/src/H5File.h
index 0ef85b5..29621aa 100644
--- a/c++/src/H5File.h
+++ b/c++/src/H5File.h
@@ -84,6 +84,10 @@ class H5_DLLCPP H5File : public H5Location, public CommonFG {
// Gets the file id
virtual hid_t getLocId() const;
+ // Creates an H5File using an existing file id. Not recommended
+ // in applications.
+ H5File(hid_t existing_id);
+
#endif // DOXYGEN_SHOULD_SKIP_THIS
///\brief Returns this class name.
diff --git a/c++/test/tfile.cpp b/c++/test/tfile.cpp
index 6d5dc23..f3bbb16 100644
--- a/c++/test/tfile.cpp
+++ b/c++/test/tfile.cpp
@@ -330,6 +330,32 @@ static void test_file_open()
verify_val(iparm1, F2_SYM_INTERN_K, "FileCreatPropList::getSymk", __LINE__, __FILE__);
verify_val(iparm2, F2_SYM_LEAF_K, "FileCreatPropList::getSymk", __LINE__, __FILE__);
+ // Test H5File constructor with existing file id
+ H5File file2(file1.getId());
+ file1.close();
+
+ // Try truncating the file, and it should fail because the file is
+ // still opened with file2.
+ try {
+ H5File file3 (FILE2, H5F_ACC_TRUNC); // should throw E
+
+ // Should FAIL but didn't, so throw an invalid action exception
+ throw InvalidActionException("H5File constructor", "Attempt truncating an opened file.");
+ }
+ catch( FileIException E ) // catching H5F_ACC_TRUNC on opened file
+ {} // do nothing, FAIL expected
+
+ // Now, really close the file.
+ file2.close();
+
+ // Truncating should succeed now.
+ H5File file3(FILE2, H5F_ACC_TRUNC);
+
+ // Opening another file to file3 object, FILE2 should be closed, so
+ // the next attempt to truncate FILE2 should succeed.
+ file3.openFile(FILE1, H5F_ACC_RDONLY);
+ H5File file4(FILE2, H5F_ACC_TRUNC);
+
PASSED();
} // end of try block