//-------------------------------------------------------------------------- // Function: CompType::getMemberName ///\brief Returns the name of a member in this compound datatype. ///\param member_num - IN: Zero-based index of the member ///\return Name of member ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- H5std_string CompType::getMemberName( unsigned member_num ) const { char* member_name_C = H5Tget_member_name( id, member_num ); if( member_name_C == NULL ) // NULL means failure { throw DataTypeIException("CompType::getMemberName", "H5Tget_member_name returns NULL for member name"); } H5std_string member_name = H5std_string(member_name_C); // convert C string to string H5free_memory(member_name_C); // free the C string return( member_name ); // return the member name string }
herr_t H5AreadVL_str (JNIEnv *env, hid_t aid, hid_t tid, jobjectArray buf) { char **strs; jstring jstr; jint i; jint n; hid_t sid; hsize_t dims[H5S_MAX_RANK]; herr_t status = -1; n = ENVPTR->GetArrayLength(ENVPAR buf); strs =(char**)HDcalloc((size_t)n, sizeof(char*)); if (strs == NULL) { h5JNIFatalError(env, "H5AreadVL_str: failed to allocate buff for read variable length strings"); } /* end if */ else { status = H5Aread(aid, tid, strs); if (status < 0) { dims[0] = (hsize_t)n; sid = H5Screate_simple(1, dims, NULL); H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, strs); H5Sclose(sid); HDfree(strs); h5JNIFatalError(env, "H5AreadVL_str: failed to read variable length strings"); } /* end if */ else { for (i=0; i < n; i++) { jstr = ENVPTR->NewStringUTF(ENVPAR strs[i]); ENVPTR->SetObjectArrayElement(ENVPAR buf, i, jstr); H5free_memory (strs[i]); } /* end for */ /* for repeatedly reading a dataset with a large number of strs (e.g., 1,000,000 strings, H5Dvlen_reclaim() may crash on Windows because the Java GC will not be able to collect free space in time. Instead, use "H5free_memory(strs[i])" above to free individual strings after it is done. H5Dvlen_reclaim(tid, mem_sid, xfer_plist_id, strs); */ HDfree(strs); } /* end else */ } /* end else */ return status; } /* end H5AreadVL_str */
/* * test_opaque * Test comments on opaque datatypes */ void test_opaque(hid_t UNUSED fid, const char * string) { hid_t type_id; char * read_buf; herr_t ret; /* Create an opaque type and give it a UTF-8 tag */ type_id = H5Tcreate(H5T_OPAQUE, (size_t)4); CHECK(type_id, FAIL, "H5Tcreate"); ret = H5Tset_tag(type_id, string); CHECK(ret, FAIL, "H5Tset_tag"); /* Read the tag back. */ read_buf = H5Tget_tag(type_id); ret = strcmp(read_buf, string); VERIFY(ret, 0, "H5Tget_tag"); H5free_memory(read_buf); ret = H5Tclose(type_id); CHECK(ret, FAIL, "H5Tclose"); }
/* * test_compound * Test that compound datatypes can have UTF-8 field names. */ void test_compound(hid_t fid, const char * string) { /* Define two compound structures, s1_t and s2_t. * s2_t is a subset of s1_t, with two out of three * fields. * This is stolen from the h5_compound example. */ typedef struct s1_t { int a; double c; float b; } s1_t; typedef struct s2_t { double c; int a; } s2_t; /* Actual variable declarations */ s1_t s1; s2_t s2; hid_t s1_tid, s2_tid; hid_t space_id, dset_id; hsize_t dim = 1; char *readbuf; herr_t ret; /* Initialize compound data */ HDmemset(&s1, 0, sizeof(s1_t)); /* To make purify happy */ s1.a = COMP_INT_VAL; s1.c = COMP_DOUBLE_VAL; s1.b = COMP_FLOAT_VAL; /* Create compound datatypes using UTF-8 field name */ s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t)); CHECK(s1_tid, FAIL, "H5Tcreate"); ret = H5Tinsert(s1_tid, string, HOFFSET(s1_t, a), H5T_NATIVE_INT); CHECK(ret, FAIL, "H5Tinsert"); /* Check that the field name was stored correctly */ readbuf = H5Tget_member_name(s1_tid, 0); ret = HDstrcmp(readbuf, string); VERIFY(ret, 0, "strcmp"); H5free_memory(readbuf); /* Add the other fields to the datatype */ ret = H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE); CHECK(ret, FAIL, "H5Tinsert"); ret = H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); CHECK(ret, FAIL, "H5Tinsert"); /* Create second datatype, with only two fields. */ s2_tid = H5Tcreate (H5T_COMPOUND, sizeof(s2_t)); CHECK(s2_tid, FAIL, "H5Tcreate"); ret = H5Tinsert(s2_tid, "c_name", HOFFSET(s2_t, c), H5T_NATIVE_DOUBLE); CHECK(ret, FAIL, "H5Tinsert"); ret = H5Tinsert(s2_tid, string, HOFFSET(s2_t, a), H5T_NATIVE_INT); CHECK(ret, FAIL, "H5Tinsert"); /* Create the dataspace and dataset. */ space_id = H5Screate_simple(1, &dim, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(fid, DSET4_NAME, s1_tid, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset_id, FAIL, "H5Dcreate2"); /* Write data to the dataset. */ ret = H5Dwrite(dset_id, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s1); CHECK(ret, FAIL, "H5Dwrite"); /* Ensure that data can be read back by field name into s2 struct */ ret = H5Dread(dset_id, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s2); CHECK(ret, FAIL, "H5Dread"); VERIFY(s2.a, COMP_INT_VAL, "H5Dread"); VERIFY(s2.c, COMP_DOUBLE_VAL, "H5Dread"); /* Clean up */ ret = H5Tclose(s1_tid); CHECK(ret, FAIL, "H5Tclose"); ret = H5Tclose(s2_tid); CHECK(ret, FAIL, "H5Tclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); }
/**************************************************************** ** ** test_read_vl_string_attribute(): Test basic VL string code. ** Tests reading VL strings from attributes ** ****************************************************************/ static void test_read_vl_string_attribute(void) { hid_t file, root, att; hid_t type; herr_t ret; char *string_att_check = NULL; /* Open file */ file = H5Fopen(DATAFILE, H5F_ACC_RDONLY, H5P_DEFAULT); CHECK(file, FAIL, "H5Fopen"); /* Create a datatype to refer to. */ type = H5Tcopy (H5T_C_S1); CHECK(type, FAIL, "H5Tcopy"); ret = H5Tset_size (type, H5T_VARIABLE); CHECK(ret, FAIL, "H5Tset_size"); root = H5Gopen2(file, "/", H5P_DEFAULT); CHECK(root, FAIL, "H5Gopen2"); /* Test reading "normal" sized string attribute */ att = H5Aopen(root, "test_scalar", H5P_DEFAULT); CHECK(att, FAIL, "H5Aopen"); ret = H5Aread(att, type, &string_att_check); CHECK(ret, FAIL, "H5Aread"); if(HDstrcmp(string_att_check,string_att) != 0) TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",string_att,string_att_check); H5free_memory(string_att_check); string_att_check = NULL; ret = H5Aclose(att); CHECK(ret, FAIL, "HAclose"); /* Test reading "large" sized string attribute */ att = H5Aopen(root, "test_scalar_large", H5P_DEFAULT); CHECK(att, FAIL, "H5Aopen"); if(string_att_write) { ret = H5Aread(att, type, &string_att_check); CHECK(ret, FAIL, "H5Aread"); if(HDstrcmp(string_att_check,string_att_write) != 0) TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check); H5free_memory(string_att_check); string_att_check = NULL; } /* Free string allocated in test_write_vl_string_attribute */ if(string_att_write) HDfree(string_att_write); ret = H5Aclose(att); CHECK(ret, FAIL, "HAclose"); ret = H5Tclose(type); CHECK(ret, FAIL, "H5Tclose"); ret = H5Gclose(root); CHECK(ret, FAIL, "H5Gclose"); ret = H5Fclose(file); CHECK(ret, FAIL, "H5Fclose"); return; }
/**************************************************************** ** ** test_write_vl_string_attribute(): Test basic VL string code. ** Tests writing VL strings as attributes ** ****************************************************************/ static void test_write_vl_string_attribute(void) { hid_t file, root, dataspace, att; hid_t type; herr_t ret; char *string_att_check = NULL; /* Open the file */ file = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(file, FAIL, "H5Fopen"); /* Create a datatype to refer to. */ type = H5Tcopy (H5T_C_S1); CHECK(type, FAIL, "H5Tcopy"); ret = H5Tset_size (type, H5T_VARIABLE); CHECK(ret, FAIL, "H5Tset_size"); root = H5Gopen2(file, "/", H5P_DEFAULT); CHECK(root, FAIL, "H5Gopen2"); dataspace = H5Screate(H5S_SCALAR); CHECK(dataspace, FAIL, "H5Screate"); /* Test creating a "normal" sized string attribute */ att = H5Acreate2(root, "test_scalar", type, dataspace, H5P_DEFAULT, H5P_DEFAULT); CHECK(att, FAIL, "H5Acreate2"); ret = H5Awrite(att, type, &string_att); CHECK(ret, FAIL, "H5Awrite"); ret = H5Aread(att, type, &string_att_check); CHECK(ret, FAIL, "H5Aread"); if(HDstrcmp(string_att_check,string_att) != 0) TestErrPrintf("VL string attributes don't match!, string_att=%s, string_att_check=%s\n",string_att,string_att_check); H5free_memory(string_att_check); string_att_check = NULL; ret = H5Aclose(att); CHECK(ret, FAIL, "HAclose"); /* Test creating a "large" sized string attribute */ att = H5Acreate2(root, "test_scalar_large", type, dataspace, H5P_DEFAULT, H5P_DEFAULT); CHECK(att, FAIL, "H5Acreate2"); string_att_write = (char*)HDcalloc((size_t)8192, sizeof(char)); HDmemset(string_att_write, 'A', (size_t)8191); ret = H5Awrite(att, type, &string_att_write); CHECK(ret, FAIL, "H5Awrite"); ret = H5Aread(att, type, &string_att_check); CHECK(ret, FAIL, "H5Aread"); if(HDstrcmp(string_att_check,string_att_write) != 0) TestErrPrintf("VL string attributes don't match!, string_att_write=%s, string_att_check=%s\n",string_att_write,string_att_check); H5free_memory(string_att_check); string_att_check = NULL; /* The attribute string written is freed below, in the test_read_vl_string_attribute() test */ /* HDfree(string_att_write); */ ret = H5Aclose(att); CHECK(ret, FAIL, "HAclose"); ret = H5Gclose(root); CHECK(ret, FAIL, "H5Gclose"); ret = H5Tclose(type); CHECK(ret, FAIL, "H5Tclose"); ret = H5Sclose(dataspace); CHECK(ret, FAIL, "H5Sclose"); ret = H5Fclose(file); CHECK(ret, FAIL, "H5Fclose"); return; }