//-------------------------------------------------------------------------- // Function: StrType::getStrpad ///\brief Retrieves the storage mechanism for of this string datatype. ///\return String storage mechanism, which can be: /// \li \c H5T_STR_NULLTERM (0) - Null terminate (as C does) /// \li \c H5T_STR_NULLPAD (0) - Pad with zeros /// \li \c H5T_STR_SPACEPAD (0) - pad with spaces (as FORTRAN does) ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- H5T_str_t StrType::getStrpad() const { H5T_str_t strpad = H5Tget_strpad( id ); // Returns a valid string padding type if successful if( strpad == H5T_STR_ERROR ) { throw DataTypeIException("StrType::getStrpad", "H5Tget_strpad failed - returned H5T_STR_ERROR"); } return( strpad ); }
/*------------------------------------------------------------------------- * Function: h5tools_str_sprint * * Purpose: Renders the value pointed to by VP of type TYPE into variable * length string STR. * * Return: A pointer to memory containing the result or NULL on error. * * Programmer: Robb Matzke * Thursday, July 23, 1998 * * Modifications: * Robb Matzke, 1999-04-26 * Made this function safe from overflow problems by allowing it * to reallocate the output string. * * Robb Matzke, 1999-06-04 * Added support for object references. The new `container' * argument is the dataset where the reference came from. * * Robb Matzke, 1999-06-07 * Added support for printing raw data. If info->raw is non-zero * then data is printed in hexadecimal format. * * Robb Matzke, 2003-01-10 * Binary output format is dd:dd:... instead of 0xdddd... so it * doesn't look like a hexadecimal integer, and thus users will * be less likely to complain that HDF5 didn't properly byte * swap their data during type conversion. * * Robb Matzke, LLNL, 2003-06-05 * If TYPE is a variable length string then the pointer to * the value to pring (VP) is a pointer to a `char*'. * * PVN, 28 March 2006 * added H5T_NATIVE_LDOUBLE case *------------------------------------------------------------------------- */ char * h5tools_str_sprint(h5tools_str_t *str, const h5tool_format_t *info, hid_t container, hid_t type, void *vp, h5tools_context_t *ctx) { size_t n, offset, size=0, nelmts, start; char *name; unsigned char *ucp_vp = (unsigned char *)vp; char *cp_vp = (char *)vp; hid_t memb, obj, region; unsigned nmembs; static char fmt_llong[8], fmt_ullong[8]; H5T_str_t pad; /* * some tempvars to store the value before we append it to the string to * get rid of the memory alignment problem */ unsigned long long tempullong; long long templlong; unsigned long tempulong; long templong; unsigned int tempuint; int tempint; /* Build default formats for long long types */ if (!fmt_llong[0]) { sprintf(fmt_llong, "%%%sd", H5_PRINTF_LL_WIDTH); sprintf(fmt_ullong, "%%%su", H5_PRINTF_LL_WIDTH); } /* Append value depending on data type */ start = h5tools_str_len(str); if (info->raw) { size_t i; n = H5Tget_size(type); if (1 == n) { h5tools_str_append(str, OPT(info->fmt_raw, "0x%02x"), ucp_vp[0]); } else { for (i = 0; i < n; i++) { if (i) h5tools_str_append(str, ":"); h5tools_str_append(str, OPT(info->fmt_raw, "%02x"), ucp_vp[i]); } } } else if (H5Tequal(type, H5T_NATIVE_FLOAT)) { float tempfloat; HDmemcpy(&tempfloat, vp, sizeof(float)); h5tools_str_append(str, OPT(info->fmt_float, "%g"), tempfloat); } else if (H5Tequal(type, H5T_NATIVE_DOUBLE)) { double tempdouble; HDmemcpy(&tempdouble, vp, sizeof(double)); h5tools_str_append(str, OPT(info->fmt_double, "%g"), tempdouble); #if H5_SIZEOF_LONG_DOUBLE !=0 } else if (H5Tequal(type, H5T_NATIVE_LDOUBLE)) { long double templdouble; HDmemcpy(&templdouble, vp, sizeof(long double)); h5tools_str_append(str, "%Lf", templdouble); #endif } else if (info->ascii && (H5Tequal(type, H5T_NATIVE_SCHAR) || H5Tequal(type, H5T_NATIVE_UCHAR))) { h5tools_print_char(str, info, (char) (*ucp_vp)); } else if (H5T_STRING == H5Tget_class(type)) { unsigned int i; char quote = '\0'; char *s; quote = '\0'; if (H5Tis_variable_str(type)) { /* cp_vp is the pointer into the struct where a `char*' is stored. So we have * to dereference the pointer to get the `char*' to pass to HDstrlen(). */ s = *(char**) cp_vp; if (s != NULL) size = HDstrlen(s); } else { s = cp_vp; size = H5Tget_size(type); } pad = H5Tget_strpad(type); /* Check for NULL pointer for string */ if (s == NULL) { h5tools_str_append(str, "NULL"); } else { for (i = 0; i < size && (s[i] || pad != H5T_STR_NULLTERM); i++) { int j = 1; /* * Count how many times the next character repeats. If the * threshold is zero then that means it can repeat any number * of times. */ if (info->str_repeat > 0) while (i + j < size && s[i] == s[i + j]) j++; /* * Print the opening quote. If the repeat count is high enough to * warrant printing the number of repeats instead of enumerating * the characters, then make sure the character to be repeated is * in it's own quote. */ if (info->str_repeat > 0 && j > info->str_repeat) { if (quote) h5tools_str_append(str, "%c", quote); quote = '\''; h5tools_str_append(str, "%s%c", i ? " " : "", quote); } else if (!quote) { quote = '"'; h5tools_str_append(str, "%s%c", i ? " " : "", quote); } /* Print the character */ h5tools_print_char(str, info, s[i]); /* Print the repeat count */ if (info->str_repeat && j > info->str_repeat) { #ifdef REPEAT_VERBOSE h5tools_str_append(str, "%c repeats %d times", quote, j - 1); #else h5tools_str_append(str, "%c*%d", quote, j - 1); #endif /* REPEAT_VERBOSE */ quote = '\0'; i += j - 1; } } if (quote) h5tools_str_append(str, "%c", quote); if (i == 0) /*empty string*/ h5tools_str_append(str, "\"\""); } /* end else */ } else if (H5Tequal(type, H5T_NATIVE_INT)) { HDmemcpy(&tempint, vp, sizeof(int)); #ifdef H5_HAVE_H5DUMP_PACKED_BITS if(packed_bits_num) tempint = (tempint >> packed_data_offset) & packed_data_mask; #endif h5tools_str_append(str, OPT(info->fmt_int, "%d"), tempint); } else if (H5Tequal(type, H5T_NATIVE_UINT)) {
/*------------------------------------------------------------------------- * 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; }
herr_t H5IMis_palette( hid_t loc_id, const char *dset_name ) { hid_t did; int has_class; hid_t atid; hid_t aid = -1; char* attr_data; /* Name of attribute */ hsize_t storage_size; /* Size of storage for attribute */ herr_t ret; /* check the arguments */ if (dset_name == NULL) return -1; /* Assume initially fail condition */ ret = -1; /* Open the dataset. */ if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "CLASS" on the dataset */ has_class = H5LT_find_attribute(did, "CLASS"); if(has_class == 0) { H5Dclose( did ); return 0; } else if(has_class == 1) { if((aid = H5Aopen(did, "CLASS", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; /* check to make sure attribute is a string */ if(H5T_STRING != H5Tget_class(atid)) goto out; /* check to make sure string is null-terminated */ if(H5T_STR_NULLTERM != H5Tget_strpad(atid)) goto out; /* allocate buffer large enough to hold string */ if((storage_size = H5Aget_storage_size(aid)) == 0) goto out; attr_data = (char*)HDmalloc( (size_t)storage_size * sizeof(char) + 1); if(attr_data == NULL) goto out; if(H5Aread(aid, atid, attr_data) < 0) goto out; if(HDstrncmp(attr_data, PALETTE_CLASS, MIN(HDstrlen(PALETTE_CLASS),HDstrlen(attr_data))) == 0) ret = 1; else ret = 0; HDfree(attr_data); if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; } /* Close the dataset. */ if ( H5Dclose( did ) < 0) return -1; return ret; out: H5Dclose( did ); return -1; }
/**************************************************************** ** ** 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() */