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; }
static int read_attribute(const coda_cursor *cursor, void *dst, long length) { long index; index = cursor->stack[cursor->n - 1].index; assert(cursor->n >= 2); switch (((coda_hdf4_type *)cursor->stack[cursor->n - 2].type)->tag) { case tag_hdf4_attributes: { coda_hdf4_attributes *type; type = (coda_hdf4_attributes *)cursor->stack[cursor->n - 2].type; if (cursor->stack[cursor->n - 1].index < type->num_obj_attributes) { int32 tag = -1; switch (type->parent_tag) { case tag_hdf4_GRImage: tag = DFTAG_RI; break; case tag_hdf4_SDS: tag = DFTAG_SD; break; case tag_hdf4_Vdata_field: case tag_hdf4_Vdata: tag = DFTAG_VS; break; case tag_hdf4_Vgroup: tag = DFTAG_VG; break; default: assert(0); exit(1); } if (read_attribute_sub(tag, type->parent_id, index, type->field_index, length, dst) != 0) { return -1; } } else if (cursor->stack[cursor->n - 1].index < type->num_obj_attributes + type->num_data_labels) { if (read_attribute_sub(DFTAG_DIL, type->ann_id[index - type->num_obj_attributes], index - type->num_obj_attributes, type->field_index, length, dst) != 0) { return -1; } } else { if (read_attribute_sub(DFTAG_DIA, type->ann_id[index - type->num_obj_attributes], index - type->num_obj_attributes - type->num_data_labels, type->field_index, length, dst) != 0) { return -1; } } } break; case tag_hdf4_file_attributes: { coda_hdf4_file_attributes *type; type = (coda_hdf4_file_attributes *)cursor->stack[cursor->n - 2].type; if (cursor->stack[cursor->n - 1].index < type->num_gr_attributes) { if (read_attribute_sub(DFTAG_RI, ((coda_hdf4_product *)cursor->product)->gr_id, index, -1, length, dst) != 0) { return -1; } } else if (cursor->stack[cursor->n - 1].index < type->num_gr_attributes + type->num_sd_attributes) { if (read_attribute_sub(DFTAG_SD, ((coda_hdf4_product *)cursor->product)->sd_id, index - type->num_gr_attributes, -1, length, dst) != 0) { return -1; } } else if (cursor->stack[cursor->n - 1].index < type->num_gr_attributes + type->num_sd_attributes + type->num_file_labels) { int32 ann_id; ann_id = ANselect(((coda_hdf4_product *)cursor->product)->an_id, index - type->num_gr_attributes - type->num_sd_attributes, AN_FILE_LABEL); if (ann_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (read_attribute_sub(DFTAG_FID, ann_id, index - type->num_gr_attributes - type->num_sd_attributes, -1, length, dst) != 0) { return -1; } if (ANendaccess(ann_id) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } else { int32 ann_id; ann_id = ANselect(((coda_hdf4_product *)cursor->product)->an_id, index - type->num_gr_attributes - type->num_sd_attributes - type->num_file_labels, AN_FILE_DESC); if (ann_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } if (read_attribute_sub(DFTAG_FD, ann_id, index - type->num_gr_attributes - type->num_sd_attributes - type->num_file_labels, -1, length, dst) != 0) { return -1; } if (ANendaccess(ann_id) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); return -1; } } } break; default: assert(0); exit(1); } return 0; }