Check for existence of group in HDF5 file @param[in] handle HDF5 file handle @param[in] group_name Name of the group to check @return True if @p group_name is in the file
| 16 | /// @param[in] group_name Name of the group to check |
| 17 | /// @return True if @p group_name is in the file |
| 18 | bool has_group(hid_t handle, const std::string& group_name) |
| 19 | { |
| 20 | const hid_t lapl_id = H5Pcreate(H5P_LINK_ACCESS); |
| 21 | if (lapl_id < 0) |
| 22 | throw std::runtime_error("Failed to create HDF5 property list"); |
| 23 | |
| 24 | htri_t link_status = H5Lexists(handle, group_name.c_str(), lapl_id); |
| 25 | if (link_status < 0) |
| 26 | throw std::runtime_error("Failed to check existence of HDF5 link in group"); |
| 27 | |
| 28 | if (link_status == 0) |
| 29 | { |
| 30 | if (H5Pclose(lapl_id) < 0) |
| 31 | throw std::runtime_error("Call to H5Pclose unsuccessful"); |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | H5O_info_t object_info; |
| 36 | #if H5_VERSION_GE(1, 12, 0) |
| 37 | herr_t err = H5Oget_info_by_name3(handle, group_name.c_str(), &object_info, |
| 38 | H5O_INFO_ALL, lapl_id); |
| 39 | #else |
| 40 | herr_t err |
| 41 | = H5Oget_info_by_name1(handle, group_name.c_str(), &object_info, lapl_id); |
| 42 | #endif |
| 43 | if (err < 0) |
| 44 | throw std::runtime_error("Call to H5Oget_info_by_name unsuccessful"); |
| 45 | |
| 46 | if (H5Pclose(lapl_id) < 0) |
| 47 | throw std::runtime_error("Call to H5Pclose unsuccessful"); |
| 48 | |
| 49 | return object_info.type == H5O_TYPE_GROUP; |
| 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 |