Skip to content

Commit 5810714

Browse files
gicmojgrewe
authored andcommitted
[h5x] cleanup bitfield2bool converter
1 parent cfcf9b6 commit 5810714

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

backend/hdf5/h5x/H5DataType.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "H5DataType.hpp"
1111

1212
#include <memory>
13+
#include <cstring>
1314

1415
namespace nix {
1516
namespace hdf5 {
@@ -158,12 +159,12 @@ bool DataType::enum_equal(const DataType &other) const {
158159
}
159160

160161
} // h5x
161-
162-
static herr_t bitfield2bool(hid_t src_id, hid_t dst_id, H5T_cdata_t *cdata,
163-
size_t nl, size_t buf_stride, size_t bkg_stride, void *buf_i,
164-
void *bkg_i, hid_t dxpl) {
165-
return 0;
166-
}
162+
static herr_t bitfield2bool(hid_t src_id,
163+
hid_t dst_id,
164+
H5T_cdata_t *cdata,
165+
size_t nl, size_t buf_stride, size_t bkg_stride,
166+
void *buf_i,
167+
void *bkg_i, hid_t dxpl);
167168

168169
h5x::DataType make_file_booltype() {
169170
h5x::DataType booltype = h5x::DataType::makeEnum(H5T_NATIVE_INT8);
@@ -184,6 +185,50 @@ h5x::DataType make_mem_booltype() {
184185
static const h5x::DataType boolfiletype = make_file_booltype();
185186
static const h5x::DataType boolmemtype = make_mem_booltype();
186187

188+
189+
static herr_t bitfield2bool(hid_t src_id,
190+
hid_t dst_id,
191+
H5T_cdata_t *cdata,
192+
size_t nl,
193+
size_t buf_stride,
194+
size_t bkg_stride,
195+
void *buf_i,
196+
void *bkg_i,
197+
hid_t dxpl) {
198+
199+
// document for what this function should to at:
200+
// https://support.hdfgroup.org/HDF5/doc/H5.user/Datatypes.html#Datatypes-DataConversion
201+
202+
switch (cdata->command) {
203+
case H5T_CONV_INIT: {
204+
cdata->need_bkg = H5T_BKG_NO;
205+
HTri s_eq = H5Tequal(src_id, H5T_STD_B8LE);
206+
HTri d_eq = H5Tequal(dst_id, boolmemtype.h5id());
207+
if (s_eq.isError() || d_eq.isError() ||
208+
!(s_eq.result() && d_eq.result())) {
209+
return -1;
210+
}
211+
return 0;
212+
}
213+
case H5T_CONV_FREE:
214+
return 0; //Nothing to do
215+
case H5T_CONV_CONV:
216+
break;
217+
}
218+
219+
if (nl != 1) {
220+
//we don't support more then 1 element
221+
//since this should never occur in NIX
222+
return -1;
223+
}
224+
225+
//alias via char is fine
226+
char *buf = static_cast<char *>(buf_i);
227+
bool val = buf[0] != 0; // any nonzero value is true
228+
memcpy(buf, &val, sizeof(bool));
229+
return 0;
230+
}
231+
187232
h5x::DataType data_type_to_h5_filetype(DataType dtype) {
188233

189234
/* The switch is structured in a way in order to get

0 commit comments

Comments
 (0)