From 986bd021d433d81a085a0c8250e1fe57e986bc20 Mon Sep 17 00:00:00 2001 From: Sam Cunliffe Date: Fri, 12 May 2023 15:28:27 +0100 Subject: [PATCH] Add a checker for the existence of a H5::Group in the file. Co-authored-by: willGraham01 --- tdms/include/hdf5_io/hdf5_base.h | 15 ++++++++++++++- tdms/src/hdf5_io/hdf5_base.cpp | 10 ---------- tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp | 7 +++++++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/tdms/include/hdf5_io/hdf5_base.h b/tdms/include/hdf5_io/hdf5_base.h index 188bb8476..0d7e2e127 100644 --- a/tdms/include/hdf5_io/hdf5_base.h +++ b/tdms/include/hdf5_io/hdf5_base.h @@ -12,7 +12,6 @@ #include -#include "cell_coordinate.h" #include "hdf5_io/hdf5_dimension.h" /** @@ -90,4 +89,18 @@ class HDF5Base { * @return false Otherwise. */ bool is_ok() const; + + /** + * @brief Checks if the group `group_name` exists in this file. + * + * @details Just wraps some ugly H5Lexists call from the C-API. + * TODO: does this work if given 'group/subgroup' ? + * + * @param group_name Name of the group under root to search for + * @return true The group is present in the file, under root + * @return false Otherwise + */ + bool group_exists(const std::string &group_name) const { + return H5Lexists(file_->getId(), group_name.c_str(), H5P_DEFAULT) > 0; + }; }; diff --git a/tdms/src/hdf5_io/hdf5_base.cpp b/tdms/src/hdf5_io/hdf5_base.cpp index d07a957e1..19b916b2c 100644 --- a/tdms/src/hdf5_io/hdf5_base.cpp +++ b/tdms/src/hdf5_io/hdf5_base.cpp @@ -13,16 +13,6 @@ using namespace std; -ijk to_ijk(const std::vector dimensions) { - unsigned int rank = dimensions.size(); - ijk out; - if (rank > 0) out.i = (int) dimensions[0]; - if (rank > 1) out.j = (int) dimensions[1]; - if (rank > 2) out.k = (int) dimensions[2]; - if (rank > 3) spdlog::warn("Rank > 3"); - return out; -} - vector HDF5Base::get_datanames() const { vector names; diff --git a/tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp b/tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp index 031b4c472..066a47e0e 100644 --- a/tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp +++ b/tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp @@ -19,6 +19,7 @@ using namespace std; using tdms_tests::create_tmp_dir; +using tdms_unit_test_data::struct_testdata; TEST_CASE("Test file I/O construction/destruction.") { // test-case wide setup - temporary directory @@ -148,3 +149,9 @@ TEST_CASE("Test read/write wrt standard datatypes") { SPDLOG_DEBUG("Removing temporary directory."); filesystem::remove_all(tmp); } + +TEST_CASE("Test group can be found") { + HDF5Reader reader(struct_testdata); + REQUIRE(!reader.group_exists("group_doesnt_exist")); + REQUIRE(reader.group_exists("example_struct")); +}