#include "H5DSprivate.h"
#include "H5LTprivate.h"
#include "H5IMprivate.h"
#include "H5TBprivate.h"
static herr_t H5DS_is_reserved(hid_t did);
static hid_t H5DS_get_REFLIST_type(void);
herr_t
H5DSset_scale(hid_t dsid, const char *dimname)
{
int has_dimlist;
H5I_type_t it;
if ((it = H5Iget_type(dsid)) < 0)
return FAIL;
if (H5I_DATASET!=it)
return FAIL;
if ((has_dimlist = H5LT_find_attribute(dsid,DIMENSION_LIST)) < 0)
return FAIL;
if (has_dimlist == 1)
return FAIL;
if (H5LT_set_attribute_string(dsid,"CLASS",DIMENSION_SCALE_CLASS) < 0)
return FAIL;
if (dimname!=NULL)
{
if (H5LT_set_attribute_string(dsid,"NAME",dimname) < 0)
return FAIL;
}
return SUCCEED;
}
herr_t
H5DSattach_scale(hid_t did, hid_t dsid, unsigned int idx)
{
int has_dimlist;
int has_reflist;
int is_ds;
hssize_t nelmts;
hid_t sid;
hid_t tid = -1;
hid_t ntid = -1;
hid_t aid = -1;
int rank;
hsize_t dims[1];
ds_list_t dsl;
ds_list_t *dsbuf = NULL;
hobj_ref_t ref_to_ds;
hobj_ref_t ref_j;
hvl_t *buf = NULL;
hid_t dsid_j;
H5O_info_t oi1, oi2;
H5I_type_t it1, it2;
int i;
size_t len;
int found_ds=0;
htri_t is_scale;
if ((is_scale = H5DSis_scale(did)) < 0)
return FAIL;
if (is_scale == 1)
return FAIL;
if(H5Oget_info2(did, &oi1, H5O_INFO_BASIC) < 0)
return FAIL;
if(H5Oget_info2(dsid, &oi2, H5O_INFO_BASIC) < 0)
return FAIL;
if(oi1.fileno == oi2.fileno && oi1.addr == oi2.addr)
return FAIL;
if ((it1 = H5Iget_type(did)) < 0)
return FAIL;
if ((it2 = H5Iget_type(dsid)) < 0)
return FAIL;
if (H5I_DATASET != it1 || H5I_DATASET != it2)
return FAIL;
if (H5LT_find_attribute(dsid,DIMENSION_LIST)==1)
return FAIL;
if (H5DS_is_reserved(did)==1)
return FAIL;
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank = H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if (rank == 0)
rank = 1;
if (H5Sclose(sid) < 0)
return FAIL;
if (idx > (unsigned)rank-1)
return FAIL;
if (H5Rcreate(&ref_to_ds, dsid, ".", H5R_OBJECT, (hid_t)-1) < 0)
return FAIL;
if (H5Rcreate(&dsl.ref, did, ".", H5R_OBJECT, (hid_t)-1) < 0)
return FAIL;
if ((has_dimlist = H5LT_find_attribute(did,DIMENSION_LIST)) < 0)
return FAIL;
if (has_dimlist == 0)
{
dims[0] = (hsize_t)rank;
if((sid = H5Screate_simple(1, dims, NULL)) < 0)
return FAIL;
if((tid = H5Tvlen_create(H5T_STD_REF_OBJ)) < 0)
goto out;
if((aid = H5Acreate2(did, DIMENSION_LIST, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
buf = (hvl_t *)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
for(i = 0; i < rank; i++) {
buf[i].len = 0;
buf[i].p = NULL;
}
buf[idx].len = 1;
buf[idx].p = HDmalloc( 1 * sizeof(hobj_ref_t));
((hobj_ref_t *)buf[idx].p)[0] = ref_to_ds;
if(H5Awrite(aid, tid, buf) < 0)
goto out;
if(H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
}
else if(has_dimlist == 1)
{
if((aid = H5Aopen(did, DIMENSION_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
buf = (hvl_t *)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
for(i = 0; i < (int)buf[idx].len; i++) {
ref_j = ((hobj_ref_t *)buf[idx].p)[i];
if((dsid_j = H5Rdereference2(did, H5P_DEFAULT, H5R_OBJECT, &ref_j)) < 0)
goto out;
if(H5Oget_info2(dsid, &oi1, H5O_INFO_BASIC) < 0)
goto out;
if(H5Oget_info2(dsid_j, &oi2, H5O_INFO_BASIC) < 0)
goto out;
if(oi1.fileno == oi2.fileno && oi1.addr == oi2.addr)
found_ds = 1;
if(H5Dclose(dsid_j) < 0)
goto out;
}
if(found_ds == 0) {
if(buf[idx].len > 0) {
buf[idx].len++;
len = buf[idx].len;
buf[idx].p = HDrealloc(buf[idx].p, len * sizeof(hobj_ref_t));
((hobj_ref_t *)buf[idx].p)[len - 1] = ref_to_ds;
}
else {
buf[idx].len = 1;
buf[idx].p = HDmalloc(sizeof(hobj_ref_t));
((hobj_ref_t *)buf[idx].p)[0] = ref_to_ds;
}
}
if(H5Awrite(aid, tid, buf) < 0)
goto out;
if(H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
}
if((has_reflist = H5LT_find_attribute(dsid, REFERENCE_LIST)) < 0)
goto out;
if(has_reflist == 0) {
dims[0] = 1;
if((sid = H5Screate_simple(1,dims,NULL)) < 0)
goto out;
if((tid = H5Tcreate(H5T_COMPOUND, sizeof(ds_list_t))) < 0)
goto out;
if(H5Tinsert(tid, "dataset", HOFFSET(ds_list_t,ref), H5T_STD_REF_OBJ) < 0)
goto out;
if(H5Tinsert(tid, "dimension", HOFFSET(ds_list_t, dim_idx), H5T_NATIVE_INT) < 0)
goto out;
if((aid = H5Acreate2(dsid, REFERENCE_LIST, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
dsl.dim_idx = idx;
if(H5Awrite(aid, tid, &dsl) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
}
else if(has_reflist == 1) {
if((aid = H5Aopen(dsid, REFERENCE_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((ntid = H5DS_get_REFLIST_type()) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
if((nelmts = H5Sget_simple_extent_npoints(sid)) < 0)
goto out;
nelmts++;
dsbuf = (ds_list_t*) HDmalloc((size_t)nelmts * sizeof(ds_list_t));
if(dsbuf == NULL)
goto out;
if(H5Aread(aid, ntid, dsbuf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
if(H5Adelete(dsid, REFERENCE_LIST) < 0)
goto out;
dsl.dim_idx = idx;
dsbuf[nelmts - 1] = dsl;
dims[0] = (hsize_t)nelmts;
if((sid = H5Screate_simple(1, dims, NULL)) < 0)
goto out;
if((aid = H5Acreate2(dsid, REFERENCE_LIST, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
if(H5Awrite(aid, ntid, dsbuf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
if (H5Tclose(ntid) < 0)
goto out;
HDfree(dsbuf);
dsbuf = NULL;
}
if((is_ds = H5DSis_scale(dsid)) < 0)
return FAIL;
if(is_ds == 0) {
if (H5LT_set_attribute_string(dsid,"CLASS",DIMENSION_SCALE_CLASS) < 0)
return FAIL;
}
return SUCCEED;
out:
if(buf)
HDfree(buf);
if(dsbuf)
HDfree(dsbuf);
H5E_BEGIN_TRY {
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(ntid);
H5Tclose(tid);
} H5E_END_TRY;
return FAIL;
}
herr_t
H5DSdetach_scale(hid_t did, hid_t dsid, unsigned int idx)
{
int has_dimlist;
int has_reflist;
hssize_t nelmts;
hid_t dsid_j;
hid_t did_i;
hid_t sid;
hid_t tid = -1;
hid_t ntid = -1;
hid_t aid = -1;
int rank;
ds_list_t *dsbuf = NULL;
hsize_t dims[1];
hobj_ref_t ref;
hvl_t *buf = NULL;
int i;
size_t j;
hssize_t ii;
H5O_info_t did_oi, dsid_oi, tmp_oi;
int found_dset = 0, found_ds = 0;
int have_ds = 0;
htri_t is_scale;
if(H5I_DATASET != H5Iget_type(did) || H5I_DATASET != H5Iget_type(dsid))
return FAIL;
if((is_scale = H5DSis_scale(did)) < 0)
return FAIL;
if( is_scale == 1)
return FAIL;
if(H5Oget_info2(did, &did_oi, H5O_INFO_BASIC) < 0)
return FAIL;
if(H5Oget_info2(dsid, &dsid_oi, H5O_INFO_BASIC) < 0)
return FAIL;
if(did_oi.fileno == dsid_oi.fileno && did_oi.addr == dsid_oi.addr)
return FAIL;
if ((has_dimlist = H5LT_find_attribute(did, DIMENSION_LIST)) < 0)
return FAIL;
if (has_dimlist == 0)
return FAIL;
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank = H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if (H5Sclose(sid) < 0)
return FAIL;
if (idx > (unsigned)rank-1)
return FAIL;
if((has_reflist = H5LT_find_attribute(dsid, REFERENCE_LIST)) < 0)
return FAIL;
if(has_reflist == 0)
return FAIL;
if((aid = H5Aopen(did, DIMENSION_LIST, H5P_DEFAULT)) < 0)
return FAIL;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
buf = (hvl_t *)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
if ( buf[idx].len > 0 )
{
for (j=0; j<buf[idx].len; j++)
{
ref = ((hobj_ref_t *)buf[idx].p)[j];
if ((dsid_j = H5Rdereference2(did,H5P_DEFAULT,H5R_OBJECT,&ref)) < 0)
goto out;
if(H5Oget_info2(dsid_j, &tmp_oi, H5O_INFO_BASIC) < 0)
goto out;
if (H5Dclose(dsid_j) < 0)
goto out;
if(dsid_oi.fileno == tmp_oi.fileno && dsid_oi.addr == tmp_oi.addr) {
size_t len = buf[idx].len;
if(j < len - 1)
((hobj_ref_t *)buf[idx].p)[j] = ((hobj_ref_t *)buf[idx].p)[len-1];
len = --buf[idx].len;
if(len == 0) {
HDfree(buf[idx].p);
buf[idx].p = NULL;
}
found_ds = 1;
break;
}
}
}
if(found_ds == 0)
goto out;
for(i = 0; i < rank; i++) {
if(buf[i].len > 0) {
have_ds = 1;
break;
}
}
if(have_ds) {
if(H5Awrite(aid, tid, buf) < 0)
goto out;
}
else {
if(H5Adelete(did, DIMENSION_LIST) < 0)
goto out;
}
if(H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
if((aid = H5Aopen(dsid, REFERENCE_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((ntid = H5DS_get_REFLIST_type()) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
if((nelmts = H5Sget_simple_extent_npoints(sid)) < 0)
goto out;
dsbuf = (ds_list_t*) HDmalloc((size_t)nelmts * sizeof(ds_list_t));
if(dsbuf == NULL)
goto out;
if(H5Aread(aid, ntid, dsbuf) < 0)
goto out;
for(ii = 0; ii < nelmts; ii++) {
if(idx == dsbuf[ii].dim_idx) {
ref = dsbuf[ii].ref;
if ((did_i = H5Rdereference2(did,H5P_DEFAULT,H5R_OBJECT,&ref)) < 0)
goto out;
if(H5Oget_info2(did_i, &tmp_oi, H5O_INFO_BASIC) < 0)
goto out;
if(H5Dclose(did_i) < 0)
goto out;
if(did_oi.fileno == tmp_oi.fileno && did_oi.addr == tmp_oi.addr) {
dsbuf[ii] = dsbuf[nelmts-1];
nelmts--;
found_dset=1;
break;
}
}
}
if (H5Sclose(sid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
if (found_dset == 0)
goto out;
if (H5Adelete(dsid, REFERENCE_LIST) < 0)
goto out;
if(nelmts)
{
dims[0] = (hsize_t)nelmts;
if((sid = H5Screate_simple(1, dims, NULL)) < 0)
goto out;
if((aid = H5Acreate2(dsid, REFERENCE_LIST, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
if(H5Awrite(aid, ntid, dsbuf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
}
if (H5Tclose(tid) < 0)
goto out;
if (H5Tclose(ntid) < 0)
goto out;
HDfree(dsbuf);
dsbuf = NULL;
return SUCCEED;
out:
H5E_BEGIN_TRY {
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(ntid);
H5Tclose(tid);
if(dsbuf) {
HDfree(dsbuf);
dsbuf = NULL;
}
if(buf) {
for(i = 0; i < rank; i++) {
if(buf[i].p)
HDfree(buf[i].p);
}
HDfree(buf);
buf = NULL;
}
} H5E_END_TRY;
return FAIL;
}
htri_t
H5DSis_attached(hid_t did, hid_t dsid, unsigned int idx)
{
int has_dimlist;
int has_reflist;
hssize_t nelmts;
hid_t sid;
hid_t tid = -1;
hid_t ntid = -1;
hid_t aid = -1;
int rank;
ds_list_t *dsbuf = NULL;
hobj_ref_t ref;
hvl_t *buf = NULL;
hid_t dsid_j;
hid_t did_i;
H5O_info_t oi1, oi2, oi3, oi4;
H5I_type_t it1, it2;
int i;
int found_dset=0, found_ds=0;
htri_t is_scale;
if ((is_scale = H5DSis_scale(did)) < 0)
return FAIL;
if ( is_scale == 1)
return FAIL;
if(H5Oget_info2(did, &oi1, H5O_INFO_BASIC) < 0)
return FAIL;
if(H5Oget_info2(dsid, &oi2, H5O_INFO_BASIC) < 0)
return FAIL;
if(oi1.fileno == oi2.fileno && oi1.addr == oi2.addr)
return FAIL;
if ((it1 = H5Iget_type(did)) < 0)
return FAIL;
if ((it2 = H5Iget_type(dsid)) < 0)
return FAIL;
if (H5I_DATASET != it1 || H5I_DATASET != it2)
return FAIL;
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank=H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if(idx > ((unsigned)rank - 1))
return FAIL;
if((has_dimlist = H5LT_find_attribute(did, DIMENSION_LIST)) < 0)
return FAIL;
if(has_dimlist == 1)
{
if((aid = H5Aopen(did, DIMENSION_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
buf = (hvl_t*)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
if (H5Aread(aid, tid, buf) < 0)
goto out;
for (i=0; i<(int)buf[idx].len; i++)
{
ref = ((hobj_ref_t *)buf[idx].p)[i];
if ((dsid_j = H5Rdereference2(did, H5P_DEFAULT, H5R_OBJECT, &ref)) < 0)
goto out;
if(H5Oget_info2(dsid, &oi1, H5O_INFO_BASIC) < 0)
goto out;
if(H5Oget_info2(dsid_j, &oi2, H5O_INFO_BASIC) < 0)
goto out;
if(oi1.fileno == oi2.fileno && oi1.addr == oi2.addr)
found_ds = 1;
if (H5Dclose(dsid_j) < 0)
goto out;
}
if (H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
}
if((has_reflist = H5LT_find_attribute(dsid, REFERENCE_LIST)) < 0)
goto out;
if(has_reflist == 1)
{
if((aid = H5Aopen(dsid, REFERENCE_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((ntid = H5DS_get_REFLIST_type()) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
if((nelmts = H5Sget_simple_extent_npoints(sid)) < 0)
goto out;
dsbuf = (ds_list_t*) HDmalloc((size_t)nelmts * sizeof(ds_list_t));
if (dsbuf == NULL)
goto out;
if (H5Aread(aid, ntid, dsbuf) < 0)
goto out;
for(i = 0; i < nelmts; i++)
{
ref = dsbuf[i].ref;
if (ref)
{
if ((did_i = H5Rdereference2(did,H5P_DEFAULT,H5R_OBJECT,&ref)) < 0)
goto out;
if(H5Oget_info2(did, &oi3, H5O_INFO_BASIC) < 0)
goto out;
if(H5Oget_info2(did_i, &oi4, H5O_INFO_BASIC) < 0)
goto out;
if(oi3.fileno == oi4.fileno && oi3.addr == oi4.addr && idx==dsbuf[i].dim_idx)
found_dset=1;
if (H5Dclose(did_i) < 0)
goto out;
}
}
if (H5Sclose(sid) < 0)
goto out;
if (H5Tclose(ntid) < 0)
goto out;
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
HDfree(dsbuf);
dsbuf = NULL;
}
if (found_ds && found_dset)
return 1;
else
return 0;
out:
H5E_BEGIN_TRY {
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(tid);
H5Tclose(ntid);
} H5E_END_TRY;
if (buf) {
HDfree(buf);
buf = NULL;
}
if(dsbuf) {
HDfree(dsbuf);
dsbuf = NULL;
}
return FAIL;
}
herr_t
H5DSiterate_scales(hid_t did, unsigned int dim, int *ds_idx,
H5DS_iterate_t visitor, void *visitor_data )
{
hid_t scale_id;
int rank;
hobj_ref_t ref;
hid_t sid;
hid_t tid = -1;
hid_t aid = -1;
hvl_t *buf = NULL;
H5I_type_t it;
herr_t ret_value=0;
int j_idx;
int nscales;
int has_dimlist;
int i;
if ((it = H5Iget_type(did)) < 0)
return FAIL;
if (H5I_DATASET!=it)
return FAIL;
if ((nscales = H5DSget_num_scales(did,dim)) < 0)
return FAIL;
if (ds_idx!=NULL)
{
if (*ds_idx>=nscales)
return FAIL;
}
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank=H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if ( dim >= (unsigned)rank )
return FAIL;
if((has_dimlist = H5LT_find_attribute(did, DIMENSION_LIST)) < 0)
return FAIL;
if(has_dimlist == 0)
return SUCCEED;
else if(has_dimlist == 1)
{
if((aid = H5Aopen(did, DIMENSION_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
buf = (hvl_t*)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
if ( buf[dim].len > 0 )
{
if (ds_idx!=NULL)
j_idx = *ds_idx;
else
j_idx=0;
for(i=j_idx; i<nscales; i++)
{
ref = ((hobj_ref_t *)buf[dim].p)[ i ];
H5E_BEGIN_TRY {
if ((scale_id = H5Rdereference2(did,H5P_DEFAULT,H5R_OBJECT,&ref)) < 0)
goto out;
} H5E_END_TRY;
if (ds_idx!=NULL)
{
*ds_idx = i;
}
if((ret_value=(visitor)(did,dim,scale_id,visitor_data))!=0)
{
if (H5Dclose(scale_id) < 0)
goto out;
break;
}
if (H5Dclose(scale_id) < 0)
goto out;
}
}
if (H5Dvlen_reclaim(tid,sid,H5P_DEFAULT,buf) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
}
return ret_value;
out:
H5E_BEGIN_TRY {
if(buf) {
H5Dvlen_reclaim(tid,sid,H5P_DEFAULT,buf);
HDfree(buf);
}
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(tid);
} H5E_END_TRY;
return FAIL;
}
herr_t
H5DSset_label(hid_t did, unsigned int idx, const char *label)
{
int has_labels;
hid_t sid = -1;
hid_t tid = -1;
hid_t aid = -1;
int rank;
hsize_t dims[1];
H5I_type_t it;
unsigned int i;
union {
char ** buf;
char const ** const_buf;
} u;
HDmemset(&u, 0, sizeof(u));
if ((it = H5Iget_type(did)) < 0)
return FAIL;
if (H5I_DATASET != it)
return FAIL;
if (label == NULL)
return FAIL;
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank = H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if ( idx >= (unsigned)rank )
return FAIL;
if ((has_labels = H5LT_find_attribute(did, DIMENSION_LABELS)) < 0)
return FAIL;
if (has_labels == 0)
{
dims[0] = (hsize_t)rank;
if ((sid = H5Screate_simple(1, dims, NULL)) < 0)
goto out;
if ((tid = H5Tcopy(H5T_C_S1)) < 0)
goto out;
if (H5Tset_size(tid, H5T_VARIABLE) < 0)
goto out;
if ((aid = H5Acreate2(did, DIMENSION_LABELS, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto out;
u.const_buf = (char const **) HDmalloc((size_t) rank * sizeof(char *));
if (u.const_buf == NULL)
goto out;
for (i = 0; i < (unsigned int) rank; i++)
u.const_buf[i] = NULL;
u.const_buf[idx] = label;
if (H5Awrite(aid, tid, u.const_buf) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
if (u.const_buf)
{
HDfree(u.const_buf);
u.const_buf = NULL;
}
}
else
{
if ((aid = H5Aopen(did, DIMENSION_LABELS, H5P_DEFAULT)) < 0)
goto out;
if ((tid = H5Aget_type(aid)) < 0)
goto out;
u.buf = (char **) HDmalloc((size_t) rank * sizeof(char *));
if (u.buf == NULL)
goto out;
if (H5Aread(aid, tid, (void *)u.buf) < 0)
goto out;
if (u.buf[idx])
HDfree(u.buf[idx]);
u.const_buf[idx] = label;
if (H5Awrite(aid, tid, u.buf) < 0)
goto out;
u.buf[idx] = NULL;
for (i = 0; i < (unsigned int) rank; i++)
{
if (u.buf[i])
HDfree(u.buf[i]);
}
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
if (u.buf)
{
HDfree(u.buf);
u.buf = NULL;
}
}
return SUCCEED;
out:
if (u.buf)
{
if (u.buf[idx])
u.buf[idx] = NULL;
for (i = 0; i < (unsigned int) rank; i++)
{
if (u.buf[i])
HDfree(u.buf[i]);
}
HDfree(u.buf);
}
H5E_BEGIN_TRY
{
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(tid);
}H5E_END_TRY;
return FAIL;
}
ssize_t
H5DSget_label(hid_t did, unsigned int idx, char *label, size_t size)
{
int has_labels;
hid_t sid = -1;
hid_t tid = -1;
hid_t aid = -1;
int rank;
char **buf = NULL;
H5I_type_t it;
size_t nbytes = 0;
size_t copy_len;
int i;
if ((it = H5Iget_type(did)) < 0)
return FAIL;
if (H5I_DATASET != it)
return FAIL;
if ((sid = H5Dget_space(did)) < 0)
return FAIL;
if ((rank = H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if ( idx >= (unsigned)rank )
return FAIL;
if ((has_labels = H5LT_find_attribute(did, DIMENSION_LABELS)) < 0)
return FAIL;
if (has_labels == 0)
{
if (label)
label[0] = 0;
return 0;
}
assert (has_labels == 1);
if ((aid = H5Aopen(did, DIMENSION_LABELS, H5P_DEFAULT)) < 0)
goto out;
if ((tid = H5Aget_type(aid)) < 0)
goto out;
buf = (char **) HDmalloc((size_t) rank * sizeof(char *));
if (buf == NULL)
goto out;
if (H5Aread(aid, tid, buf) < 0)
goto out;
if (buf[idx] != NULL)
{
nbytes = HDstrlen(buf[idx]);
copy_len = MIN(size-1, nbytes);
if (label)
{
HDmemcpy(label, buf[idx], copy_len);
label[copy_len] = '\0';
}
}
for (i = 0; i < rank; i++)
{
if (buf[i])
HDfree(buf[i]);
}
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
if (buf)
{
HDfree(buf);
buf = NULL;
}
return (ssize_t) nbytes;
out:
if (buf)
{
for (i = 0; i < rank; i++)
{
if (buf[i])
HDfree(buf[i]);
}
HDfree(buf);
}
H5E_BEGIN_TRY
{
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(tid);
}H5E_END_TRY;
return FAIL;
}
ssize_t
H5DSget_scale_name(hid_t did, char *name, size_t size)
{
hid_t aid;
hid_t tid = -1;
hid_t sid;
H5I_type_t it;
size_t nbytes;
size_t copy_len;
int has_name;
char *buf=NULL;
if ((it = H5Iget_type(did)) < 0)
return FAIL;
if (H5I_DATASET!=it)
return FAIL;
if ((H5DSis_scale(did))<=0)
return FAIL;
if ((has_name = H5LT_find_attribute(did, "NAME")) < 0)
return FAIL;
if (has_name == 0)
return 0;
if((aid = H5Aopen(did, "NAME", H5P_DEFAULT)) < 0)
return FAIL;
if((sid = H5Aget_space(aid)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((nbytes = H5Tget_size(tid)) == 0)
goto out;
buf = (char*)HDmalloc(nbytes * sizeof(char));
if (buf == NULL)
goto out;
if (H5Aread(aid,tid,buf) < 0)
goto out;
copy_len = MIN(size-1, nbytes);
if (name) {
HDmemcpy(name, buf, copy_len);
name[copy_len]='\0';
}
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
if (H5Sclose(sid) < 0)
goto out;
if (buf)
HDfree(buf);
return (ssize_t)(nbytes - 1);
out:
H5E_BEGIN_TRY {
H5Aclose(aid);
H5Tclose(tid);
H5Sclose(sid);
} H5E_END_TRY;
if (buf)
HDfree(buf);
return FAIL;
}
htri_t H5DSis_scale(hid_t did)
{
hid_t tid = -1;
hid_t aid = -1;
herr_t attr_class;
htri_t is_ds = -1;
H5I_type_t it;
char *buf = NULL;
size_t string_size;
H5T_class_t type_class;
H5T_str_t strpad;
if ((it = H5Iget_type(did)) < 0)
goto out;
if(H5I_DATASET != it)
goto out;
if((attr_class = H5LT_find_attribute(did, "CLASS")) < 0)
goto out;
if(attr_class == 0) {
is_ds = 0;
goto out;
}
else
{
if((aid = H5Aopen(did, "CLASS", H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((type_class = H5Tget_class(tid)) < 0)
goto out;
if(H5T_STRING != type_class) {
is_ds = 0;
goto out;
}
if((strpad = H5Tget_strpad(tid)) < 0 )
goto out;
if(H5T_STR_NULLTERM != strpad) {
is_ds = 0;
goto out;
}
if((string_size = H5Tget_size(tid)) == 0)
goto out;
if(string_size != 16) {
is_ds = 0;
goto out;
}
buf = (char*)HDmalloc((size_t)string_size * sizeof(char));
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
if(HDstrncmp(buf, DIMENSION_SCALE_CLASS,
MIN(HDstrlen(DIMENSION_SCALE_CLASS),HDstrlen(buf)))==0)
is_ds = 1;
HDfree(buf);
if(H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
}
out:
if(is_ds < 0) {
HDfree(buf);
H5E_BEGIN_TRY {
H5Aclose(aid);
H5Tclose(tid);
} H5E_END_TRY;
}
return is_ds;
}
int
H5DSget_num_scales(hid_t did, unsigned int idx)
{
int has_dimlist;
hid_t sid;
hid_t tid = -1;
hid_t aid = -1;
int rank;
hvl_t *buf = NULL;
H5I_type_t it;
int nscales;
if((it = H5Iget_type(did)) < 0)
return FAIL;
if(H5I_DATASET != it)
return FAIL;
if((sid = H5Dget_space(did)) < 0)
return FAIL;
if((rank = H5Sget_simple_extent_ndims(sid)) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(idx >= (unsigned int )rank)
return FAIL;
if((has_dimlist = H5LT_find_attribute(did, DIMENSION_LIST)) < 0)
return FAIL;
if(has_dimlist == 0)
return 0;
else {
if((aid = H5Aopen(did, DIMENSION_LIST, H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if((sid = H5Aget_space(aid)) < 0)
goto out;
buf = (hvl_t *)HDmalloc((size_t)rank * sizeof(hvl_t));
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
nscales = (int)buf[idx].len;
if(H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, buf) < 0)
goto out;
if(H5Sclose(sid) < 0)
goto out;
if(H5Tclose(tid) < 0)
goto out;
if(H5Aclose(aid) < 0)
goto out;
HDfree(buf);
buf = NULL;
}
return nscales;
out:
H5E_BEGIN_TRY {
H5Sclose(sid);
H5Aclose(aid);
H5Tclose(tid);
} H5E_END_TRY;
if(buf)
HDfree(buf);
return FAIL;
}
static herr_t
H5DS_is_reserved(hid_t did)
{
int has_class;
hid_t tid = -1;
hid_t aid = -1;
char *buf;
hsize_t storage_size;
herr_t ret;
if((has_class = H5LT_find_attribute(did, "CLASS")) < 0)
return -1;
if(has_class == 0)
return 0;
assert(has_class == 1);
if((aid = H5Aopen(did, "CLASS", H5P_DEFAULT)) < 0)
goto out;
if((tid = H5Aget_type(aid)) < 0)
goto out;
if(H5T_STRING != H5Tget_class(tid))
goto out;
if(H5T_STR_NULLTERM != H5Tget_strpad(tid))
goto out;
if((storage_size = H5Aget_storage_size(aid)) == 0)
goto out;
buf = (char*)HDmalloc( (size_t)storage_size * sizeof(char) + 1);
if(buf == NULL)
goto out;
if(H5Aread(aid, tid, buf) < 0)
goto out;
if(HDstrncmp(buf, IMAGE_CLASS, MIN(HDstrlen(IMAGE_CLASS),HDstrlen(buf))) == 0 ||
HDstrncmp(buf, PALETTE_CLASS, MIN(HDstrlen(PALETTE_CLASS),HDstrlen(buf))) == 0 ||
HDstrncmp(buf, TABLE_CLASS, MIN(HDstrlen(TABLE_CLASS),HDstrlen(buf))) == 0 )
ret = 1;
else
ret = 0;
HDfree(buf);
if (H5Tclose(tid) < 0)
goto out;
if (H5Aclose(aid) < 0)
goto out;
return ret;
out:
H5E_BEGIN_TRY
{
H5Tclose(tid);
H5Aclose(aid);
} H5E_END_TRY;
return FAIL;
}
static hid_t
H5DS_get_REFLIST_type(void)
{
hid_t ntid_t = -1;
if((ntid_t = H5Tcreate(H5T_COMPOUND, sizeof(ds_list_t))) < 0)
goto out;
if(H5Tinsert(ntid_t, "dataset", HOFFSET(ds_list_t,ref), H5T_STD_REF_OBJ) < 0)
goto out;
if(H5Tinsert(ntid_t, "dimension", HOFFSET(ds_list_t, dim_idx), H5T_NATIVE_INT) < 0)
goto out;
return ntid_t;
out:
H5E_BEGIN_TRY {
H5Tclose(ntid_t);
} H5E_END_TRY;
return FAIL;
}