Skip to content

Commit

Permalink
Add a checker for the existence of a H5::Group in the file.
Browse files Browse the repository at this point in the history
Co-authored-by: willGraham01 <[email protected]>
  • Loading branch information
samcunliffe and willGraham01 committed May 12, 2023
1 parent 05ef9fc commit 986bd02
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
15 changes: 14 additions & 1 deletion tdms/include/hdf5_io/hdf5_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <H5Cpp.h>

#include "cell_coordinate.h"
#include "hdf5_io/hdf5_dimension.h"

/**
Expand Down Expand Up @@ -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;
};
};
10 changes: 0 additions & 10 deletions tdms/src/hdf5_io/hdf5_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@

using namespace std;

ijk to_ijk(const std::vector<hsize_t> 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<string> HDF5Base::get_datanames() const {
vector<string> names;

Expand Down
7 changes: 7 additions & 0 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"));
}

0 comments on commit 986bd02

Please sign in to comment.