diff options
author | Thomas White <taw@physics.org> | 2015-10-05 11:31:01 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2015-10-05 11:34:37 +0200 |
commit | d09471162b5eef05b063907bc48e6a8cc41ffcf4 (patch) | |
tree | 13bacbe12238fbeda723967dc8d6e48abef771a6 | |
parent | d55e0c829b16651662370f5ce0d2cc14dc18a633 (diff) |
Read variable-length HDF5 strings properly
-rw-r--r-- | libcrystfel/src/hdf5-file.c | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/libcrystfel/src/hdf5-file.c b/libcrystfel/src/hdf5-file.c index 24a8c282..8a7a46b7 100644 --- a/libcrystfel/src/hdf5-file.c +++ b/libcrystfel/src/hdf5-file.c @@ -1934,27 +1934,46 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name, herr_t r; hid_t sh; + htri_t v; - size = H5Tget_size(type); - tmp = malloc(size+1); + v = H5Tis_variable_str(type); + if ( v < 0 ) { + return "WTF?"; + } - sh = H5Screate(H5S_SCALAR); + if ( v && (v>0) ) { + + r = H5Dread(dh, type, H5S_ALL, H5S_ALL, + H5P_DEFAULT, &tmp); + if ( r < 0 ) { + tmp = NULL; + } - r = H5Dread(dh, type, sh, sh, H5P_DEFAULT, tmp); - if ( r < 0 ) { - free(tmp); - tmp = NULL; } else { - /* Two possibilities: - * String is already zero-terminated - * String is not terminated. - * Make sure things are done properly... */ - tmp[size] = '\0'; - chomp(tmp); + size = H5Tget_size(type); + tmp = malloc(size+1); + + sh = H5Screate(H5S_SCALAR); + + r = H5Dread(dh, type, sh, sh, H5P_DEFAULT, tmp); + if ( r < 0 ) { + free(tmp); + tmp = NULL; + } else { + + /* Two possibilities: + * String is already zero-terminated + * String is not terminated. + * Make sure things are done properly... */ + tmp[size] = '\0'; + chomp(tmp); + } + + H5Sclose(sh); + } - H5Sclose(sh); } else { |