int coda_hdf4_reopen(coda_product **product) { coda_hdf4_product *product_file; product_file = (coda_hdf4_product *)malloc(sizeof(coda_hdf4_product)); if (product_file == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", sizeof(coda_hdf4_product), __FILE__, __LINE__); coda_close(*product); return -1; } product_file->filename = NULL; product_file->file_size = (*product)->file_size; product_file->format = coda_format_hdf4; product_file->root_type = NULL; product_file->product_definition = NULL; product_file->product_variable_size = NULL; product_file->product_variable = NULL; product_file->mem_size = 0; product_file->mem_ptr = NULL; product_file->is_hdf = 0; product_file->file_id = -1; product_file->gr_id = -1; product_file->sd_id = -1; product_file->an_id = -1; product_file->num_gr_file_attributes = 0; product_file->num_sd_file_attributes = 0; product_file->num_sds = 0; product_file->sds = NULL; product_file->num_images = 0; product_file->gri = NULL; product_file->num_vgroup = 0; product_file->vgroup = NULL; product_file->num_vdata = 0; product_file->vdata = NULL; product_file->filename = strdup((*product)->filename); if (product_file->filename == NULL) { coda_set_error(CODA_ERROR_OUT_OF_MEMORY, "out of memory (could not duplicate filename string) (%s:%u)", __FILE__, __LINE__); coda_hdf4_close((coda_product *)product_file); coda_close(*product); return -1; } coda_close(*product); product_file->is_hdf = Hishdf(product_file->filename); /* is this a real HDF4 file or a (net)CDF file */ if (product_file->is_hdf) { product_file->file_id = Hopen(product_file->filename, DFACC_READ, 0); if (product_file->file_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } if (Vstart(product_file->file_id) != 0) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->gr_id = GRstart(product_file->file_id); if (product_file->gr_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->an_id = ANstart(product_file->file_id); if (product_file->an_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } } product_file->sd_id = SDstart(product_file->filename, DFACC_READ); if (product_file->sd_id == -1) { coda_set_error(CODA_ERROR_HDF4, NULL); coda_hdf4_close((coda_product *)product_file); return -1; } product_file->root_type = NULL; if (init_SDSs(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } if (product_file->is_hdf) { if (init_GRImages(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } if (init_Vdatas(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } /* initialization of Vgroup entries should happen last, so we can build the structural tree */ if (init_Vgroups(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } } if (coda_hdf4_create_root(product_file) != 0) { coda_hdf4_close((coda_product *)product_file); return -1; } *product = (coda_product *)product_file; return 0; }
int main(int argc, char *argv[]) { int32 infile, aid, ret; char *filename; char datafilename[DF_MAXFNLEN]; uint16 tag; uint16 ref; int32 offset, fileoffset; int32 length; int16 special; /* Get invocation name of program */ progname = *argv++; argc--; /* parse arguments */ while (argc > 0 && **argv == '-') { switch ((*argv)[1]) { case 'd': argc--; argv++; if (argc > 0) { strcpy(datafilename, *argv++); argc--; } else { usage(); exit(1); } break; default: usage(); exit(1); } } if (argc == 1) { filename = *argv++; argc--; } else { usage(); exit(1); } if (datafilename[0] == '\0') strcpy(datafilename, DefaultDatafile); /* Check to make sure input file is HDF */ ret = (int) Hishdf(filename); if (ret == FALSE) { error("given file is not an HDF file\n"); } /* check if datafile already exists. If so, set offset to its length. */ { struct stat buf; if (stat(datafilename, &buf) == 0) { printf("External file %s already exists. Using append mode.\n", datafilename); fileoffset = (int32)buf.st_size; } else fileoffset = 0; } /* Open HDF file */ infile = Hopen(filename, DFACC_RDWR, 0); if (infile == FAIL) { error("Can't open the HDF file\n"); } /* Process the file */ ret = aid = Hstartread(infile, DFTAG_SD, DFREF_WILDCARD); while (ret != FAIL) { /* * Get data about the current one */ ret = Hinquire(aid, NULL, &tag, &ref, &length, &offset, NULL, NULL, &special); /* check the tag value since external element object are returned the same. */ if (tag == DFTAG_SD) { printf("moving Scientific Data (%d,%d) to %s\n", tag, ref, datafilename); ret = HXcreate(infile, tag, ref, datafilename, fileoffset, length); fileoffset += length; } /* * Move to the next one */ ret = Hnextread(aid, DFTAG_SD, DFREF_WILDCARD, DF_CURRENT); } /* * Close the access element */ ret = Hendaccess(aid); if (ret == FAIL) hdferror(); /* done; close files */ Hclose(infile); return (0); }
uint16 tag, ref; int16 acc_mode, special; int32 ret; int i; intn errors = 0; intn ret_bool; for (i = 0; i < BUF_SIZE; i++) outbuf[i] = (char) (i % 256); MESSAGE(5, printf("Creating a file %s\n", TESTFILE_NAME); ); fid = Hopen(TESTFILE_NAME, DFACC_CREATE, 0); CHECK_VOID(fid, FAIL, "Hopen"); ret_bool = (intn) Hishdf(TESTFILE_NAME); CHECK_VOID(ret_bool, FALSE, "Hishdf"); ret = (int32)Hnewref(fid); CHECK_VOID(ret, FAIL, "Hnewref"); MESSAGE(5, printf("Reading / Writing to file\n"); ); ret = Hputelement(fid, (uint16) 100, 1, (const uint8 *) "testing 100 1", (int32)HDstrlen("testing 100 1") + 1); CHECK_VOID(ret, FAIL, "Hputelement"); ret = Hputelement(fid, (uint16) 100, (uint16) 4, outbuf, 2000); CHECK_VOID(ret, FAIL, "Hputelement"); ret = (int32)Hnewref(fid);