33#include < H5Cpp.h>
44#include < cassert>
55#include < ranges>
6+ #include < type_traits>
67#include < vector>
78
89#include < iostream>
@@ -120,6 +121,7 @@ inline H5::PredType get_type(H5::DataSet& dataset) {
120121}
121122
122123template <typename H5GroupOrFile, std::ranges::contiguous_range R>
124+ requires (!std::is_same_v<std::remove_cvref_t <R>, std::string>)
123125void write_dataset (H5GroupOrFile& f, const std::string& label, R&& r) {
124126 using T = std::ranges::range_value_t <R>;
125127 hsize_t size = std::ranges::size (r);
@@ -132,6 +134,21 @@ void write_dataset(H5GroupOrFile& f, const std::string& label, R&& r) {
132134 dataspace.close ();
133135}
134136
137+ template <typename H5GroupOrFile, std::ranges::contiguous_range R>
138+ requires (std::is_same_v<std::remove_cvref_t <R>, std::string>)
139+ void write_dataset (H5GroupOrFile& f, const std::string& label, R&& r) {
140+ H5::StrType string_type (H5::PredType::C_S1, r.size ());
141+ H5Tset_cset (string_type, H5T_CSET_UTF8);
142+ hsize_t size = r.size ();
143+ H5::DataSpace dataspace (1 , &size);
144+
145+ auto dataset = f.createDataSet (label.c_str (), string_type, H5S_SCALAR);
146+
147+ dataset.write (r.c_str (), string_type);
148+
149+ dataset.close ();
150+ }
151+
135152inline std::string get_attribute (H5::H5Object& f, const std::string& key) {
136153 auto attribute = f.openAttribute (key.c_str ());
137154
@@ -151,6 +168,7 @@ inline std::string get_attribute(H5::H5Object& f, const std::string& key) {
151168inline void set_attribute (H5::H5Object& f, const std::string& key,
152169 const std::string& value) {
153170 H5::StrType string_type (H5::PredType::C_S1, value.size ());
171+ H5Tset_cset (string_type, H5T_CSET_UTF8);
154172 hsize_t size = value.size ();
155173 H5::DataSpace dataspace (1 , &size);
156174
0 commit comments