# HG changeset patch # User John W. Eaton # Date 1238013885 14400 # Node ID 9543a90fac1863f9e74c4bf201db7e4c22b11614 # Parent 07c4e6c6ad8933e41aad85f80cd4adfacdf6a6d3 seek to skip if writing inside bounds of existing file diff -r 07c4e6c6ad89 -r 9543a90fac18 src/ChangeLog --- a/src/ChangeLog Wed Mar 25 14:34:38 2009 -0400 +++ b/src/ChangeLog Wed Mar 25 16:44:45 2009 -0400 @@ -1,5 +1,10 @@ 2009-03-25 John W. Eaton + * oct-stream.cc (octave_stream::write (const Array&, + octave_idx_type, oct_data_conv::data_type, octave_idx_type, + oct_mach_info::float_format)): Seek to skip if still inside bounds + of existing file. Otherwise, write NUL to skip. + * Makefile.in (%.df : %.cc): Write source file name to output, wrapped in XDEFUN_FILE_NAME macro. * mkbuiltins: Provide definition for XDEFUN_FILE_NAME. diff -r 07c4e6c6ad89 -r 9543a90fac18 src/oct-stream.cc --- a/src/oct-stream.cc Wed Mar 25 14:34:38 2009 -0400 +++ b/src/oct-stream.cc Wed Mar 25 16:44:45 2009 -0400 @@ -3595,17 +3595,36 @@ { std::ostream& os = *osp; - // It seems that Matlab writes zeros instead of actually - // seeking. Hmm... - if (skip != 0 && (i % block_size) == 0) { - // FIXME -- probably should try to write larger - // blocks... - - unsigned char zero = 0; - for (int j = 0; j < skip; j++) - os.write (reinterpret_cast (&zero), 1); + // Seek to skip when inside bounds of existing file. + // Otherwise, write NUL to skip. + + long orig_pos = tell (); + + seek (0, SEEK_END); + + long eof_pos = tell (); + + // Is it possible for this to fail to return us to the + // original position? + seek (orig_pos, SEEK_SET); + + long remaining = eof_pos - orig_pos; + + if (remaining < skip) + { + seek (0, SEEK_END); + + // FIXME -- probably should try to write larger + // blocks... + + unsigned char zero = 0; + for (octave_idx_type j = 0; j < skip - remaining; j++) + os.write (reinterpret_cast (&zero), 1); + } + else + seek (skip, SEEK_CUR); } if (os)