Skip to content

Commit

Permalink
Further updates for ritsuko's new functions.
Browse files Browse the repository at this point in the history
- The new missing placeholder functions now just perform checks and don't
  open the attribute, to avoid mixing up the two concerns.
- We also switch to the new open_and_load function for loading a scalar
  string attribute, so we get rid of our internal version.
  • Loading branch information
LTLA committed Nov 22, 2023
1 parent 4ec0a2f commit b1d39e6
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions include/uzuki2/parse_hdf5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ namespace hdf5 {
/**
* @cond
*/
template<class Object_>
std::string check_scalar_string_attribute(const Object_& handle, const char* name) {
auto attr = ritsuko::hdf5::open_attribute(handle, name);
if (!ritsuko::hdf5::is_scalar(attr) || attr.getTypeClass() != H5T_STRING) {
throw std::runtime_error("'" + std::string(name) + "' should be a scalar string attribute");
}
return ritsuko::hdf5::load_scalar_string_attribute(attr);
}

inline H5::DataSet check_scalar_dataset(const H5::Group& handle, const char* name) {
if (handle.childObjType(name) != H5O_TYPE_DATASET) {
throw std::runtime_error("expected '" + std::string(name) + "' to be a dataset");
Expand All @@ -75,7 +66,8 @@ void parse_integer_like(const H5::DataSet& handle, Host* ptr, Function check, co
const char* placeholder_name = "missing-value-placeholder";
has_missing = handle.attrExists(placeholder_name);
if (has_missing) {
auto attr = ritsuko::hdf5::open_missing_placeholder_attribute(handle, placeholder_name, /* type_class_only = */ version.lt(1, 2));
auto attr = handle.openAttribute(placeholder_name);
ritsuko::hdf5::check_missing_placeholder_attribute(handle, attr, /* type_class_only = */ version.lt(1, 2));
attr.read(H5::PredType::NATIVE_INT32, &missing_value);
}
}
Expand Down Expand Up @@ -103,7 +95,7 @@ void parse_string_like(const H5::DataSet& handle, Host* ptr, Function check, hsi
throw std::runtime_error("expected a string dataset");
}

auto missingness = ritsuko::hdf5::load_string_missing_placeholder(handle, "missing-value-placeholder");
auto missingness = ritsuko::hdf5::open_and_load_optional_string_missing_placeholder(handle, "missing-value-placeholder");
bool has_missing = missingness.first;
std::string missing_val = missingness.second;

Expand Down Expand Up @@ -145,7 +137,8 @@ void parse_numbers(const H5::DataSet& handle, Host* ptr, Function check, const V
const char* placeholder_name = "missing-value-placeholder";
has_missing = handle.attrExists(placeholder_name);
if (has_missing) {
auto attr = ritsuko::hdf5::open_missing_placeholder_attribute(handle, placeholder_name, /* type_class_only = */ version.lt(1, 2));
auto attr = handle.openAttribute(placeholder_name);
ritsuko::hdf5::check_missing_placeholder_attribute(handle, attr, /* type_class_only = */ version.lt(1, 2));
attr.read(H5::PredType::NATIVE_DOUBLE, &missing_value);
}
}
Expand Down Expand Up @@ -207,7 +200,7 @@ void extract_names(const H5::Group& handle, Host* ptr, hsize_t buffer_size) try
template<class Provisioner, class Externals>
std::shared_ptr<Base> parse_inner(const H5::Group& handle, Externals& ext, const Version& version, hsize_t buffer_size) try {
// Deciding what type we're dealing with.
auto object_type = check_scalar_string_attribute(handle, "uzuki_object");
auto object_type = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "uzuki_object");
std::shared_ptr<Base> output;

if (object_type == "list") {
Expand All @@ -233,7 +226,7 @@ std::shared_ptr<Base> parse_inner(const H5::Group& handle, Externals& ext, const
}

} else if (object_type == "vector") {
auto vector_type = check_scalar_string_attribute(handle, "uzuki_type");
auto vector_type = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "uzuki_type");

auto dhandle = ritsuko::hdf5::open_dataset(handle, "data");
size_t len = ritsuko::hdf5::get_1d_length(dhandle.getSpace(), true);
Expand Down Expand Up @@ -455,7 +448,7 @@ template<class Provisioner, class Externals>
ParsedList parse(const H5::Group& handle, Externals ext, Options options = Options()) {
Version version;
if (handle.attrExists("uzuki_version")) {
auto ver_str = check_scalar_string_attribute(handle, "uzuki_version");
auto ver_str = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "uzuki_version");
auto vraw = ritsuko::parse_version_string(ver_str.c_str(), ver_str.size(), /* skip_patch = */ true);
version.major = vraw.major;
version.minor = vraw.minor;
Expand Down

0 comments on commit b1d39e6

Please sign in to comment.