From 5b6ae0b6039e5e28d60618ed41d50373c580fb3f Mon Sep 17 00:00:00 2001 From: Peter Cao Date: Tue, 30 Jul 2013 14:23:24 -0500 Subject: [svn-r23945] fixed HDFFV-8306 using file stream for output data --- tools/h5jam/h5unjam.c | 105 ++++++++++++++++---------------------------------- 1 file changed, 33 insertions(+), 72 deletions(-) diff --git a/tools/h5jam/h5unjam.c b/tools/h5jam/h5unjam.c index 691e2f2..83c6614 100644 --- a/tools/h5jam/h5unjam.c +++ b/tools/h5jam/h5unjam.c @@ -27,7 +27,7 @@ hsize_t write_pad( int , hsize_t ); hsize_t compute_pad( hsize_t ); -herr_t copy_to_file( int , int , ssize_t, ssize_t ); +herr_t copy_to_file( FILE *, FILE * , ssize_t, ssize_t ); int do_delete = FALSE; char *output_file = NULL; @@ -137,20 +137,28 @@ static int parse_command_line(int argc, const char *argv[]) { int opt = FALSE; - - /* parse command line options */ + + /* parse command line options */ while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF) { switch((char)opt) { case 'o': output_file = HDstrdup(opt_arg); - break; + if (output_file) + h5tools_set_data_output_file(output_file, 1); + break; case 'i': input_file = HDstrdup(opt_arg); - break; + if (input_file) + h5tools_set_input_file(input_file); + break;; case 'u': ub_file = HDstrdup(opt_arg); + if (ub_file) + h5tools_set_output_file(ub_file); + else + rawoutstream = stdout; break; case 'd': @@ -205,9 +213,6 @@ done: int main(int argc, const char *argv[]) { - int ifid = -1; - int ufid = -1; - int h5fid = -1; void *edata; H5E_auto2_t func; hid_t ifile = -1; @@ -222,24 +227,24 @@ main(int argc, const char *argv[]) h5tools_setprogname(PROGRAMNAME); h5tools_setstatus(EXIT_SUCCESS); - /* Disable error reporting */ + /* Disable error reporting */ H5Eget_auto2(H5E_DEFAULT, &func, &edata); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); - /* Initialize h5tools lib */ + /* Initialize h5tools lib */ h5tools_init(); if(EXIT_FAILURE == parse_command_line(argc, argv)) goto done; if (input_file == NULL) { - /* no user block */ + /* no user block */ error_msg("missing arguemnt for HDF5 file input.\n"); help_ref_msg(stderr); h5tools_setstatus(EXIT_FAILURE); goto done; } - + testval = H5Fis_hdf5(input_file); if (testval <= 0) { @@ -283,14 +288,7 @@ main(int argc, const char *argv[]) goto done; } - ifid = HDopen(input_file,O_RDONLY,0); - if(ifid < 0) { - error_msg("unable to open input HDF5 file \"%s\"\n", input_file); - h5tools_setstatus(EXIT_FAILURE); - goto done; - } - - res = HDfstat(ifid, &sbuf); + res = HDfstat(fileno(rawinstream), &sbuf); if(res < 0) { error_msg("Can't stat file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); @@ -305,43 +303,15 @@ main(int argc, const char *argv[]) goto done; } - if (ub_file == NULL) { - /* write to sdtout */ - ufid = HDdup(1); - } - else { - ufid = HDopen(ub_file,O_WRONLY|O_CREAT|O_TRUNC, 0644 ); - - if (ufid < 0) { - error_msg("unable to open user block file for output\"%s\"\n", ub_file); - h5tools_setstatus(EXIT_FAILURE); - goto done; - } - } - if (output_file == NULL) { - h5fid = HDopen(input_file,O_WRONLY, 0); - - if (h5fid < 0) { error_msg("unable to open output HDF5 file \"%s\"\n", input_file); h5tools_setstatus(EXIT_FAILURE); goto done; - } } - else { - h5fid = HDopen(output_file,O_WRONLY|O_CREAT|O_TRUNC, 0644 ); - if (h5fid < 0) { - error_msg("unable to open output HDF5 file \"%s\"\n", output_file); - h5tools_setstatus(EXIT_FAILURE); - goto done; - } - } - - - /* copy from 0 to 'usize - 1' into ufid */ + /* copy from 0 to 'usize - 1' into ufid */ if (!do_delete) { - if(copy_to_file(ifid, ufid, 0, (ssize_t) usize) < 0) { + if(copy_to_file(rawinstream, rawoutstream, 0, (ssize_t) usize) < 0) { error_msg("unable to copy user block to output file \"%s\"\n", ub_file); h5tools_setstatus(EXIT_FAILURE); goto done; @@ -349,34 +319,25 @@ main(int argc, const char *argv[]) } /* copy from usize to end of file into h5fid, - * starting at end of user block if present - */ - if(copy_to_file(ifid, h5fid, (ssize_t) usize, (ssize_t)(fsize - (ssize_t)usize)) < 0) { + * starting at end of user block if present */ + if(copy_to_file(rawinstream, rawdatastream, (ssize_t) usize, (ssize_t)(fsize - (ssize_t)usize)) < 0) { error_msg("unable to copy hdf5 data to output file \"%s\"\n", output_file); h5tools_setstatus(EXIT_FAILURE); goto done; } - + done: if(input_file) HDfree(input_file); + if(output_file) HDfree(output_file); + if(ub_file) { HDfree(ub_file); - if(ufid >= 0) - status = HDclose(ufid); - HDassert(status >= 0); - } - - if(h5fid >= 0) { - status = HDclose(h5fid); - HDassert(status >= 0); - } - if(ifid >= 0) { - status = HDclose(ifid); - HDassert(status >= 0); } + + h5tools_close(); return h5tools_getstatus(); } @@ -388,7 +349,7 @@ done: * Returns 0 on success, -1 on failure. */ herr_t -copy_to_file( int infid, int ofid, ssize_t where, ssize_t how_much ) +copy_to_file( FILE *infid, FILE *ofid, ssize_t where, ssize_t how_much ) { static char buf[COPY_BUF_SIZE]; off_t to; @@ -406,20 +367,20 @@ copy_to_file( int infid, int ofid, ssize_t where, ssize_t how_much ) while(how_much > 0) { /* Seek to correct position in input file */ - HDlseek(infid,from,SEEK_SET); + HDfseek(infid,from,SEEK_SET); /* Read data to buffer */ if (how_much > COPY_BUF_SIZE) - nchars = HDread(infid,buf,(unsigned)COPY_BUF_SIZE); + nchars = HDfread(buf,1,(unsigned)COPY_BUF_SIZE,infid); else - nchars = HDread(infid,buf,(unsigned)how_much); + nchars = HDfread(buf, 1,(unsigned)how_much,infid); if(nchars < 0) { ret_value = -1; goto done; } /* end if */ /* Seek to correct position in output file */ - HDlseek(ofid,to,SEEK_SET); + HDfseek(ofid,to,SEEK_SET); /* Update positions/size */ how_much -= nchars; @@ -429,7 +390,7 @@ copy_to_file( int infid, int ofid, ssize_t where, ssize_t how_much ) /* Write nchars bytes to output file */ wnchars = nchars; while(nchars > 0) { - wnchars = HDwrite(ofid,buf,(unsigned)nchars); + wnchars = HDfwrite(buf, 1,(unsigned)nchars,ofid); if(wnchars < 0) { ret_value = -1; goto done; -- cgit v0.12