/*------------------------------------------------------------------------- * Function: H5LD_get_dset_elmts * * Purpose: To retrieve selected data from the dataset * * Return: Success: 0 * Failure: negative * * Programmer: Vailin Choi; August 2010 * *------------------------------------------------------------------------- */ static herr_t H5LD_get_dset_elmts(hid_t did, const hsize_t *prev_dims, const hsize_t *cur_dims, const char *fields, void *buf) { hid_t dtid = -1, tid = -1; /* Dataset type id */ hid_t sid = -1, mid = -1; /* Dataspace and memory space id */ hssize_t snum_elmts; /* Number of dataset elements in the selection (signed) */ hsize_t num_elmts; /* Number of dataset elements in the selection */ hsize_t start[H5S_MAX_RANK];/* Starting offset */ hsize_t count[H5S_MAX_RANK];/* ??offset */ H5LD_memb_t **listv = NULL; /* Vector for storing information in "fields" */ char *dup_fields = NULL; /* A copy of "fields" */ char *sav_buf = NULL; /* Saved pointer temporary buffer */ unsigned ctr; /* Counter for # of curr_dims > prev_dims */ int ndims; /* Number of dimensions for the dataset */ int i; /* Local index variable */ herr_t ret_value = FAIL; /* Return value */ /* Verify parameters */ if(prev_dims == NULL || cur_dims == NULL || buf == NULL) goto done; /* Get dataset's dataspace */ if((sid = H5Dget_space(did)) < 0) goto done; /* Get the number of dimensions */ if((ndims = H5Sget_simple_extent_ndims(sid)) < 0) goto done; /* Verify that cur_dims must have one dimension whose size is greater than prev_dims */ HDmemset(start, 0, sizeof start); HDmemset(count, 0, sizeof count); ctr = 0; for(i = 0; i < ndims; i++) if(cur_dims[i] > prev_dims[i]) { ++ctr; count[i] = cur_dims[i] - prev_dims[i]; start[i] = prev_dims[i]; } /* end if */ else { /* < or = */ start[i] = 0; count[i] = MIN(prev_dims[i], cur_dims[i]); } /* end else */ if(!ctr) goto done; if(ctr == 1) { /* changes for only one dimension */ /* Make the selection in the dataset based on "cur_dims" and "prev_dims" */ if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) goto done; } /* end if */ else { /* changes for more than one dimensions */ HDmemset(start, 0, sizeof start); /* Make the selection in the dataset based on "cur_dims" and "prev_dims" */ if(H5Sselect_hyperslab(sid, H5S_SELECT_SET, start, NULL, cur_dims, NULL) < 0) goto done; if(H5Sselect_hyperslab(sid, H5S_SELECT_NOTB, start, NULL, prev_dims, NULL) < 0) goto done; } /* end else */ /* Get the number of elements in the selection */ if(0 == (snum_elmts = H5Sget_select_npoints(sid))) goto done; num_elmts = (hsize_t)snum_elmts; /* Create the memory space for the selection */ if((mid = H5Screate_simple(1, &num_elmts, NULL)) < 0) goto done; /* Get the native datatype size */ if((dtid = H5Dget_type(did)) < 0) goto done; if((tid = H5Tget_native_type(dtid, H5T_DIR_DEFAULT)) < 0) goto done; if(fields == NULL) { /* nothing in "fields" */ /* Read and store all the elements in "buf" */ if(H5Dread(did, tid, mid, sid, H5P_DEFAULT, buf) < 0) goto done; } /* end if */ else { /* "fields" is specified */ unsigned char *buf_p = (unsigned char *)buf; /* Pointer to the destination buffer */ char *tmp_buf; /* Temporary buffer for data read */ size_t tot_tsize; /* Total datatype size */ size_t len; /* Estimate the number of comma-separated fields in "fields" */ /* should be a compound datatype if "fields" exists */ if(H5Tget_class(tid) != H5T_COMPOUND) goto done; /* Get the total size of the dataset's datatypes */ if(0 == (tot_tsize = H5LD_get_dset_type_size(did, NULL))) goto done; /* Allocate memory for reading in the elements in the dataset selection */ if(NULL == (sav_buf = tmp_buf = (char *)HDcalloc((size_t)num_elmts, tot_tsize))) goto done; /* Read the dataset elements in the selection */ if(H5Dread(did, tid, mid, sid, H5P_DEFAULT, tmp_buf) < 0) goto done; /* Make a copy of "fields" */ if(NULL == (dup_fields = HDstrdup(fields))) goto done; /* Allocate memory for the vector of H5LD_memb_t pointers */ len = (HDstrlen(fields) / 2) + 2; if(NULL == (listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *)))) goto done; /* Process and store information for "fields" */ if(H5LD_construct_vector(dup_fields, listv, tid) < 0) goto done; /* Copy data for each dataset element in the selection */ for(i = 0; i < (int)num_elmts; i++) { int j; /* Local index variable */ /* Copy data for "fields" to the input buffer */ for(j = 0; listv[j] != NULL; j++) { HDmemcpy(buf_p, tmp_buf + listv[j]->tot_offset, listv[j]->last_tsize); buf_p += listv[j]->last_tsize; } /* end for */ tmp_buf += tot_tsize; } /* end for */ /* Clean up the vector of H5LD_memb_t structures */ H5LD_clean_vector(listv); } /* end else */ /* Indicate success */ ret_value = SUCCEED; done: H5E_BEGIN_TRY H5Tclose(dtid); H5Tclose(tid); H5Sclose(sid); H5Sclose(mid); H5E_END_TRY /* Free the array of H5LD_memb_t pointers */ if(listv) HDfree(listv); /* Free memory */ if(dup_fields) HDfree(dup_fields); if(sav_buf) HDfree(sav_buf); return(ret_value); } /* H5LD_get_dset_elmts() */
/*------------------------------------------------------------------------- * Function: process_cmpd_fields * * Purpose: To check whether the fields selected in "g_list_of_fields" * are valid fields associated with the dataset. * * Return: 0 on success; negative on failure * * Programmer: Vailin Choi; August 2010 * *------------------------------------------------------------------------- */ static herr_t process_cmpd_fields(hid_t fid, char *dsetname) { hid_t did=-1; /* dataset id */ hid_t dtid=-1, tid=-1; /* dataset's data type id */ size_t len; /* number of comma-separated fields in "g_list_of_fields" */ herr_t ret_value = SUCCEED; /* Return value */ HDassert(g_list_of_fields && *g_list_of_fields); /* Open the dataset */ if((did = H5Dopen2(fid, dsetname, H5P_DEFAULT)) < 0) { error_msg("error in opening dataset \"%s\"\n", dsetname); ret_value = FAIL; goto done; } /* Get the dataset's datatype */ if(((dtid = H5Dget_type(did)) < 0) || (tid = H5Tget_native_type(dtid, H5T_DIR_DEFAULT)) < 0) { error_msg("error in getting dataset's datatype\n"); ret_value = FAIL; goto done; } /* Check to make sure that the dataset's datatype is compound type */ if(H5Tget_class(dtid) != H5T_COMPOUND) { error_msg("dataset should be compound type for <list_of_fields>\n"); ret_value = FAIL; goto done; } /* Make a copy of "g_list_of_fields" */ if((g_dup_fields = HDstrdup(g_list_of_fields)) == NULL) { error_msg("error in duplicating g_list_of_fields\n"); ret_value = FAIL; goto done; } /* Estimate the number of comma-separated fields in "g_list of_fields" */ len = HDstrlen(g_list_of_fields)/2 + 2; /* Allocate memory for a list vector of H5LD_memb_t structures to store "g_list_of_fields" info */ if((g_listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *))) == NULL) { error_msg("error in allocating memory for H5LD_memb_t\n"); ret_value = FAIL; goto done; } /* Process and store info for "g_listv" */ if(H5LD_construct_vector(g_dup_fields, g_listv, tid) < 0) { error_msg("error in processing <list_of_fields>\n"); ret_value = FAIL; goto done; } /* Will free memory for g_listv and g_dup_fields when exiting from h5watch */ done: /* Closing */ H5E_BEGIN_TRY H5Tclose(dtid); H5Tclose(tid); H5Dclose(did); H5E_END_TRY return(ret_value); } /* process_cmpd_fields() */
/*------------------------------------------------------------------------- * Function: H5LD_get_dset_type_size * * Purpose: To return the size of the dataset's datatype in bytes * null "fields": return the size of the dataset's datatype * non-null "fields": return the size of the dataset's datatype * with respect to the selection in "fields" * * Return: Success: size of the dataset's datatype * Failure: 0 (valid datatypes are never zero size) * * Programmer: Vailin Choi; March 2010 * *------------------------------------------------------------------------- */ static size_t H5LD_get_dset_type_size(hid_t did, const char *fields) { hid_t dset_tid = -1; /* Dataset's type identifier */ hid_t tid = -1; /* Native Type identifier */ H5LD_memb_t **listv = NULL; /* Vector for storing information in "fields" */ char *dup_fields = NULL; /* A copy of "fields" */ size_t ret_value = 0; /* Return value */ /* Get the datatype of the dataset */ if((dset_tid = H5Dget_type(did)) < 0) goto done; if((tid = H5Tget_native_type(dset_tid, H5T_DIR_DEFAULT)) < 0) goto done; if(fields == NULL) /* If no "fields" is specified */ ret_value = H5Tget_size(tid); else { /* "fields" are specified */ size_t len; /* Estimate the number of comma-separated fields in "fields" */ size_t tot = 0; /* Data type size of all the fields in "fields" */ int n = 0, num = 0; /* Local index variables */ HDassert(fields && *fields); /* Should be a compound datatype if "fields" exists */ if(H5Tget_class(dset_tid) != H5T_COMPOUND) goto done; /* Get a copy of "fields" */ if(NULL == (dup_fields = HDstrdup(fields))) goto done; /* Allocate memory for a list of H5LD_memb_t pointers to store "fields" info */ len = (HDstrlen(fields) / 2) + 2; if(NULL == (listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *)))) goto done; /* Process and store info for "fields" */ if((num = H5LD_construct_vector(dup_fields, listv/*OUT*/, tid)) < 0) goto done; /* Sum up the size of all the datatypes in "fields" */ for(n = 0; n < num; n++) tot += listv[n]->last_tsize; /* Clean up the vector of H5LD_memb_t structures */ H5LD_clean_vector(listv); /* Return the total size */ ret_value = tot; } /* end else */ done: H5E_BEGIN_TRY H5Tclose(tid); H5Tclose(dset_tid); H5E_END_TRY /* Free the array of H5LD_memb_t pointers */ if(listv) HDfree(listv); /* Free memory */ if(dup_fields) HDfree(dup_fields); return(ret_value); } /* H5LD_get_dset_type_size() */