ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
read_scalar_string.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_READ_SCALAR_STRING_HPP
2#define RITSUKO_HDF5_READ_SCALAR_STRING_HPP
3
4#include "H5Cpp.h"
5
6#include <string>
7#include <vector>
8#include <stdexcept>
9#include <cassert>
10
11#include "get_name.hpp"
12#include "strnlen.hpp"
13#include "ReclaimVlsMemory.hpp"
14
20namespace ritsuko {
21
22namespace hdf5 {
23
29inline std::string read_scalar_string(const H5::DataSet& data) {
30 const auto& dtype = data.getDataType();
31 assert(dtype.getClass() == H5T_STRING);
32 assert(data.getSpace().getSimpleExtentNdims() == 0);
33
34 if (dtype.isVariableStr()) {
35 const auto& dspace = data.getSpace();
36 const auto& plist = H5::DSetMemXferPropList::DEFAULT;
37 char* vptr;
38 data.read(&vptr, dtype);
39 [[maybe_unused]] ReclaimVlsMemory deletor(&dtype, &dspace, &plist, &vptr);
40 if (vptr == NULL) {
41 throw std::runtime_error("detected a NULL pointer for a variable length string in '" + get_name(data) + "'");
42 }
43 std::string output(vptr);
44 return output;
45
46 } else {
47 const auto fixed_length = dtype.getSize();
48 auto buffer = sanisizer::create<std::vector<char> >(fixed_length);
49 data.read(buffer.data(), dtype);
50 return std::string(buffer.begin(), buffer.begin() + strnlen(buffer.data(), fixed_length));
51 }
52}
53
59inline std::string read_scalar_string(const H5::Attribute& attr) {
60 const auto& dtype = attr.getDataType();
61 assert(dtype.getClass() == H5T_STRING);
62 assert(attr.getSpace().getSimpleExtentNdims() == 0);
63
64 // Unfortunately, we can't just do 'std::string output; attr.read(dtype, output);',
65 // as we need to catch NULL pointers in the variable case.
66
67 if (dtype.isVariableStr()) {
68 const auto& dspace = attr.getSpace();
69 const auto& plist = H5::DSetMemXferPropList::DEFAULT; // yes, even H5Aread uses H5P_DATASET_XFER_DEFAULT.
70 char* buffer = NULL;
71 attr.read(dtype, &buffer);
72 [[maybe_unused]] ReclaimVlsMemory deletor(&dtype, &dspace, &plist, &buffer);
73 if (buffer == NULL) {
74 throw std::runtime_error("detected a NULL pointer for a variable length string attribute");
75 }
76 return std::string(buffer);
77
78 } else {
79 const auto len = dtype.getSize();
80 auto buffer = sanisizer::create<std::vector<char> >(len);
81 attr.read(dtype, buffer.data());
82 return std::string(buffer.begin(), buffer.begin() + strnlen(buffer.data(), len));
83 }
84}
85
86}
87
88}
89
90#endif
Reclaim memory allocated to HDF5's variable length strings.
Reclaim memory for HDF5's variable length strings.
Definition ReclaimVlsMemory.hpp:36
Get the name of a HDF5 object.
std::string get_name(const Object_ &obj)
Definition get_name.hpp:29
std::string read_scalar_string(const H5::DataSet &data)
Definition read_scalar_string.hpp:29
std::size_t strnlen(const char *ptr, std::size_t max)
Definition strnlen.hpp:25
Helper functions for ArtifactDB parsing and validation.
Definition Pointer.hpp:21
Determine the length of a fixed-size string.