diff options
Diffstat (limited to 'tools/h5dump')
-rw-r--r-- | tools/h5dump/h5dump.c | 92 | ||||
-rw-r--r-- | tools/h5dump/h5dumpgentest.c | 54 | ||||
-rwxr-xr-x | tools/h5dump/testh5dump.sh | 4 |
3 files changed, 126 insertions, 24 deletions
diff --git a/tools/h5dump/h5dump.c b/tools/h5dump/h5dump.c index d751d25..36250d3 100644 --- a/tools/h5dump/h5dump.c +++ b/tools/h5dump/h5dump.c @@ -677,12 +677,14 @@ print_datatype(hid_t type) char *fname; hid_t nmembers, mtype, str_type; int i, j, ndims, perm[H5DUMP_MAX_RANK]; - size_t size; + size_t size=0; hsize_t dims[H5DUMP_MAX_RANK]; H5T_str_t str_pad; H5T_cset_t cset; H5G_stat_t statbuf; hid_t super; + hid_t tmp_type; + htri_t is_vlstr=FALSE; switch (H5Tget_class(type)) { case H5T_INTEGER: @@ -770,15 +772,23 @@ print_datatype(hid_t type) break; case H5T_STRING: - size = H5Tget_size(type); - str_pad = H5Tget_strpad(type); - cset = H5Tget_cset(type); + /* Make a copy of type in memory in case when TYPE is on disk, the size + * will be bigger than in memory. This makes it easier to compare + * types in memory. */ + tmp_type = H5Tcopy(type); + size = H5Tget_size(tmp_type); + str_pad = H5Tget_strpad(tmp_type); + cset = H5Tget_cset(tmp_type); + is_vlstr = H5Tis_variable_str(tmp_type); printf("H5T_STRING %s\n", dump_header_format->strblockbegin); indent += COL; indentation(indent + COL); - printf("%s %d;\n", STRSIZE, (int) size); + if(is_vlstr) + printf("%s H5T_VARIABLE;\n", STRSIZE); + else + printf("%s %d;\n", STRSIZE, (int) size); indentation(indent + COL); printf("%s ", STRPAD); @@ -800,14 +810,17 @@ print_datatype(hid_t type) printf("unknown_cset;\n"); str_type = H5Tcopy(H5T_C_S1); + if(is_vlstr) + H5Tset_size(str_type, H5T_VARIABLE); + else + H5Tset_size(str_type, size); H5Tset_cset(str_type, cset); - H5Tset_size(str_type, size); H5Tset_strpad(str_type, str_pad); indentation(indent + COL); printf("%s ", CTYPE); - if (H5Tequal(type, str_type)) { + if (H5Tequal(tmp_type, str_type)) { printf("H5T_C_S1;\n"); H5Tclose(str_type); } else { @@ -817,7 +830,7 @@ print_datatype(hid_t type) H5Tset_size(str_type, size); H5Tset_strpad(str_type, str_pad); - if (H5Tequal(type, str_type)) { + if (H5Tequal(tmp_type, str_type)) { printf("H5T_FORTRAN_S1;\n"); } else { printf("unknown_one_character_type;\n "); @@ -827,6 +840,8 @@ print_datatype(hid_t type) H5Tclose(str_type); } + H5Tclose(tmp_type); + indent -= COL; indentation(indent + COL); printf("%s", dump_header_format->strblockend); @@ -1887,13 +1902,14 @@ dump_data(hid_t obj_id, int obj_data, struct subset_t *sset) alloc_size = nelmts * MAX(H5Tget_size(type), H5Tget_size(p_type)); assert(alloc_size == (hsize_t)((size_t)alloc_size)); /*check for overflow*/ + buf = malloc((size_t)alloc_size); assert(buf); if (H5Aread(obj_id, p_type, buf) >= 0) status = h5tools_dump_mem(stdout, outputformat, obj_id, p_type, - space, buf, depth); - + space, buf, depth); + free(buf); H5Tclose(p_type); H5Sclose(space); @@ -2857,10 +2873,25 @@ done: d_status = EXIT_FAILURE; free_handler(hand, argc); - + + for(i=0; i<info.group_table->nobjs; i++) { + if(info.group_table->objs[i].objname) + free(info.group_table->objs[i].objname); + } free(group_table->objs); + + for(i=0; i<info.dset_table->nobjs; i++) { + if(info.dset_table->objs[i].objname) + free(info.dset_table->objs[i].objname); + } free(dset_table->objs); + + for(i=0; i<info.type_table->nobjs; i++) { + if(info.type_table->objs[i].objname) + free(info.type_table->objs[i].objname); + } free(type_table->objs); + free(prefix); free(info.prefix); @@ -4557,12 +4588,14 @@ xml_print_strs(hid_t did, int source) { herr_t e; hid_t type, space; - char *buf; + void *buf; char *bp; char *onestring; hsize_t ssiz; - size_t tsiz; + size_t tsiz, str_size; size_t i; + htri_t is_vlstr; + if (source == DATASET_DATA) { type = H5Dget_type(did); } else if (source == ATTRIBUTE_DATA) { @@ -4575,12 +4608,14 @@ xml_print_strs(hid_t did, int source) /* return an error */ return FAIL; } + is_vlstr = H5Tis_variable_str(type); + if (source == DATASET_DATA) { space = H5Dget_space(did); ssiz = H5Sget_simple_extent_npoints(space); ssiz *= H5Tget_size(type); - buf = calloc((size_t)ssiz, sizeof(char)); + buf = malloc((size_t)ssiz); if (buf == NULL) { return FAIL; @@ -4596,11 +4631,12 @@ xml_print_strs(hid_t did, int source) space = H5Aget_space(did); ssiz = H5Sget_simple_extent_npoints(space); ssiz *= H5Tget_size(type); - - buf = calloc((size_t)ssiz, sizeof(char)); + + buf = malloc((size_t)ssiz); if (buf == NULL) { return FAIL; } + e = H5Aread(did, type, buf); if (e < 0) { free(buf); @@ -4611,28 +4647,36 @@ xml_print_strs(hid_t did, int source) return FAIL; } -/* pull out each string... */ + /* pull out each string... */ ssiz = H5Sget_simple_extent_npoints(space); tsiz = H5Tget_size(type); - onestring = (char *) calloc((size_t)tsiz, sizeof(char)); - bp = buf; + bp = (char*)buf; + if(!is_vlstr) + onestring = (char *) calloc(tsiz, sizeof(char)); for (i = 0; i < ssiz; i++) { - strncpy(onestring, bp, tsiz); + if(is_vlstr) { + onestring = *(char **)bp; + str_size = (size_t)strlen(onestring); + } else { + strncpy(onestring, bp, tsiz); + str_size = tsiz; + } indentation(indent + COL); if (!onestring) { printf("\"%s\"\n", "NULL"); } else { - char *t_onestring = xml_escape_the_string(onestring, (int)tsiz); + char *t_onestring = xml_escape_the_string(onestring, (int)str_size); - printf("\"%s\"\n", xml_escape_the_string(onestring, (int)tsiz)); + printf("\"%s\"\n", t_onestring); free(t_onestring); } - - bp += tsiz; + + bp += tsiz; } + return SUCCEED; } diff --git a/tools/h5dump/h5dumpgentest.c b/tools/h5dump/h5dumpgentest.c index 8f6e3bc..f0e9515 100644 --- a/tools/h5dump/h5dumpgentest.c +++ b/tools/h5dump/h5dumpgentest.c @@ -63,6 +63,7 @@ #define FILE35 "tfamily%05d.h5" #define FILE36 "tmulti" #define FILE37 "tlarge_objname.h5" +#define FILE38 "tvlstr.h5" #define LENSTR 50 #define LENSTR2 11 @@ -108,6 +109,9 @@ typedef struct s1_t { #define ARRAY3_DIM1 6 #define ARRAY3_DIM2 3 +/* VL string datatype name */ +#define VLSTR_TYPE "vl_string_type" + static void gent_group(void) { hid_t fid, group; @@ -2838,6 +2842,55 @@ static void gent_large_objname(void) H5Fclose(fid); } +static void gent_vlstr(void) +{ + const char *wdata[SPACE1_DIM1]= { + "Four score and seven years ago our forefathers brought forth on this continent a new nation,", + "conceived in liberty and dedicated to the proposition that all men are created equal.", + "Now we are engaged in a great civil war,", + "testing whether that nation or any nation so conceived and so dedicated can long endure." + }; /* Information to write */ + char *string_att= "This is the string for the attribute"; + hid_t fid1; /* HDF5 File IDs */ + hid_t dataset, root; /* Dataset ID */ + hid_t sid1, dataspace;/* Dataspace ID */ + hid_t tid1, att; /* Datatype ID */ + hsize_t dims1[] = {SPACE1_DIM1}; + + /* Create file */ + fid1 = H5Fcreate(FILE38, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL); + + /* Create a VL string datatype to refer to */ + tid1 = H5Tcopy (H5T_C_S1); + H5Tset_size (tid1,H5T_VARIABLE); + + /* Create a dataset and write VL string to it. */ + dataset=H5Dcreate(fid1,"Dataset1",tid1,sid1,H5P_DEFAULT); + H5Dwrite(dataset,tid1,H5S_ALL,H5S_ALL,H5P_DEFAULT,wdata); + H5Dclose(dataset); + + /* Create a named VL string type. Change padding of datatype */ + H5Tset_strpad(tid1, H5T_STR_NULLPAD); + H5Tcommit(fid1, "vl_string_type", tid1); + + /* Create an group attribute of VL string type */ + root = H5Gopen(fid1, "/"); + dataspace = H5Screate(H5S_SCALAR); + + att = H5Acreate(root, "test_scalar", tid1, dataspace, H5P_DEFAULT); + H5Awrite(att, tid1, &string_att); + + /* Close */ + H5Tclose(tid1); + H5Sclose(sid1); + H5Sclose(dataspace); + H5Aclose(att); + H5Gclose(root); + H5Fclose(fid1); +} + int main(void) { gent_group(); @@ -2888,6 +2941,7 @@ int main(void) gent_multi(); gent_large_objname(); + gent_vlstr(); return 0; } diff --git a/tools/h5dump/testh5dump.sh b/tools/h5dump/testh5dump.sh index ec22362..99c1a89 100755 --- a/tools/h5dump/testh5dump.sh +++ b/tools/h5dump/testh5dump.sh @@ -144,6 +144,9 @@ TOOLTEST tvldtypes2.ddl tvldtypes2.h5 TOOLTEST tvldtypes3.ddl tvldtypes3.h5 TOOLTEST tvldtypes4.ddl tvldtypes4.h5 +#test for file with variable length string data +TOOLTEST tvlstr.ddl tvlstr.h5 + # test for files with array data TOOLTEST tarray1.ddl tarray1.h5 TOOLTEST tarray2.ddl tarray2.h5 @@ -218,6 +221,7 @@ TOOLTEST tarray7.h5.xml --xml tarray7.h5 TOOLTEST tvldtypes1.h5.xml --xml tvldtypes1.h5 TOOLTEST tvldtypes2.h5.xml --xml tvldtypes2.h5 TOOLTEST tvldtypes3.h5.xml --xml tvldtypes3.h5 +TOOLTEST tvlstr.h5.xml --xml tvlstr.h5 TOOLTEST tsaf.h5.xml --xml tsaf.h5 TOOLTEST tempty.h5.xml --xml tempty.h5 |