ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
load_attribute.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_LOAD_ATTRIBUTE_HPP
2#define RITSUKO_HDF5_LOAD_ATTRIBUTE_HPP
3
4#include "H5Cpp.h"
5
6#include <vector>
7#include <string>
8
9#include "get_1d_length.hpp"
11#include "_strings.hpp"
12
18namespace ritsuko {
19
20namespace hdf5 {
21
27inline std::string load_scalar_string_attribute(const H5::Attribute& attr) {
28 auto dtype = attr.getDataType();
29
30 // Unfortunately, we can't just do 'std::string output; attr.read(dtype, output);',
31 // as we need to catch NULL pointers in the variable case.
32
33 if (dtype.isVariableStr()) {
34 auto mspace = attr.getSpace();
35 char* buffer;
36 attr.read(dtype, &buffer);
37 [[maybe_unused]] VariableStringCleaner deletor(dtype.getId(), mspace.getId(), &buffer);
38 if (buffer == NULL) {
39 throw std::runtime_error("detected a NULL pointer for a variable length string attribute");
40 }
41 return std::string(buffer);
42
43 } else {
44 size_t len = dtype.getSize();
45 std::vector<char> buffer(len);
46 attr.read(dtype, buffer.data());
47 auto ptr = buffer.data();
48 return std::string(ptr, ptr + find_string_length(ptr, len));
49 }
50}
51
59inline std::vector<std::string> load_1d_string_attribute(const H5::Attribute& attr, hsize_t full_length) {
60 auto dtype = attr.getDataType();
61 auto mspace = attr.getSpace();
62 std::vector<std::string> output;
63 output.reserve(full_length);
64
65 if (dtype.isVariableStr()) {
66 std::vector<char*> buffer(full_length);
67 attr.read(dtype, buffer.data());
68 [[maybe_unused]] VariableStringCleaner deletor(dtype.getId(), mspace.getId(), buffer.data());
69 for (hsize_t i = 0; i < full_length; ++i) {
70 if (buffer[i] == NULL) {
71 throw std::runtime_error("detected a NULL pointer for a variable length string attribute");
72 }
73 output.emplace_back(buffer[i]);
74 }
75
76 } else {
77 size_t len = dtype.getSize();
78 std::vector<char> buffer(len * full_length);
79 attr.read(dtype, buffer.data());
80 auto ptr = buffer.data();
81 for (size_t i = 0; i < full_length; ++i, ptr += len) {
82 output.emplace_back(ptr, ptr + find_string_length(ptr, len));
83 }
84 }
85
86 return output;
87}
88
95inline std::vector<std::string> load_1d_string_attribute(const H5::Attribute& attr) {
96 return load_1d_string_attribute(attr, get_1d_length(attr.getSpace(), false));
97}
98
105template<typename Type_>
106Type_ load_scalar_numeric_attribute(const H5::Attribute& attr) {
107 Type_ val;
108 const auto& mtype = as_numeric_datatype<Type_>();
109 attr.read(mtype, &val);
110 return val;
111}
112
120template<typename Type_>
121std::vector<Type_> load_1d_numeric_attribute(const H5::Attribute& attr, hsize_t full_length) {
122 const auto& mtype = as_numeric_datatype<Type_>();
123 std::vector<Type_> buffer(full_length);
124 attr.read(mtype, buffer.data());
125 return buffer;
126}
127
135template<typename Type_>
136std::vector<Type_> load_1d_numeric_attribute(const H5::Attribute& attr) {
137 return load_1d_numeric_attribute<Type_>(attr, get_1d_length(attr.getSpace(), false));
138}
139
140}
141
142}
143
144#endif
Choose a HDF5 datatype.
Get the length of a 1-dimensional HDF5 dataset.
Type_ load_scalar_numeric_attribute(const H5::Attribute &attr)
Definition load_attribute.hpp:106
std::vector< std::string > load_1d_string_attribute(const H5::Attribute &attr, hsize_t full_length)
Definition load_attribute.hpp:59
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:26
std::vector< Type_ > load_1d_numeric_attribute(const H5::Attribute &attr, hsize_t full_length)
Definition load_attribute.hpp:121
hsize_t get_1d_length(const H5::DataSpace &space, bool allow_scalar)
Definition get_1d_length.hpp:25
std::string load_scalar_string_attribute(const H5::Attribute &attr)
Definition load_attribute.hpp:27
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15