/* * 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"); }
//-------------------------------------------------------------------------- // Function: StrType::getCset ///\brief Retrieves the character set type of this string datatype. ///\return 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 //-------------------------------------------------------------------------- H5T_cset_t StrType::getCset() const { H5T_cset_t cset = H5Tget_cset( id ); // Returns a valid character set type if successful if( cset == H5T_CSET_ERROR ) { throw DataTypeIException("StrType::getCset", "H5Tget_cset failed"); } return( cset ); }
/*------------------------------------------------------------------------- * subroutine for test_text_dtype(): test_strings(). *------------------------------------------------------------------------- */ static int test_strings(void) { hid_t dtype; size_t str_size; H5T_str_t str_pad; H5T_cset_t str_cset; H5T_class_t type_class; char* dt_str; size_t str_len; TESTING3(" text for string types"); if((dtype = H5LTtext_to_dtype("H5T_STRING { STRSIZE 13; STRPAD H5T_STR_NULLTERM; CSET H5T_CSET_ASCII; CTYPE H5T_C_S1; }", H5LT_DDL))<0) goto out; if((type_class = H5Tget_class(dtype))<0) goto out; if(type_class != H5T_STRING) goto out; str_size = H5Tget_size(dtype); if(str_size != 13) goto out; str_pad = H5Tget_strpad(dtype); if(str_pad != H5T_STR_NULLTERM) goto out; str_cset = H5Tget_cset(dtype); if(str_cset != H5T_CSET_ASCII) goto out; if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0) goto out; dt_str = (char*)calloc(str_len, sizeof(char)); if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0) goto out; if(strcmp(dt_str, "H5T_STRING {\n STRSIZE 13;\n STRPAD H5T_STR_NULLTERM;\n CSET H5T_CSET_ASCII;\n CTYPE H5T_C_S1;\n }")) { printf("dt=\n%s\n", dt_str); goto out; } free(dt_str); if(H5Tclose(dtype)<0) goto out; if((dtype = H5LTtext_to_dtype("H5T_STRING { STRSIZE H5T_VARIABLE; STRPAD H5T_STR_NULLPAD; CSET H5T_CSET_ASCII; CTYPE H5T_C_S1; }", H5LT_DDL))<0) goto out; if(!H5Tis_variable_str(dtype)) goto out; str_pad = H5Tget_strpad(dtype); if(str_pad != H5T_STR_NULLPAD) goto out; str_cset = H5Tget_cset(dtype); if(str_cset != H5T_CSET_ASCII) goto out; if(H5LTdtype_to_text(dtype, NULL, H5LT_DDL, &str_len)<0) goto out; dt_str = (char*)calloc(str_len, sizeof(char)); if(H5LTdtype_to_text(dtype, dt_str, H5LT_DDL, &str_len)<0) goto out; if(strcmp(dt_str, "H5T_STRING {\n STRSIZE H5T_VARIABLE;\n STRPAD H5T_STR_NULLPAD;\n CSET H5T_CSET_ASCII;\n CTYPE H5T_C_S1;\n }")) { printf("dt=\n%s\n", dt_str); goto out; } free(dt_str); if(H5Tclose(dtype)<0) goto out; PASSED(); return 0; out: H5_FAILED(); return -1; }
Object::TypeName Object::getStlType(hid_t hType) const { H5T_class_t attrType = H5Tget_class(hType); // parsing type info stringstream typeName; switch(attrType) { case H5T_INTEGER: { size_t precision = H5Tget_precision(hType); H5T_sign_t sign = H5Tget_sign(hType); switch (sign) { case H5T_SGN_NONE: typeName << "u"; break; case H5T_SGN_2: break; case H5T_NSGN: break; case H5T_SGN_ERROR: break; } typeName << "int" << dec << precision << "_t"; } break; case H5T_FLOAT: { size_t precision = H5Tget_precision(hType); if (precision == 32) { typeName << "float"; } else if (precision == 64) { typeName << "double"; } else { stringstream ss; ss << "Object::getStlType: H5T_FLOAT with unkown precision " << precision << "bits"; throw Exception(ss); } } break; case H5T_STRING: { H5T_cset_t charSet = H5Tget_cset(hType); if (charSet != 0) { stringstream ss; ss << "Object::getStlType: Unknown character encoding type '" << charSet << "'. Currently only US-ASCII is supported."; throw Exception(ss); } typeName << "string"; } break; case H5T_BITFIELD: throw Exception("Object::getStlType: Unknown type H5T_BITFIELD"); break; case H5T_OPAQUE: throw Exception("Object::getStlType: Unknown type H5T_OPAQUE"); break; case H5T_COMPOUND: throw Exception("Object::getStlType: Unknown type H5T_COMPOUND"); break; case H5T_REFERENCE: throw Exception("Object::getStlType: Unknown type H5T_REFERENCE"); break; case H5T_ENUM: throw Exception("Object::getStlType: Unknown type H5T_ENUM"); break; case H5T_VLEN: throw Exception("Object::getStlType: Unknown type H5T_VLEN"); break; case H5T_ARRAY: throw Exception("Object::getStlType: Unknown type H5T_ARRAY"); break; case H5T_NO_CLASS: throw Exception("Object::getStlType: Unknown type H5T_NO_CLASS"); break; case H5T_TIME: throw Exception("Object::getStlType: Unknown type H5T_TIME"); break; case H5T_NCLASSES: throw Exception("Object::getStlType: Unknown type H5T_NCLASSES"); break; } return typeName.str(); }
/**************************************************************** ** ** test_vlstring_type(): Test VL string type. ** Tests if VL string is treated as string. ** ****************************************************************/ static void test_vlstring_type(void) { hid_t fid; /* HDF5 File IDs */ hid_t tid_vlstr; H5T_cset_t cset; H5T_str_t pad; htri_t vl_str; /* Whether string is VL */ herr_t ret; /* Output message about test being performed */ MESSAGE(5, ("Testing VL String type\n")); /* Open file */ fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fopen"); /* Create a datatype to refer to */ tid_vlstr = H5Tcopy(H5T_C_S1); CHECK(tid_vlstr, FAIL, "H5Tcopy"); /* Change padding and verify it */ ret = H5Tset_strpad(tid_vlstr, H5T_STR_NULLPAD); CHECK(ret, FAIL, "H5Tset_strpad"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Convert to variable-length string */ ret = H5Tset_size(tid_vlstr, H5T_VARIABLE); CHECK(ret, FAIL, "H5Tset_size"); /* Check if datatype is VL string */ ret = H5Tget_class(tid_vlstr); VERIFY(ret, H5T_STRING, "H5Tget_class"); ret = H5Tis_variable_str(tid_vlstr); VERIFY(ret, TRUE, "H5Tis_variable_str"); /* Verify that the class detects as a string */ vl_str = H5Tdetect_class(tid_vlstr, H5T_STRING); CHECK(vl_str, FAIL, "H5Tdetect_class"); VERIFY(vl_str, TRUE, "H5Tdetect_class"); /* Check default character set and padding */ cset = H5Tget_cset(tid_vlstr); VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Commit variable-length string datatype to storage */ ret = H5Tcommit2(fid, VLSTR_TYPE, tid_vlstr, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tcommit2"); /* Close datatype */ ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT); CHECK(tid_vlstr, FAIL, "H5Topen2"); ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fopen"); /* Open the variable-length string datatype just created */ tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT); CHECK(tid_vlstr, FAIL, "H5Topen2"); /* Verify character set and padding */ cset = H5Tget_cset(tid_vlstr); VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Close datatype and file */ ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); } /* end test_vlstring_type() */