static int read_attribute_sub(int32 tag, int32 attr_id, int32 attr_index, int32 field_index, int32 length, void *buffer) { int result; result = 0; switch (tag) { case DFTAG_RI: /* GRImage attribute */ result = GRgetattr(attr_id, attr_index, buffer); break; case DFTAG_SD: /* SDS attribute */ result = SDreadattr(attr_id, attr_index, buffer); break; case DFTAG_VS: /* Vdata attribute */ result = VSgetattr(attr_id, field_index, attr_index, buffer); break; case DFTAG_VG: /* Vgroup attribute */ result = Vgetattr(attr_id, attr_index, buffer); break; case DFTAG_DIL: /* data label annotation */ case DFTAG_FID: /* file label annotation */ /* labels receive a terminating zero from the HDF4 lib, so we need to read it using one byte extra */ /* the buffer should have already been made large enough for this by coda_hdf4_cursor_read_string */ result = ANreadann(attr_id, buffer, length + 1); break; case DFTAG_DIA: /* data description annotation */ case DFTAG_FD: /* file description annotation */ result = ANreadann(attr_id, buffer, length); break; default: assert(0); exit(1); } if (result == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } return 0; }
/* * dumps attributes of vdata for vgroup */ intn dumpattr(int32 vid, int32 findex, intn isvs, file_format_t ff, FILE *fp) { intn i, k; intn cn = 0; intn nattrs; intn alloc_flag = 0; int32 i_type; int32 i_count; int32 i_size, e_size; int32 off; uint8 *buf = NULL; uint8 *ptr = NULL; intn (*vfmtfn)(VOIDP, file_format_t ff, FILE *); intn status; intn ret_value = SUCCEED; char name[FIELDNAMELENMAX+1]; uint8 attrbuf[BUFFER]; /* vdata or vgroup? */ if (isvs) nattrs = VSfnattrs(vid, findex); else /* nattrs = Vnattrs(vid); <- replaced with Vnattrs2 to catch all attributes; previously, SD attributes were missed by V API */ nattrs = Vnattrs2(vid); if (FAIL == nattrs) { fprintf(stderr,">>>dumpattr: Failed to get number of attributes for vid %d \n",(int)vid); ret_value = FAIL; goto done; } fprintf(fp, " number of attributes = %d \n", nattrs); /* loop for number of attributes to process */ for (i = 0; i < nattrs; i++) { /* get attribute infor of vdata/vgroup */ if (isvs) status = VSattrinfo(vid, findex, i, name, &i_type, &i_count, &e_size); else /* Changed to use updated func of Vattrinfo - BMR, 1/7/2013 */ status = Vattrinfo2(vid, i, name, &i_type,&i_count, &e_size, NULL, NULL); if (status == FAIL) { fprintf(stderr,">>>dumpattr: failed in getting %d'th attr info.\n",i); ret_value = FAIL; goto done; } /* get attribute hdfsize of vdata/vgroup */ if (isvs) status = VSattrhdfsize(vid, findex, i, &i_size); else status = Vattrhdfsize(vid, i, &i_size); if (status == FAIL) { fprintf(stderr,">>>dumpattr: failed in getting %d'th attr hdfsize.\n",i); ret_value = FAIL; goto done; } fprintf(fp," attr%d: name=%s type=%d count=%d size=%d\n", i, name, (int)i_type, (int)i_count, (int)i_size); /* we have two buffer sizes? */ if (e_size > BUFFER) { if (NULL == (buf = HDmalloc(e_size))) { fprintf(stderr,">>>dumpattr:can't allocate buf for %d'th attribute.\n",i); ret_value = FAIL; goto done; /* do we want exit here? */ } alloc_flag = 1; /* get attribute itself */ if (isvs) status = VSgetattr(vid, findex, i, (VOIDP)buf); else /* Changed to use updated func of Vgetattr - BMR, 1/7/2013 */ status = Vgetattr2(vid, i, (VOIDP)buf); if (status == FAIL) { fprintf(stderr,">>>dympattr: failed in getting %d'th attr .\n",i); ret_value = FAIL; goto done; } } else { /* get attribute itself */ if (isvs) status = VSgetattr(vid, findex, i, (VOIDP)attrbuf); else /* Changed to use updated func of Vgetattr - BMR, 1/7/2013 */ status = Vgetattr2(vid, i, (VOIDP)attrbuf); if (status == FAIL) { fprintf(stderr,">>>dympattr: failed in getting %d'th attr.\n",i); ret_value = FAIL; goto done; } } /* format output */ switch (i_type & 0xff) { case DFNT_CHAR: case DFNT_UCHAR: vfmtfn = fmtchar; break; case DFNT_UINT8: vfmtfn = fmtuint8; break; case DFNT_INT8: vfmtfn = fmtint8; break; case DFNT_UINT16: vfmtfn = fmtuint16; break; case DFNT_INT16: vfmtfn = fmtint16; break; case DFNT_UINT32: vfmtfn = fmtuint32; break; case DFNT_INT32: vfmtfn = fmtint32; break; case DFNT_FLOAT32: vfmtfn = fmtfloat32; break; case DFNT_FLOAT64: vfmtfn = fmtfloat64; break; default: fprintf(stderr,">>>dumpattr: sorry, type [%d] not supported\n", (int) i_type); ret_value = FAIL; goto done; } /* find offset */ off = DFKNTsize(i_type | DFNT_NATIVE); /* which buffer are we using? */ ptr = (alloc_flag) ? buf : attrbuf; putchar('\t'); cn = 0; for (k = 0; k < i_count; k++) { cn += vfmtfn((uint8 *)ptr, ff, fp); ptr += off; putchar(' '); cn++; if (cn > 55) { putchar('\n'); putchar('\t'); cn = 0; } } if (cn) putchar('\n'); /* free allocated space if any */ if (alloc_flag) { if ( buf != NULL) HDfree(buf); alloc_flag = 0; buf = NULL; } } /* for i */ done: if (ret_value == FAIL) { /* Failure cleanup */ if (buf != NULL) HDfree(buf); } /* Normal cleanup */ return ret_value; }
int main( ) { /************************* Variable declaration **************************/ intn status_n; /* returned status for functions returning an intn */ int32 status_32, /* returned status for functions returning an int32 */ file_id, vdata_ref, vdata_id, field_index, /* index of a field within the vdata */ n_vdattrs, /* number of vdata attributes */ n_fldattrs, /* number of field attributes */ vdata_type, /* to hold the type of vdata's attribute */ vdata_n_values,/* to hold the number of vdata's attribute values */ vdata_size, /* to hold the size of vdata's attribute values */ field_type, /* to hold the type of field's attribute */ field_n_values,/* to hold the number of field's attribute values */ field_size; /* to hold the size of field's attribute values */ char vd_attr[VATTR_N_VALUES] = {'A', 'B', 'C'};/* vdata attribute values*/ int32 fld_attr[FATTR_N_VALUES] = {2, 4, 6, 8}; /* field attribute values*/ char vattr_buf[VATTR_N_VALUES]; /* to hold vdata attribute's values */ int32 fattr_buf[FATTR_N_VALUES]; /* to hold field attribute's values */ char vattr_name[30], /* name of vdata attribute */ fattr_name[30]; /* name of field attribute */ /********************** End of variable declaration **********************/ /* * Open the HDF file for writing. */ file_id = Hopen (FILE_NAME, DFACC_WRITE, 0); /* * Initialize the VS interface. */ status_n = Vstart (file_id); /* * Get the reference number of the vdata named VDATA_NAME. */ vdata_ref = VSfind (file_id, VDATA_NAME); /* * Attach to the vdata for writing. */ vdata_id = VSattach (file_id, vdata_ref, "w"); /* * Attach an attribute to the vdata, i.e., indicated by the second parameter. */ status_n = VSsetattr (vdata_id, _HDF_VDATA, VATTR_NAME, DFNT_CHAR, VATTR_N_VALUES, vd_attr); /* * Get the index of the field FIELD_NAME within the vdata. */ status_n = VSfindex (vdata_id, FIELD_NAME, &field_index); /* * Attach an attribute to the field field_index. */ status_n = VSsetattr (vdata_id, field_index, FATTR_NAME, DFNT_INT32, FATTR_N_VALUES, fld_attr); /* * Get the number of attributes attached to the vdata's first * field - should be 0. */ n_fldattrs = VSfnattrs (vdata_id, 0); printf ( "Number of attributes of the first field of the vdata: %d\n", n_fldattrs); /* * Get the number of attributes attached to the field specified by * field_index - should be 1. */ n_fldattrs = VSfnattrs (vdata_id, field_index); printf ( "Number of attributes of field %s: %d\n", FIELD_NAME, n_fldattrs); /* * Get the total number of the field's and vdata's attributes - should be 2. */ n_vdattrs = VSnattrs (vdata_id); printf ( "Number of attributes of the vdata and its fields: %d\n", n_vdattrs); /* * Get information about the vdata's first attribute, indicated * by the third parameter which is the index of the attribute. */ status_n = VSattrinfo (vdata_id, _HDF_VDATA, 0, vattr_name, &vdata_type, &vdata_n_values, &vdata_size); /* * Get information about the first attribute of the field specified by * field_index. */ status_n = VSattrinfo (vdata_id, field_index, 0, fattr_name, &field_type, &field_n_values, &field_size); /* * Get the vdata's first attribute. */ status_n = VSgetattr (vdata_id, _HDF_VDATA, 0, vattr_buf); printf("Values of the vdata attribute = %c %c %c\n", vattr_buf[0], vattr_buf[1], vattr_buf[2]); /* * Get the first attribute of the field specified by field_index. */ status_n = VSgetattr (vdata_id, field_index, 0, fattr_buf); printf("Values of the field attribute = %d %d %d %d\n", fattr_buf[0], fattr_buf[1], fattr_buf[2], fattr_buf[3]); /* * Terminate access to the vdata and to the VS interface, then close * the HDF file. */ status_32 = VSdetach (vdata_id); status_n = Vend (file_id); status_32 = Hclose (file_id); return 0; }