Mercurial > hg > octave-nkf
diff src/oct-stream.cc @ 9017:9543a90fac18
seek to skip if writing inside bounds of existing file
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Mar 2009 16:44:45 -0400 |
parents | eb63fbe60fab |
children | 4b2147b25e8d |
line wrap: on
line diff
--- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -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<const char *> (&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<const char *> (&zero), 1); + } + else + seek (skip, SEEK_CUR); } if (os)