changeset 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 07c4e6c6ad89
children 9057df9bb8a1
files src/ChangeLog src/oct-stream.cc
diffstat 2 files changed, 33 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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  <jwe@octave.org>
 
+	* oct-stream.cc (octave_stream::write (const Array<T>&,
+	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.
--- 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<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)