#include #include "H5Include.h" #include "H5RefCounter.h" #include "H5Exception.h" #include "H5IdComponent.h" #include "H5Idtemplates.h" #include "H5PropList.h" #include "H5Object.h" #include "H5FaccProp.h" #include "H5FcreatProp.h" #include "H5DxferProp.h" #include "H5DcreatProp.h" #include "H5CommonFG.h" #include "H5Group.h" #include "H5AbstractDs.h" #include "H5DataSpace.h" #include "H5DataSet.h" #include "H5File.h" #include "H5Alltypes.h" #ifndef H5_NO_NAMESPACE namespace H5 { #endif // Creates or opens an HDF5 file depending on the parameter flags. H5File::H5File( const string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent() { getFile( name.c_str(), flags, create_plist, access_plist ); } H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : IdComponent() { getFile( name, flags, create_plist, access_plist ); } // This function is private and contains common code between the // constructors taking a string or a char* void H5File::getFile( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) { // These bits only set for creation, so if any of them are set, // create the file. if( flags & (H5F_ACC_EXCL|H5F_ACC_TRUNC|H5F_ACC_DEBUG )) { hid_t create_plist_id = create_plist.getId(); hid_t access_plist_id = access_plist.getId(); id = H5Fcreate( name, flags, create_plist_id, access_plist_id ); } // Open the file if none of the bits above are set. else { // use create_plist for access plist because of the default argument hid_t access_plist_id = create_plist.getId(); id = H5Fopen( name, flags, access_plist_id ); } if( id <= 0 ) // throw an exception when open/create fail { throw FileIException(); } } // Copy constructor: makes a copy of the original H5File object. H5File::H5File( const H5File& original ) : IdComponent( original ) {} // Determines whether a file specified by its name in HDF5 format bool H5File::isHdf5(const string& name ) { return( isHdf5( name.c_str()) ); } bool H5File::isHdf5(const char* name ) { // Calls C routine H5Fis_hdf5 to determine whether the file is in // HDF5 format. It returns positive value, 0, or negative value htri_t ret_value = H5Fis_hdf5( name ); if( ret_value > 0 ) return true; else if( ret_value == 0 ) return false; else // Raise exception when H5Fis_hdf5 returns a negative value { throw FileIException(); } } // Get id of the location, which id the file id here; used by CommonFG // member functions hid_t H5File::getLocId() const { return( getId() ); } // Reopens this file void H5File::reopen() { // reset the identifier of this H5File - send 'this' in so that // H5Fclose can be called appropriately resetIdComponent( this ); // call C routine to reopen the file - Note: not sure about this // does id need to be closed later? which id to be the parameter? id = H5Freopen( id ); if( id <= 0 ) // Raise exception when H5Freopen returns a neg value { throw FileIException(); } } // Returns the creation property list of this file FileCreatPropList H5File::getCreatePlist() const { hid_t create_plist_id = H5Fget_create_plist( id ); // if H5Fget_create_plist returns a valid id, create and return // the FileCreatPropList object for this property list if( create_plist_id > 0 ) { FileCreatPropList create_plist( create_plist_id ); return( create_plist ); } else { throw FileIException(); } } // Returns the access property list of this file FileAccPropList H5File::getAccessPlist() const { hid_t access_plist_id = H5Fget_access_plist( id ); // if H5Fget_access_plist returns a valid id, create and return // the FileAccPropList object for this property list if( access_plist_id > 0 ) { FileAccPropList access_plist( access_plist_id ); return access_plist; } else // Raise an exception { throw FileIException(); } } // Calls the C API H5Fclose to close this file. Used by IdComponent::reset void H5File::p_close() const { herr_t ret_value = H5Fclose( id ); if( ret_value < 0 ) { throw FileIException(); } } // Throw file exception void H5File::throwException() const { throw FileIException(); } // The destructor of this instance calls IdComponent::reset to // reset its identifier - no longer true // Older compilers (baldric) don't support template member functions // and IdComponent::reset is one; so at this time, the resetId is not // a member function so it can be template to work around that problem. H5File::~H5File() { // The HDF5 file id will be closed properly resetIdComponent( this ); } #ifndef H5_NO_NAMESPACE } // end namespace #endif