/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/detail/vlen_string.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/detail/vlen_string.hh Source File
DiFfRG
vlen_string.hh
Go to the documentation of this file.
1#pragma once
2
3#include <hdf5lib/datatype.hh>
4
5#include <cstddef>
6#include <cstring>
7#include <string>
8#include <vector>
9
11{
14 inline std::vector<const char *> pack_strings(const std::string *src, std::size_t n)
15 {
16 std::vector<const char *> out(n);
17 for (std::size_t i = 0; i < n; ++i)
18 out[i] = src[i].c_str();
19 return out;
20 }
21
27 inline void read_vlen_strings(hid_t dset, hid_t /*mem_space*/, hid_t /*file_space*/, hid_t mem_type,
28 std::string *dst, std::size_t n)
29 {
30 std::vector<char *> raw(n, nullptr);
31 throw_if_negative(H5Dread(dset, mem_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, raw.data()),
32 "H5Dread (vlen string) failed");
33 for (std::size_t i = 0; i < n; ++i)
34 dst[i] = raw[i] != nullptr ? std::string(raw[i]) : std::string();
35
36 // Reclaim the per-element allocations. H5Dvlen_reclaim was renamed in
37 // HDF5 1.12; we use H5Treclaim, which is the supported spelling in 2.x.
38 // H5Treclaim requires an explicit (non-H5S_ALL) dataspace.
39 hid_t reclaim_space = H5Dget_space(dset);
40 if (reclaim_space >= 0) {
41 H5Treclaim(mem_type, reclaim_space, H5P_DEFAULT, raw.data());
42 H5Sclose(reclaim_space);
43 }
44 }
45} // namespace DiFfRG::hdf5::detail
Definition vlen_string.hh:11
std::vector< const char * > pack_strings(const std::string *src, std::size_t n)
Definition vlen_string.hh:14
void read_vlen_strings(hid_t dset, hid_t, hid_t, hid_t mem_type, std::string *dst, std::size_t n)
Definition vlen_string.hh:27
void throw_if_negative(hid_t id, const char *what)
Definition handle.hh:11