diff options
-rw-r--r-- | hl/src/H5DS.c | 120 | ||||
-rw-r--r-- | hl/src/H5DS.h | 13 | ||||
-rw-r--r-- | hl/test/test_ds.c | 238 |
3 files changed, 256 insertions, 115 deletions
diff --git a/hl/src/H5DS.c b/hl/src/H5DS.c index bc22625..7939627 100644 --- a/hl/src/H5DS.c +++ b/hl/src/H5DS.c @@ -34,7 +34,7 @@ *------------------------------------------------------------------------- */ -herr_t H5DSset_scale(hid_t did, +herr_t H5DSset_scale(hid_t dsid, char *dimname) { int has_dimlist; @@ -45,7 +45,7 @@ herr_t H5DSset_scale(hid_t did, *------------------------------------------------------------------------- */ /* get ID type */ - if ((it = H5Iget_type(did))<0) + if ((it = H5Iget_type(dsid))<0) return FAIL; if (H5I_DATASET!=it) @@ -56,8 +56,8 @@ herr_t H5DSset_scale(hid_t did, *------------------------------------------------------------------------- */ - /* try to find the attribute "DIMENSION_LIST" on the >>data<< dataset */ - if ((has_dimlist = H5LT_find_attribute(did,DIMENSION_LIST))<0) + /* try to find the attribute "DIMENSION_LIST" */ + if ((has_dimlist = H5LT_find_attribute(dsid,DIMENSION_LIST))<0) return FAIL; if (has_dimlist == 1) @@ -68,12 +68,12 @@ herr_t H5DSset_scale(hid_t did, *------------------------------------------------------------------------- */ - if (H5LT_set_attribute_string(did,"CLASS",DIMENSION_SCALE_CLASS)<0) + if (H5LT_set_attribute_string(dsid,"CLASS",DIMENSION_SCALE_CLASS)<0) return FAIL; if (dimname!=NULL) { - if (H5LT_set_attribute_string(did,"NAME",dimname)<0) + if (H5LT_set_attribute_string(dsid,"NAME",dimname)<0) return FAIL; } @@ -1028,8 +1028,8 @@ out: */ herr_t H5DSset_label(hid_t did, - char *label, - unsigned int idx) + unsigned int idx, + char *label) { int has_labels; hid_t sid; /* space ID */ @@ -1135,9 +1135,6 @@ herr_t H5DSset_label(hid_t did, if ((tid = H5Aget_type(aid))<0) goto out; - - if ((sid = H5Aget_space(aid))<0) - goto out; /* allocate and initialize */ buf = (char **)malloc((size_t)rank * sizeof(char *)); @@ -1149,22 +1146,14 @@ herr_t H5DSset_label(hid_t did, if (H5Aread(aid,tid,buf)<0) goto out; - for(i=0; i<(unsigned int)rank; i++) - { - if (idx == i ) - { - /* store the label information in the required index */ - buf[idx] = label; - } - } + /* store the label information in the required index */ + buf[idx] = label; /* write the attribute with the new references */ if (H5Awrite(aid,tid,buf)<0) goto out; /* close */ - if (H5Sclose(sid)<0) - goto out; if (H5Tclose(tid)<0) goto out; if (H5Aclose(aid)<0) @@ -1189,8 +1178,12 @@ out: * Function: H5DSget_label * * Purpose: get a label for dimension IDX + * Up to 'size' characters are stored in 'label' followed by a '\0' string + * terminator. If the label is longer than 'size'-1, + * the string terminator is stored in the last position of the buffer to + * properly terminate the string. * - * Return: Success: SUCCESS, Failure: FAIL + * Return: 0 if no label found, size of label if found, Failure: FAIL * * Programmer: pvn@ncsa.uiuc.edu * @@ -1202,9 +1195,10 @@ out: * *------------------------------------------------------------------------- */ -herr_t H5DSget_label(hid_t did, - char *label, - unsigned int idx) +ssize_t H5DSget_label(hid_t did, + unsigned int idx, + char *label, + size_t size) { int has_labels; hid_t sid; /* space ID */ @@ -1213,7 +1207,8 @@ herr_t H5DSget_label(hid_t did, int rank; /* rank of dataset */ char **buf=NULL; /* buffer to store in the attribute */ H5I_type_t it; /* ID type */ - unsigned int i; + size_t nbytes; + size_t copy_len; /*------------------------------------------------------------------------- * parameter checking @@ -1235,8 +1230,13 @@ herr_t H5DSget_label(hid_t did, if ((has_labels = H5LT_find_attribute(did,DIMENSION_LABELS))<0) return FAIL; + /* return 0 and NULL for label if no label found */ if (has_labels == 0) - return FAIL; + { + if (label) + label=NULL; + return 0; + } /* get dataset space */ if ((sid = H5Dget_space(did))<0) @@ -1251,7 +1251,7 @@ herr_t H5DSget_label(hid_t did, goto out; /*------------------------------------------------------------------------- - * make the attribute and insert label + * open the attribute and read label *------------------------------------------------------------------------- */ @@ -1262,9 +1262,6 @@ herr_t H5DSget_label(hid_t did, if ((tid = H5Aget_type(aid))<0) goto out; - - if ((sid = H5Aget_space(aid))<0) - goto out; /* allocate and initialize */ buf = (char **)malloc((size_t)rank * sizeof(char *)); @@ -1275,19 +1272,22 @@ herr_t H5DSget_label(hid_t did, /* read */ if (H5Aread(aid,tid,buf)<0) goto out; - - for(i=0; i<(unsigned int)rank; i++) - { - if (idx == i ) - { - /* store the label information in the required index */ - strcpy(label,buf[idx]); - } + + /* get the real string length */ + nbytes = HDstrlen(buf[idx]); + + /* compute the string length which will fit into the user's buffer */ + copy_len = MIN(size-1, nbytes); + + /* copy all/some of the name */ + if( label && copy_len>0) { + HDmemcpy(label, buf[idx], copy_len); + + /* terminate the string */ + label[copy_len]='\0'; } /* close */ - if (H5Sclose(sid)<0) - goto out; if (H5Tclose(tid)<0) goto out; if (H5Aclose(aid)<0) @@ -1296,8 +1296,7 @@ herr_t H5DSget_label(hid_t did, free(buf); } - - return SUCCESS; + return (ssize_t) nbytes; /* error zone, gracefully close */ out: @@ -1314,9 +1313,13 @@ out: /*------------------------------------------------------------------------- * Function: H5DSget_scale_name * - * Purpose: get the name of a DS + * Purpose: get the scale name of DID + * Up to 'size' characters are stored in 'name' followed by a '\0' string + * terminator. If the name is longer than 'size'-1, + * the string terminator is stored in the last position of the buffer to + * properly terminate the string. * - * Return: Success: SUCCESS, Failure: FAIL + * Return: size of name if found, Failure: FAIL * * Programmer: pvn@ncsa.uiuc.edu * @@ -1337,7 +1340,8 @@ ssize_t H5DSget_scale_name(hid_t did, hid_t tid; /* attribute type ID */ hid_t sid; /* space ID */ H5I_type_t it; /* ID type */ - size_t len; + size_t nbytes; + size_t copy_len; int has_name; char *buf=NULL; @@ -1384,11 +1388,11 @@ ssize_t H5DSget_scale_name(hid_t did, goto out; /* get the size */ - if ((len = H5Tget_size(tid))<0) + if ((nbytes = H5Tget_size(tid))<0) goto out; /* allocate a temporary buffer */ - buf = (char*)malloc(len * sizeof(char)); + buf = (char*)malloc(nbytes * sizeof(char)); if (buf == NULL) goto out; @@ -1396,13 +1400,16 @@ ssize_t H5DSget_scale_name(hid_t did, if (H5Aread(aid,tid,buf)<0) goto out; - /* compute the string length which will fit into the user's buffer, copy all/some of the name */ - if(name) - { - HDstrncpy(name, buf, MIN(len+1,size)); - if(len >= size) - name[size-1]='\0'; - } /* end if */ + /* compute the string length which will fit into the user's buffer */ + copy_len = MIN(size-1, nbytes); + + /* copy all/some of the name */ + if (name && copy_len>0) { + HDmemcpy(name, buf, copy_len); + + /* terminate the string */ + name[copy_len]='\0'; + } /* close */ if (H5Tclose(tid)<0) @@ -1417,8 +1424,7 @@ ssize_t H5DSget_scale_name(hid_t did, buf=NULL; } - - return (ssize_t) len; + return (ssize_t) nbytes; /* error zone, gracefully close */ out: diff --git a/hl/src/H5DS.h b/hl/src/H5DS.h index 4ae47ef..327f602 100644 --- a/hl/src/H5DS.h +++ b/hl/src/H5DS.h @@ -48,7 +48,7 @@ herr_t H5DSdetach_scale(hid_t did, hid_t dsid, unsigned int idx); -herr_t H5DSset_scale(hid_t did, +herr_t H5DSset_scale(hid_t dsid, char *dimname); herr_t H5DSget_nscales(hid_t did, @@ -56,12 +56,13 @@ herr_t H5DSget_nscales(hid_t did, int *nscales); herr_t H5DSset_label(hid_t did, - char *label, - unsigned int idx); + unsigned int idx, + char *label); -herr_t H5DSget_label(hid_t did, - char *label, - unsigned int idx); +ssize_t H5DSget_label(hid_t did, + unsigned int idx, + char *label, + size_t size); ssize_t H5DSget_scale_name(hid_t did, char *name, diff --git a/hl/test/test_ds.c b/hl/test/test_ds.c index 31a9708..fa6a25c 100644 --- a/hl/test/test_ds.c +++ b/hl/test/test_ds.c @@ -34,7 +34,23 @@ static int test_errors(void); #define DIM2_SIZE 4 #define DIM0 0 #define DIM1 1 -#define SCALE_1_NAME "Latitude" + +#define DS_1_NAME "ds_a_1" +#define DS_11_NAME "ds_a_11" +#define DS_2_NAME "ds_a_2" +#define DS_21_NAME "ds_a_21" +#define DS_22_NAME "ds_a_22" + +#define SCALE_1_NAME "Latitude set 0" +#define SCALE_11_NAME "Latitude set 1" +#define SCALE_2_NAME "Longitude set 0" +#define SCALE_21_NAME "Longitude set 1" +#define SCALE_22_NAME "Longitude set 2" + +#define DIM0_LABEL "Latitude" +#define DIM1_LABEL "Longitude" + + /*------------------------------------------------------------------------- @@ -95,13 +111,20 @@ static int test_simple(void) int s2_wbuf[DIM2_SIZE] = {100,200,300,400}; /* data of DS 2 dataset */ int s21_wbuf[DIM2_SIZE] = {10,20,30,40}; /* data of DS 2 dataset */ int s22_wbuf[DIM2_SIZE] = {5,10,50,300}; /* data of DS 2 dataset */ - char s1_label[16]; /* read label for DS 1 */ - char s2_label[16]; /* read label for DS 2 */ + char dim0_label[16]; /* read label for DIM 0 */ + char dim1_label[16]; /* read label for DIM 1 */ + char *dim0_labeld; /* read label for DIM 0 */ + char *dim1_labeld; /* read label for DIM 1 */ + char dim0_labels[3]; /* read label for DIM 0 */ + char dim1_labels[3]; /* read label for DIM 1 */ + ssize_t dim0_label_size; /* lenght of label buffer */ + ssize_t dim1_label_size; /* lenght of label buffer */ unsigned int dim; /* dataset dimension index */ int scale_idx; /* scale index */ int nscales; /* number of scales in DIM */ ssize_t name_len; /* lenght of name buffer */ char *name_out=NULL; /* scale name buffer */ + char snames[3]; /* scale name buffer */ int i, j; @@ -126,23 +149,23 @@ static int test_simple(void) goto out; /* make a DS dataset for the first dimension */ - if (H5LTmake_dataset_int(fid,"ds_a_1",rankds,s1_dim,s1_wbuf)<0) + if (H5LTmake_dataset_int(fid,DS_1_NAME,rankds,s1_dim,s1_wbuf)<0) goto out; /* make a DS dataset with an alternate scale for the 2nd dimension */ - if (H5LTmake_dataset_int(fid,"ds_a_11",rankds,s1_dim,s11_wbuf)<0) + if (H5LTmake_dataset_int(fid,DS_11_NAME,rankds,s1_dim,s11_wbuf)<0) goto out; /* make a DS dataset for the second dimension */ - if (H5LTmake_dataset_int(fid,"ds_a_2",rankds,s2_dim,s2_wbuf)<0) + if (H5LTmake_dataset_int(fid,DS_2_NAME,rankds,s2_dim,s2_wbuf)<0) goto out; /* make a DS dataset with an alternate scale for the 2nd dimension */ - if (H5LTmake_dataset_int(fid,"ds_a_21",rankds,s2_dim,s21_wbuf)<0) + if (H5LTmake_dataset_int(fid,DS_21_NAME,rankds,s2_dim,s21_wbuf)<0) goto out; /* make a DS dataset with an alternate scale for the 2nd dimension */ - if (H5LTmake_dataset_int(fid,"ds_a_22",rankds,s2_dim,s22_wbuf)<0) + if (H5LTmake_dataset_int(fid,DS_22_NAME,rankds,s2_dim,s22_wbuf)<0) goto out; @@ -158,15 +181,15 @@ static int test_simple(void) goto out; /*------------------------------------------------------------------------- - * attach the "ds_a_1" dimension scale to "dset_a" + * attach the DS_1_NAME dimension scale to "dset_a" *------------------------------------------------------------------------- */ /* get the DS dataset id */ - if ((dsid = H5Dopen(fid,"ds_a_1"))<0) + if ((dsid = H5Dopen(fid,DS_1_NAME))<0) goto out; - /* attach the "ds_a_1" dimension scale to "dset_a" at dimension 0 */ + /* attach the DS_1_NAME dimension scale to "dset_a" at dimension 0 */ if (H5DSattach_scale(did,dsid,DIM0)<0) goto out; @@ -175,15 +198,15 @@ static int test_simple(void) goto out; /*------------------------------------------------------------------------- - * attach the "ds_a_11" dimension scale to "dset_a" + * attach the DS_11_NAME dimension scale to "dset_a" *------------------------------------------------------------------------- */ /* get the DS dataset id */ - if ((dsid = H5Dopen(fid,"ds_a_11"))<0) + if ((dsid = H5Dopen(fid,DS_11_NAME))<0) goto out; - /* attach the "ds_a_11" dimension scale to "dset_a" at dimension 0 */ + /* attach the DS_11_NAME dimension scale to "dset_a" at dimension 0 */ if (H5DSattach_scale(did,dsid,DIM0)<0) goto out; @@ -192,12 +215,12 @@ static int test_simple(void) goto out; /*------------------------------------------------------------------------- - * attach the "ds_a_2" dimension scale to "dset_a" + * attach the DS_2_NAME dimension scale to "dset_a" *------------------------------------------------------------------------- */ /* get the DS dataset id */ - if ((dsid = H5Dopen(fid,"ds_a_2"))<0) + if ((dsid = H5Dopen(fid,DS_2_NAME))<0) goto out; /* attach the "ds2" dimension scale to "dset_a" as the 2nd dimension */ @@ -209,15 +232,15 @@ static int test_simple(void) goto out; /*------------------------------------------------------------------------- - * attach the "ds_a_21" dimension scale to "dset_a" + * attach the DS_21_NAME dimension scale to "dset_a" *------------------------------------------------------------------------- */ /* get the DS dataset id */ - if ((dsid = H5Dopen(fid,"ds_a_21"))<0) + if ((dsid = H5Dopen(fid,DS_21_NAME))<0) goto out; - /* attach the "ds_a_21" dimension scale to "dset_a" as the 2nd dimension */ + /* attach the DS_21_NAME dimension scale to "dset_a" as the 2nd dimension */ if (H5DSattach_scale(did,dsid,DIM1)<0) goto out; @@ -226,12 +249,12 @@ static int test_simple(void) goto out; /*------------------------------------------------------------------------- - * attach the "ds_a_22" dimension scale to "dset_a" + * attach the DS_22_NAME dimension scale to "dset_a" *------------------------------------------------------------------------- */ /* get the DS dataset id */ - if ((dsid = H5Dopen(fid,"ds_a_22"))<0) + if ((dsid = H5Dopen(fid,DS_22_NAME))<0) goto out; /* attach the "ds22" dimension scale to "dset_a" as the 2nd dimension */ @@ -249,28 +272,28 @@ static int test_simple(void) *------------------------------------------------------------------------- */ - if ((dsid = H5Dopen(fid,"ds_a_1"))<0) + if ((dsid = H5Dopen(fid,DS_1_NAME))<0) goto out; if (H5DSis_attached(did,dsid,DIM0)<=0) goto out; if (H5Dclose(dsid)) goto out; - if ((dsid = H5Dopen(fid,"ds_a_2"))<0) + if ((dsid = H5Dopen(fid,DS_2_NAME))<0) goto out; if (H5DSis_attached(did,dsid,DIM1)<=0) goto out; if (H5Dclose(dsid)) goto out; - if ((dsid = H5Dopen(fid,"ds_a_21"))<0) + if ((dsid = H5Dopen(fid,DS_21_NAME))<0) goto out; if (H5DSis_attached(did,dsid,DIM1)<=0) goto out; if (H5Dclose(dsid)) goto out; - if ((dsid = H5Dopen(fid,"ds_a_22"))<0) + if ((dsid = H5Dopen(fid,DS_22_NAME))<0) goto out; if (H5DSis_attached(did,dsid,DIM1)<=0) goto out; @@ -927,83 +950,194 @@ static int test_simple(void) if ((did = H5Dopen(fid,"dset_a"))<0) goto out; - if (H5DSset_label(did,"DY",0)<0) - goto out; +/*------------------------------------------------------------------------- + * set label + *------------------------------------------------------------------------- + */ - if (H5DSset_label(did,"DX",1)<0) + if (H5DSset_label(did,DIM0,DIM0_LABEL)<0) + goto out; + if (H5DSset_label(did,DIM1,DIM1_LABEL)<0) goto out; - if (H5DSget_label(did,s1_label,0)<0) +/*------------------------------------------------------------------------- + * get the scale name using a static buffer + *------------------------------------------------------------------------- + */ + + if (H5DSget_label(did,DIM0,dim0_label,sizeof(dim0_label))<0) + goto out; + if (H5DSget_label(did,DIM1,dim1_label,sizeof(dim1_label))<0) goto out; - if (H5DSget_label(did,s2_label,1)<0) + if (strcmp(DIM0_LABEL,dim0_label)!=0) + goto out; + if (strcmp(DIM1_LABEL,dim1_label)!=0) goto out; - if (strcmp("DY",s1_label)!=0) +/*------------------------------------------------------------------------- + * get the scale name using a dynamic buffer + *------------------------------------------------------------------------- + */ + + if ((dim0_label_size=H5DSget_label(did,DIM0,NULL,0))<0) goto out; - if (strcmp("DX",s2_label)!=0) + if ((dim1_label_size=H5DSget_label(did,DIM1,NULL,0))<0) goto out; - if (H5Dclose(did)) + /* allocate */ + dim0_labeld = (char*)malloc(dim0_label_size * sizeof(char)); + dim1_labeld = (char*)malloc(dim1_label_size * sizeof(char)); + if ( dim0_labeld==NULL || dim1_labeld==NULL) goto out; - PASSED(); + if (H5DSget_label(did,DIM0,dim0_labeld,dim0_label_size)<0) + goto out; + if (H5DSget_label(did,DIM1,dim1_labeld,dim1_label_size)<0) + goto out; + if (strncmp(DIM0_LABEL,dim0_labeld,dim0_label_size-1)!=0) + goto out; + if (strncmp(DIM1_LABEL,dim1_labeld,dim1_label_size-1)!=0) + goto out; + + if (dim0_labeld) + { + free(dim0_labeld); + dim0_labeld=NULL; + } + if (dim1_labeld) + { + free(dim1_labeld); + dim1_labeld=NULL; + } + + /*------------------------------------------------------------------------- - * test 5: set scale/get scale name + * get the label using a static buffer smaller than the string lenght *------------------------------------------------------------------------- */ - - TESTING2("set scale/get scale name"); + if (H5DSget_label(did,DIM0,dim0_labels,sizeof(dim0_labels))<0) + goto out; + if (H5DSget_label(did,DIM1,dim1_labels,sizeof(dim1_labels))<0) + goto out; + if (strncmp(DIM0_LABEL,dim0_label,sizeof(dim0_labels)-1)!=0) + goto out; + if (strncmp(DIM1_LABEL,dim1_label,sizeof(dim1_labels)-1)!=0) + goto out; + + + + if (H5Dclose(did)) + goto out; + + PASSED(); + /*------------------------------------------------------------------------- - * make a dataset named "scale_1" and convert it to a DS dataset + * test 5: set scale/get scale name *------------------------------------------------------------------------- */ - if (H5LTmake_dataset_int(fid,"scale_1",rankds,s1_dim,s1_wbuf)<0) - goto out; + TESTING2("set scale/get scale name"); - if ((did = H5Dopen(fid,"scale_1"))<0) + if ((dsid = H5Dopen(fid,DS_1_NAME))<0) goto out; - if (H5DSset_scale(did,SCALE_1_NAME)<0) + if (H5DSset_scale(dsid,SCALE_1_NAME)<0) goto out; - /* verify that "scale_1" is a dimension scale dataset */ - if ((H5DSis_scale(did))==0) + /* verify that DS_1_NAME is a dimension scale dataset */ + if ((H5DSis_scale(dsid))==0) goto out; /*------------------------------------------------------------------------- - * get its scale name + * get the scale name using a dynamic buffer *------------------------------------------------------------------------- */ /* get the lenght of the scale name (pass NULL in name) */ - if ((name_len=H5DSget_scale_name(did,NULL,0))<0) + if ((name_len=H5DSget_scale_name(dsid,NULL,0))<0) goto out; - /* allocate a temporary buffer */ + /* allocate a buffer */ name_out = (char*)malloc(name_len * sizeof(char)); if (name_out == NULL) goto out; /* get the scale name using this buffer */ - if (H5DSget_scale_name(did,name_out,name_len)<0) + if (H5DSget_scale_name(dsid,name_out,name_len)<0) goto out; if (strcmp(SCALE_1_NAME,name_out)!=0) goto out; - if (H5Dclose(did)) - goto out; - if (name_out) { free(name_out); name_out=NULL; } +/*------------------------------------------------------------------------- + * get the scale name using a static buffer + *------------------------------------------------------------------------- + */ + + /* get the scale name using this buffer */ + if (H5DSget_scale_name(dsid,sname,sizeof(sname))<0) + goto out; + + if (strcmp(SCALE_1_NAME,sname)!=0) + goto out; + +/*------------------------------------------------------------------------- + * get the scale name using a static buffer smaller than the string lenght + *------------------------------------------------------------------------- + */ + + /* get the scale name using this buffer */ + if (H5DSget_scale_name(dsid,snames,sizeof(snames))<0) + goto out; + + if (strncmp(SCALE_1_NAME,snames,sizeof(snames)-1)!=0) + goto out; + + if (H5Dclose(dsid)) + goto out; + +/*------------------------------------------------------------------------- + * add scale names + *------------------------------------------------------------------------- + */ + + if ((dsid = H5Dopen(fid,DS_11_NAME))<0) + goto out; + if (H5DSset_scale(dsid,SCALE_11_NAME)<0) + goto out; + if (H5Dclose(dsid)) + goto out; + + if ((dsid = H5Dopen(fid,DS_2_NAME))<0) + goto out; + if (H5DSset_scale(dsid,SCALE_2_NAME)<0) + goto out; + if (H5Dclose(dsid)) + goto out; + + if ((dsid = H5Dopen(fid,DS_21_NAME))<0) + goto out; + if (H5DSset_scale(dsid,SCALE_21_NAME)<0) + goto out; + if (H5Dclose(dsid)) + goto out; + + if ((dsid = H5Dopen(fid,DS_22_NAME))<0) + goto out; + if (H5DSset_scale(dsid,SCALE_22_NAME)<0) + goto out; + if (H5Dclose(dsid)) + goto out; + PASSED(); @@ -1105,7 +1239,7 @@ static int test_simple(void) if (match_size==0) goto out; - /* both "ds_a_1" and "ds_a_2" are the on the first index */ + /* both DS_1_NAME and DS_2_NAME are the on the first index */ if (idx!=0) goto out; } |