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; }
int list_an(int32 infile_id, int32 outfile_id, options_t *options) { int32 an_id, /* AN interface identifier */ ann_id, /* an annotation identifier */ ann_length, /* length of the text in an annotation */ an_out, /* AN interface identifier */ file_label_id, /* file label identifier */ file_desc_id, /* file description identifier */ n_file_labels, n_file_descs, n_data_labels, n_data_descs; char *ann_buf=NULL; /* buffer to hold the read annotation */ int i; /* position of an annotation in all of the same type*/ if ( options->trip==0 ) { return SUCCEED; } /* Initialize the AN interface */ an_id = ANstart (infile_id); an_out = ANstart (outfile_id); /* * Get the annotation information, e.g., the numbers of file labels, file * descriptions, data labels, and data descriptions. */ if (ANfileinfo (an_id,&n_file_labels,&n_file_descs,&n_data_labels,&n_data_descs)==FAIL) { printf( "Could not get AN info\n"); goto out; } /*------------------------------------------------------------------------- * AN_FILE_LABEL *------------------------------------------------------------------------- */ for (i = 0; i < n_file_labels; i++) { /* Get the identifier of the current data label */ ann_id = ANselect (an_id, i, AN_FILE_LABEL); /* Get the length of the data label */ ann_length = ANannlen (ann_id); /* Allocate space for the buffer to hold the data label text */ ann_buf = malloc ((ann_length+1) * sizeof (char)); /* * Read and display the file label. Note that the size of the buffer, * i.e., the third parameter, is 1 character more than the length of * the data label; that is for the null character. It is not the case * when a description is retrieved because the description does not * necessarily end with a null character. * */ if (ANreadann (ann_id, ann_buf, ann_length+1)==FAIL) { printf( "Could not read AN\n"); goto out; } /* Create the file label */ file_label_id = ANcreatef (an_out, AN_FILE_LABEL); /* Write the annotations */ if (ANwriteann (file_label_id, ann_buf, ann_length)==FAIL) { printf("Failed to write file label %d\n", i); goto out; } /* Terminate access to the current data label */ if (ANendaccess (ann_id)==FAIL || ANendaccess (file_label_id)==FAIL) { printf( "Could not end AN\n"); goto out; } /* Free the space allocated for the annotation buffer */ if (ann_buf) free (ann_buf); } /*------------------------------------------------------------------------- * AN_FILE_DESC *------------------------------------------------------------------------- */ for (i = 0; i < n_file_descs; i++) { /* Get the identifier of the current data label */ ann_id = ANselect (an_id, i, AN_FILE_DESC); /* Get the length of the data label */ ann_length = ANannlen (ann_id); /* Allocate space for the buffer to hold the data label text */ ann_buf = malloc ((ann_length+1) * sizeof (char)); if (ANreadann (ann_id, ann_buf, ann_length+1)==FAIL) { printf( "Could not read AN\n"); goto out; } /* Create the label */ file_desc_id = ANcreatef (an_out, AN_FILE_DESC); /* Write the annotations */ if (ANwriteann (file_desc_id, ann_buf, ann_length)==FAIL) { printf("Failed to write file description %d\n", i); goto out; } /* Terminate access to the current data label */ if (ANendaccess (ann_id)==FAIL || ANendaccess (file_desc_id)==FAIL) { printf( "Could not read AN\n"); goto out; } /* Free the space allocated for the annotation buffer */ if (ann_buf) { free (ann_buf); ann_buf = NULL; } } /* Terminate access to the AN interface */ if (ANend (an_id)==FAIL || ANend (an_out)==FAIL) { printf( "Could not end AN\n"); goto out; } return SUCCEED; out: if (ANend (an_id)==FAIL || ANend (an_out)==FAIL) { printf( "Could not end AN\n"); } if (ann_buf!=NULL) free (ann_buf); return FAIL; }