changeset 21621:fe0a6de805e4

textscan: Leave file read position correct at exit (bug #47671) * oct-stream.cc (delimited_stream::refresh_buf): Track movement of data within the buffer. * file-io.cc (Ftextscan): New BIST test.
author Lachlan Andrew <lachlanbis@gmail.com>
date Tue, 12 Apr 2016 21:05:50 +1000
parents f458313c0de4
children fed1111e1899
files libinterp/corefcn/file-io.cc libinterp/corefcn/oct-stream.cc
diffstat 2 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc	Wed Apr 13 14:50:32 2016 -0700
+++ b/libinterp/corefcn/file-io.cc	Tue Apr 12 21:05:50 2016 +1000
@@ -1872,6 +1872,18 @@
 %! assert (A{4}, [4 ; 8], 1e-6);
 %! assert (E);
 
+## Test leaving the file at the correct position on exit
+%!test
+%! f = tempname ();
+%! fid = fopen (f, "w+");
+%! fprintf (fid, "1,2\n3,4\n");
+%! fseek (fid, 0, "bof");
+%! A = textscan (fid, "%s %f", 2, "Delimiter", ",");
+%! E = ftell (fid);
+%! fclose (fid);
+%! unlink (f);
+%! assert (E, 8);
+
 ## Tests reading with empty format; empty fields & incomplete lower row
 %!test
 %! f = tempname ();
--- a/libinterp/corefcn/oct-stream.cc	Wed Apr 13 14:50:32 2016 -0700
+++ b/libinterp/corefcn/oct-stream.cc	Tue Apr 12 21:05:50 2016 +1000
@@ -1428,7 +1428,10 @@
   octave_quit ();                       // allow ctrl-C
 
   if (old_remaining > 0)
-    memmove (buf, idx, old_remaining);
+    {
+      buf_in_file += (idx - buf);
+      memmove (buf, idx, old_remaining);
+    }
 
   progress_marker -= idx - buf;         // where original idx would have been
   idx = buf;