//-------------------------------------------------------------------------- // Function: StrType::setCset ///\brief Sets character set to be used. ///\param cset - IN: character set type, which can be: /// \li \c H5T_CSET_ASCII (0) - Character set is US ASCII. ///\note /// ASCII and UTF-8 Unicode are the only currently supported character /// encodings. Extended ASCII encodings (for example, ISO 8859) are not /// supported. This encoding policy is not enforced by the HDF5 Library. /// Using encodings other than ASCII and UTF-8 can lead to compatibility /// and usability problems. See the C API entry H5Pset_char_encoding for /// more information. ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void StrType::setCset( H5T_cset_t cset ) const { herr_t ret_value = H5Tset_cset( id, cset ); if( ret_value < 0 ) { throw DataTypeIException("StrType::setCset", "H5Tset_cset failed"); } }
void hdf5_datatype::set_encoding(H5T_cset_t encoding) { herr_t status = H5Tset_cset(get_id(), encoding); if(status < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_datatype_set_charset_error ) ); } }
/* * test_fl_string * Tests that UTF-8 can be used for fixed-length string data. * Writes the string to a dataset and reads it back again. */ void test_fl_string(hid_t fid, const char *string) { hid_t dtype_id, space_id, dset_id; hsize_t dims = 1; char read_buf[MAX_STRING_LENGTH]; H5T_cset_t cset; herr_t ret; /* Create the datatype, ensure that the character set behaves * correctly (it should default to ASCII and can be set to UTF8) */ dtype_id = H5Tcopy(H5T_C_S1); CHECK(dtype_id, FAIL, "H5Tcopy"); ret = H5Tset_size(dtype_id, (size_t)MAX_STRING_LENGTH); CHECK(ret, FAIL, "H5Tset_size"); cset = H5Tget_cset(dtype_id); VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset"); ret = H5Tset_cset(dtype_id, H5T_CSET_UTF8); CHECK(ret, FAIL, "H5Tset_cset"); cset = H5Tget_cset(dtype_id); VERIFY(cset, H5T_CSET_UTF8, "H5Tget_cset"); /* Create dataspace for a dataset */ space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); /* Create a dataset */ dset_id = H5Dcreate2(fid, DSET1_NAME, dtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset_id, FAIL, "H5Dcreate2"); /* Write UTF-8 string to dataset */ ret = H5Dwrite(dset_id, dtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, string); CHECK(ret, FAIL, "H5Dwrite"); /* Read string back and make sure it is unchanged */ ret = H5Dread(dset_id, dtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, read_buf); CHECK(ret, FAIL, "H5Dread"); VERIFY(HDstrcmp(string, read_buf), 0, "strcmp"); /* Close all */ ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Tclose(dtype_id); CHECK(ret, FAIL, "H5Tclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); }
int NC4_put_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; H5T_class_t t_class; size_t size; hid_t grp = -1; hid_t exists = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; herr_t herr = 0; char* text = NULL; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* See if the NCPROPS attribute exists */ if(H5Aexists(grp,NCPROPS) == 0) { /* Does not exist */ ncstat = NC4_buildpropinfo(&h5->fileinfo->propattr,&text); if(text == NULL || ncstat != NC_NOERR) { goto done; } herr = -1; /* Create a datatype to refer to. */ HCHECK((atype = H5Tcopy(H5T_C_S1))); HCHECK((H5Tset_cset(atype, H5T_CSET_ASCII))); HCHECK((H5Tset_size(atype, strlen(text)+1))); /*keep nul term */ HCHECK((aspace = H5Screate(H5S_SCALAR))); HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT))); HCHECK((H5Awrite(attid, atype, text))); herr = 0; } done: if(ncstat != NC_NOERR) { if(text != NULL) { free(text); text = NULL; } } if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(atype >= 0) HCHECK((H5Tclose(atype))); return ncstat; }
int NC4_put_propattr(NC_HDF5_FILE_INFO_T* h5) { int ncstat = NC_NOERR; char text[NCPROPS_LENGTH+1]; H5T_class_t t_class; size_t size; hid_t grp = -1; hid_t exists = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; herr_t herr = 0; /* Get root group */ grp = h5->root_grp->hdf_grpid; /* get root group */ /* See if the NCPROPS attribute exists */ exists = H5Aopen_name(grp, NCPROPS); if(exists < 0) {/* Does not exist */ herr = -1; /* Create a datatype to refer to. */ HCHECK((atype = H5Tcopy(H5T_C_S1))); HCHECK((H5Tset_cset(atype, H5T_CSET_UTF8))); HCHECK((H5Tset_size(atype, NCPROPS_LENGTH))); HCHECK((aspace = H5Screate(H5S_SCALAR))); HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT))); HCHECK((H5Awrite(attid, atype, h5->fileinfo->propattr.text))); herr = 0; } done: if(exists >= 0) HCHECK((H5Aclose(exists))); if(attid >= 0) HCHECK((H5Aclose(attid))); if(aspace >= 0) HCHECK((H5Sclose(aspace))); if(atype >= 0) HCHECK((H5Tclose(atype))); return ncstat; }
int hdf5read(char *name, struct descriptor_xd *xd) { hid_t obj,type; H5G_stat_t statbuf; int item_type; int idx = 0; int status = FindItem(name, &obj, &item_type); if (status & 1) { if (item_type == H5G_DATASET) { int size; char dtype; int htype = 42; int is_signed; hsize_t ds_dims[64]; hid_t space = H5Dget_space(obj); int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0); size_t precision; H5Sclose(space); type = H5Dget_type(obj); switch (H5Tget_class(type)) { case H5T_COMPOUND: { printf("Compound data is not supported, skipping\n"); break; } case H5T_INTEGER: precision = H5Tget_precision(type); is_signed = (H5Tget_sign(type) != H5T_SGN_NONE); size = precision/8; switch (precision) { case 8: dtype = is_signed ? DTYPE_B : DTYPE_BU; htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR; break; case 16: dtype = is_signed ? DTYPE_W : DTYPE_WU; htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT; break; case 32: dtype = is_signed ? DTYPE_L : DTYPE_LU; htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT; break; case 64: dtype = is_signed ? DTYPE_Q : DTYPE_QU; htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG; break; default: dtype = 0; break; } PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd); break; case H5T_FLOAT: precision = H5Tget_precision(type); size = precision/8; switch (precision) { case 32: dtype = DTYPE_NATIVE_FLOAT; htype = H5T_NATIVE_FLOAT; break; case 64: dtype = DTYPE_NATIVE_DOUBLE; htype = H5T_NATIVE_DOUBLE; break; default: dtype = 0; break; } PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,0,xd); break; case H5T_TIME: printf("dataset is time ---- UNSUPPORTED\n"); break; case H5T_STRING: { int slen = H5Tget_size(type); hid_t st_id; if (slen < 0) { printf("Badly formed string attribute\n"); return; } #if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1 if(H5Tis_variable_str(type)) { st_id = H5Tcopy (H5T_C_S1); H5Tset_size(st_id, H5T_VARIABLE); } else { #endif st_id = H5Tcopy (type); H5Tset_cset(st_id, H5T_CSET_ASCII); #if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1 } #endif if (H5Tget_size(st_id) > slen) { slen = H5Tget_size(st_id); } H5Tset_size (st_id, slen); PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 0, xd); } /* printf("dataset is string\n"); */ /* dtype = DTYPE_T; */ /* htype = H5T_STRING; */ /* PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */ break; case H5T_BITFIELD: printf("dataset is bitfield ---- UNSUPPORTED\n"); break; case H5T_OPAQUE: printf("dataset is opaque ---- UNSUPPORTED\n"); break; case H5T_ARRAY: printf("dataset is array ---- UNSUPPORTED\n"); break; case H5T_VLEN: printf("dataset is vlen ---- UNSUPPORTED\n"); break; } H5Tclose(type); } else { int size; char dtype; int htype = 42; int is_signed; hsize_t ds_dims[64]; hid_t space = H5Aget_space(obj); int n_ds_dims = H5Sget_simple_extent_dims(space,ds_dims,0); size_t precision; H5Sclose(space); type = H5Aget_type(obj); switch (H5Tget_class(type)) { case H5T_COMPOUND: { printf("Compound data is not supported, skipping\n"); break; } case H5T_INTEGER: precision = H5Tget_precision(type); is_signed = (H5Tget_sign(type) != H5T_SGN_NONE); size = precision/8; switch (precision) { case 8: dtype = is_signed ? DTYPE_B : DTYPE_BU; htype = is_signed ? H5T_NATIVE_CHAR : H5T_NATIVE_UCHAR; break; case 16: dtype = is_signed ? DTYPE_W : DTYPE_WU; htype = is_signed ? H5T_NATIVE_SHORT : H5T_NATIVE_USHORT; break; case 32: dtype = is_signed ? DTYPE_L : DTYPE_LU; htype = is_signed ? H5T_NATIVE_INT : H5T_NATIVE_UINT; break; case 64: dtype = is_signed ? DTYPE_Q : DTYPE_QU; htype = is_signed ? H5T_NATIVE_LLONG : H5T_NATIVE_ULLONG; break; default: dtype = 0; break; } PutData(obj, dtype, htype, size, n_ds_dims, ds_dims, 1, xd); break; case H5T_FLOAT: precision = H5Tget_precision(type); size = precision/8; switch (precision) { case 32: dtype = DTYPE_NATIVE_FLOAT; htype = H5T_NATIVE_FLOAT; break; case 64: dtype = DTYPE_NATIVE_DOUBLE; htype = H5T_NATIVE_DOUBLE; break; default: dtype = 0; break; } PutData(obj, dtype, htype, size, n_ds_dims, ds_dims,1, xd); break; case H5T_TIME: printf("dataset is time ---- UNSUPPORTED\n"); break; case H5T_STRING: { int slen = H5Tget_size(type); hid_t st_id; if (slen < 0) { printf("Badly formed string attribute\n"); return; } #if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1 if(H5Tis_variable_str(type)) { st_id = H5Tcopy (H5T_C_S1); H5Tset_size(st_id, H5T_VARIABLE); } else { #endif st_id = H5Tcopy (type); H5Tset_cset(st_id, H5T_CSET_ASCII); #if H5_VERS_MAJOR>=1&&H5_VERS_MINOR>=6&&H5_VERS_RELEASE>=1 } #endif if (H5Tget_size(st_id) > slen) { slen = H5Tget_size(st_id); } H5Tset_size (st_id, slen); PutData(obj, DTYPE_T, st_id, slen, n_ds_dims, ds_dims, 1, xd); } /* dtype = DTYPE_T; */ /* htype = H5T_STRING; */ /* PutData(obj, dtype, htype, 0, 0, 0, 1, xd); */ break; case H5T_BITFIELD: printf("dataset is bitfield ---- UNSUPPORTED\n"); break; case H5T_OPAQUE: printf("dataset is opaque ---- UNSUPPORTED\n"); break; case H5T_ARRAY: printf("dataset is array ---- UNSUPPORTED\n"); break; case H5T_VLEN: printf("dataset is vlen ---- UNSUPPORTED\n"); break; } H5Tclose(type); } } return status; }
int NC4_write_ncproperties(NC_FILE_INFO_T* h5) { int retval = NC_NOERR; hid_t hdf5grpid = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; char* text = NULL; size_t len = 0; LOG((3, "%s", __func__)); /* If the file is read-only, return an error. */ if (h5->no_write) {retval = NC_EPERM; goto done;} hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid; if(H5Aexists(hdf5grpid,NCPROPS) > 0) /* Already exists, no overwrite */ goto done; /* Build the attribute string */ if((retval = NC4_buildpropinfo(&h5->provenance->propattr,&text))) goto done; /* Build the HDF5 string type */ if ((atype = H5Tcopy(H5T_C_S1)) < 0) {retval = NC_EHDFERR; goto done;} if (H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) {retval = NC_EHDFERR; goto done;} if(H5Tset_cset(atype, H5T_CSET_ASCII) < 0) {retval = NC_EHDFERR; goto done;} len = strlen(text); if(H5Tset_size(atype, len) < 0) {retval = NC_EFILEMETA; goto done;} /* Create NCPROPS attribute */ if((aspace = H5Screate(H5S_SCALAR)) < 0) {retval = NC_EFILEMETA; goto done;} if ((attid = H5Acreate(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0) {retval = NC_EFILEMETA; goto done;} if (H5Awrite(attid, atype, text) < 0) {retval = NC_EFILEMETA; goto done;} /* Verify */ #if 0 { hid_t spacev, typev; hsize_t dsize, tsize; typev = H5Aget_type(attid); spacev = H5Aget_space(attid); dsize = H5Aget_storage_size(attid); tsize = H5Tget_size(typev); fprintf(stderr,"dsize=%lu tsize=%lu\n",(unsigned long)dsize,(unsigned long)tsize); } #endif done: if(text != NULL) free(text); /* Close out the HDF5 objects */ if(attid > 0 && H5Aclose(attid) < 0) retval = NC_EHDFERR; if(aspace > 0 && H5Sclose(aspace) < 0) retval = NC_EHDFERR; if(atype > 0 && H5Tclose(atype) < 0) retval = NC_EHDFERR; /* For certain errors, actually fail, else log that attribute was invalid and ignore */ switch (retval) { case NC_ENOMEM: case NC_EHDFERR: case NC_EPERM: case NC_EFILEMETA: case NC_NOERR: break; default: LOG((0,"Invalid _NCProperties attribute")); retval = NC_NOERR; break; } return retval; }