RefTester(hid_t id) : obj(id) { if (H5Iis_valid(id)) { ref = H5Iget_ref(id); } else { ref = 0; } }
void close(hid_t id, H5I_type_t type) { if((id == -1) || (is_constant(type, id))) return; if(!H5Iis_valid(id)) H5CPP_THROW("attempted to close bad hdf5 id: " << id); _close(id, type); }
void close(hid_t id) { if((id == -1) || (is_constant(id))) return; if(!H5Iis_valid(id)) H5CPP_THROW("attempted to close bad hdf5 id: " << id); H5I_type_t t = H5CPP_ERR_ON_NEG(H5Iget_type(id)); _close(id, t); }
//-------------------------------------------------------------------------- // Function: isValid (static) ///\brief Checks if the given ID is valid. ///\return true if the given identifier is valid, and false, otherwise. ///\par Description /// A valid ID is one that is in use and has an application /// reference count of at least 1. // Programmer Binh-Minh Ribler - Mar 1, 2017 //-------------------------------------------------------------------------- bool IdComponent::isValid(hid_t an_id) { // Call C function htri_t ret_value = H5Iis_valid(an_id); if (ret_value > 0) return true; else if (ret_value == 0) return false; else // Raise exception when H5Iis_valid returns a negative value throw IdComponentException("isValid", "H5Iis_valid failed"); }
/* * Class: hdf_hdf5lib_H5 * Method: H5Iis_valid * Signature: (J)Z */ JNIEXPORT jboolean JNICALL Java_hdf_hdf5lib_H5_H5Iis_1valid (JNIEnv *env, jclass clss, jlong obj_id) { htri_t bval = JNI_FALSE; bval = H5Iis_valid((hid_t)obj_id); if (bval > 0) bval = JNI_TRUE; else if (bval < 0) h5libraryError(env); return (jboolean)bval; } /* end Java_hdf_hdf5lib_H5_H5Iis_1valid */
void TestH5::setUp() { unsigned int &openMode = open_mode(); if (openMode == H5F_ACC_TRUNC) { h5file = H5Fcreate("test_h5.h5", openMode, H5P_DEFAULT, H5P_DEFAULT); } else { h5file = H5Fopen("test_h5.h5", openMode, H5P_DEFAULT); } CPPUNIT_ASSERT(H5Iis_valid(h5file)); hid_t g; if (openMode == H5F_ACC_TRUNC) { g = H5Gcreate2(h5file, "h5", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } else { g = H5Gopen2(h5file, "h5", H5P_DEFAULT); } CPPUNIT_ASSERT(H5Iis_valid(g)); h5group = nix::hdf5::Group(g); openMode = H5F_ACC_RDWR; }
// Check for path validity char AH5_path_valid(hid_t loc_id, const char *path) { char *temp; int i, slashes = 0; temp = strdup(path); for (i = (int) strlen(path); i > 0; i--) { if (temp[i] == '/') { temp[i] = '\0'; slashes++; /* count number of slashes excluding the first one */ } } if (strcmp(path, ".") == 0) { if (!H5Iis_valid(loc_id)) { free(temp); return AH5_FALSE; } } else { if(H5Lexists(loc_id, temp, H5P_DEFAULT) != AH5_TRUE) { free(temp); return AH5_FALSE; } } i = 1; while (slashes > 0) { while (temp[i] != '\0') i++; temp[i] = '/'; slashes--; if(H5Lexists(loc_id, temp, H5P_DEFAULT) != AH5_TRUE) { free(temp); return AH5_FALSE; } } free(temp); return AH5_TRUE; }
/*---------------------------------------------------------------------------- * Name: h5iis_valid_c * Purpose: Calls H5Iis_valid * Inputs: obj_id - object identifier * Outputs: 0 = false, 1 = true * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Tuesday, August 24, 2004 * Modifications: *---------------------------------------------------------------------------*/ int_f h5iis_valid_c(hid_t_f *obj_id, int_f *c_valid) { int ret_value; htri_t c_ret_value; /* * Call H5Iis_valid */ if ((c_ret_value = H5Iis_valid(*obj_id)) < 0) HGOTO_DONE(FAIL); /* Set output & return values */ *c_valid = (int_f)c_ret_value; ret_value=0; done: return ret_value; }
FileHDF5::FileHDF5(const string &name, FileMode mode, Compression compression, OpenFlags flags) { if (!fileExists(name)) { mode = FileMode::Overwrite; } this->mode = mode; this->compr = compression; //we want hdf5 to keep track of the order in which links were created so that //the order for indexed based accessors is stable cf. issue #387 H5Object fcpl = H5Pcreate(H5P_FILE_CREATE); fcpl.check("Could not create file creation plist"); HErr res = H5Pset_link_creation_order(fcpl.h5id(), H5P_CRT_ORDER_TRACKED|H5P_CRT_ORDER_INDEXED); res.check("Unable to create file (H5Pset_link_creation_order failed.)"); unsigned int h5mode = map_file_mode(mode); bool is_create = !fileExists(name) || h5mode == H5F_ACC_TRUNC; if (is_create) { hid = H5Fcreate(name.c_str(), h5mode, fcpl.h5id(), H5P_DEFAULT); } else { hid = H5Fopen(name.c_str(), h5mode, H5P_DEFAULT); } if (!H5Iis_valid(hid)) { throw H5Exception("Could not open/create file"); } openRoot(); if (is_create) { createHeader(); } else if (!checkHeader(mode) && (flags & OpenFlags::Force) != OpenFlags::Force) { throw nix::InvalidFile("FileHDF5::open_existing!"); } metadata = root.openGroup("metadata"); data = root.openGroup("data"); setCreatedAt(); setUpdatedAt(); }
int main(int argc, char *argv[]) { hid_t fid = -1; hid_t access_plist = -1; hid_t create_plist = -1; hid_t cparm = -1; hid_t datatype = -1; hid_t dataspace = -1; hid_t dataset = -1; hid_t memspace = -1; hid_t groupDetector = -1; int rank = 1; hsize_t chunk[2] = {10,10}; hsize_t dims[2] = {1,1}; hsize_t elementSize[2] = {1,1}; hsize_t maxdims[2] = {H5S_UNLIMITED,H5S_UNLIMITED}; int ivalue[2]; int fillValue = 0; /* Open the source file and dataset */ /* All SWMR files need to use the latest file format */ access_plist = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG); #if H5_VERSION_GE(1,9,178) H5Pset_object_flush_cb(access_plist, cFlushCallback, NULL); #endif H5Pset_libver_bounds(access_plist, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST); create_plist = H5Pcreate(H5P_FILE_CREATE); fid = H5Fcreate("test_string_swmr.h5", H5F_ACC_TRUNC, create_plist, access_plist); /* Data */ rank = 2; dims[0] = 10; dims[1] = 10; dataspace = H5Screate_simple(rank, dims, dims); cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(cparm, rank, chunk); datatype = H5Tcopy(H5T_NATIVE_INT8); H5Pset_fill_value(cparm, datatype, &fillValue); groupDetector = H5Gcreate(fid, "detector", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); access_plist = H5Pcreate(H5P_DATASET_ACCESS); H5Pset_chunk_cache(access_plist, 503, 100, 1.0); dataset = H5Dcreate2(groupDetector, "data1", datatype, dataspace, H5P_DEFAULT, cparm, access_plist); ivalue[0] = 1; ivalue[1] = 1; writeInt32Attribute(dataset, "NDArrayDimBinning", 2, ivalue); ivalue[0] = 0; ivalue[1] = 0; writeInt32Attribute(dataset, "NDArrayDimOffset", 2, ivalue); writeInt32Attribute(dataset, "NDArrayDimReverse", 2, ivalue); ivalue[0] = 2; writeInt32Attribute(dataset, "NDArrayNumDims", 1, ivalue); H5Gclose(groupDetector); dims[0] = 1; dims[1] = 1; chunk[0] = 1; rank = 1; /* Unique ID */ datatype = H5T_NATIVE_INT32; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "NDArrayUniqueId", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "NDArrayUniqueId"); writeStringAttribute(dataset, "NDAttrDescription", "The unique ID of the NDArray"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceDriver"); writeStringAttribute(dataset, "NDAttrSource", "Driver"); /* EPICS Timestemp */ datatype = H5T_NATIVE_DOUBLE; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "NDArrayTimeStamp", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "NDArrayTimeStamp"); writeStringAttribute(dataset, "NDAttrDescription", "The timestamp of the NDArray as float64"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceDriver"); writeStringAttribute(dataset, "NDAttrSource", "Driver"); /* EPICS TS sec */ datatype = H5T_NATIVE_UINT32; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "NDArrayEpicsTSSec", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "NDArrayEpicsTSSec"); writeStringAttribute(dataset, "NDAttrDescription", "The NDArray EPICS timestamp seconds past epoch"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceDriver"); writeStringAttribute(dataset, "NDAttrSource", "Driver"); /* EPICS TS nsec */ datatype = H5T_NATIVE_UINT32; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "NDArrayEpicsTSnSec", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "NDArrayEpicsTSnSec"); writeStringAttribute(dataset, "NDAttrDescription", "The NDArray EPICS timestamp nanoseconds"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceDriver"); writeStringAttribute(dataset, "NDAttrSource", "Driver"); /* Color mode */ datatype = H5T_NATIVE_INT32; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "ColorMode", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "ColorMode"); writeStringAttribute(dataset, "NDAttrDescription", "Color mode"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceDriver"); writeStringAttribute(dataset, "NDAttrSource", "Driver"); /* Camera manufacturer */ datatype = H5Tcopy(H5T_C_S1); H5Tset_size(datatype, 256); cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_fill_value(cparm, datatype, &fillValue); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "CameraManufacturer", datatype, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); memspace = H5Screate_simple(rank, elementSize, NULL); writeStringAttribute(dataset, "NDAttrName", "CameraManufacturer"); writeStringAttribute(dataset, "NDAttrDescription", "Camera manufacturer"); writeStringAttribute(dataset, "NDAttrSourceType", "NDAttrSourceParam"); writeStringAttribute(dataset, "NDAttrSource", "MANUFACTURER"); /* Performance data */ rank = 2; dims[0] = 1; dims[1] = 5; chunk[1] = 5; cparm = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(cparm, rank, chunk); dataspace = H5Screate_simple(rank, dims, maxdims); dataset = H5Dcreate2(fid, "timestamp", H5T_NATIVE_DOUBLE, dataspace, H5P_DEFAULT, cparm, H5P_DEFAULT); if (!H5Iis_valid(dataset)) { printf("Error writing performance dataset"); } H5Sclose(dataspace); #if H5_VERSION_GE(1,9,178) H5Fstart_swmr_write(fid); #endif H5Fclose(fid); return 0; } /* end main */
/* Test the H5Iis_valid function */ static int test_is_valid(void) { hid_t dtype; /* datatype id */ int64_t nmembs1; /* number of type memnbers */ int64_t nmembs2; htri_t tri_ret; /* htri_t return value */ herr_t ret; /* return value */ /* Create a datatype id */ dtype = H5Tcopy(H5T_NATIVE_INT); CHECK(dtype, FAIL, "H5Tcopy"); if (dtype < 0) goto out; /* Check that the ID is valid */ tri_ret = H5Iis_valid(dtype); VERIFY(tri_ret, TRUE, "H5Iis_valid"); if (tri_ret != TRUE) goto out; /* Artificially manipulate the reference counts so app_count is 0, and dtype * appears to be an internal id. This takes advantage of the fact that * H5Ipkg is included. */ ret = H5I_inc_ref(dtype, FALSE); CHECK(ret, FAIL, "H5I_inc_ref"); if (ret < 0) goto out; ret = H5I_dec_app_ref(dtype); CHECK(ret, FAIL, "H5I_dec_ref"); if (ret < 0) goto out; /* Check that dtype is invalid */ tri_ret = H5Iis_valid(dtype); VERIFY(tri_ret, FALSE, "H5Iis_valid"); if (tri_ret != FALSE) goto out; /* Close dtype and verify that it has been closed */ nmembs1 = H5I_nmembers(H5I_DATATYPE); CHECK(nmembs1, FAIL, "H5I_nmembers"); if (nmembs1 < 0) goto out; ret = H5I_dec_ref(dtype); CHECK(ret, FAIL, "H5I_dec_ref"); if (ret < 0) goto out; nmembs2 = H5I_nmembers(H5I_DATATYPE); VERIFY(nmembs2, nmembs1 - 1, "H5I_nmembers"); if (nmembs2 != nmembs1 - 1) goto out; /* Check that dtype is invalid */ tri_ret = H5Iis_valid(dtype); VERIFY(tri_ret, FALSE, "H5Iis_valid"); if (tri_ret != FALSE) goto out; /* Check that an id of -1 is invalid */ tri_ret = H5Iis_valid((hid_t)-1); VERIFY(tri_ret, FALSE, "H4Iis_valid"); if (tri_ret != FALSE) goto out; return 0; out: /* Don't attempt to close dtype as we don't know the exact state of the * reference counts. Every state in this function will be automatically * closed at library exit anyways, as internal count is never > 1. */ return -1; }
file::file(h5_object obj) : h5_object(std::move(obj)) { if (!H5Iis_valid(this->id)) TRIQS_RUNTIME_ERROR << "Invalid input in h5::file constructor from id"; if (H5Iget_type(this->id) != H5I_FILE) TRIQS_RUNTIME_ERROR << "h5::file constructor must take the id of a file "; }
~group() { if (H5Iis_valid(_self) > 0) { H5Gclose(_self); } }