changeset 16060:f837bdd535f7

textscan.m: catch wrong headerlines values, tests added
author Philip Nienhuis <prnienhuis@users.sf.net>
date Thu, 14 Feb 2013 21:11:46 +0100
parents 2175c41b12d1
children 1a2f1001d07d
files scripts/io/textscan.m
diffstat 1 files changed, 21 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/io/textscan.m	Thu Feb 14 21:09:45 2013 +0100
+++ b/scripts/io/textscan.m	Thu Feb 14 21:11:46 2013 +0100
@@ -1,4 +1,4 @@
-## Copyright (C) 2010-2012 Ben Abbott <bpabbott@mac.com>
+## Copyright (C) 2010-2013 Ben Abbott <bpabbott@mac.com>
 ##
 ## This file is part of Octave.
 ##
@@ -167,14 +167,23 @@
     st_pos = ftell (fid);
     ## Skip header lines if requested
     headerlines = find (strcmpi (args, "headerlines"), 1);
-    ## Beware of zero valued headerline, fskipl would skip to EOF
-    if (! isempty (headerlines) && (args{headerlines + 1} > 0))
-      fskipl (fid, args{headerlines + 1});
-      args(headerlines:headerlines+1) = [];
-      st_pos = ftell (fid);
+    if (! isempty (headerlines))
+      ## Beware of missing or wrong headerline value
+      if (headerlines  == numel (args)
+         || ! isnumeric (args{headerlines + 1}))
+        error ("Missing or illegal value for 'headerlines'" );
+      endif
+      ## Avoid conveying floats to fskipl
+      args{headerlines + 1} = round (args{headerlines + 1});
+      if (args{headerlines + 1} > 0)
+        ## Beware of zero valued headerline, fskipl would skip to EOF
+        fskipl (fid, args{headerlines + 1});
+        args(headerlines:headerlines+1) = [];
+        st_pos = ftell (fid);
+      elseif (args{headerlines + 1} < 0)
+        warning ("textscan.m: negative headerline value ignored");
+      endif
     endif
-    ## Read a first file chunk. Rest follows after endofline processing
-    [str, count] = fscanf (fid, "%c", BUFLENGTH);
   endif
 
   ## Check for empty result
@@ -497,3 +506,7 @@
 %!   rh = strtrim (rh);
 %!   assert (strcmp (lh, rh));
 %! end
+
+%!error <missing or illegal value for> textread (file_in_loadpath ("textscan.m"), "", "headerlines")
+%!error <missing or illegal value for> textread (file_in_loadpath ("textscan.m"), "", "headerlines", 'hh')
+%!error <character value required for> textread (file_in_loadpath ("textscan.m"), "", "endofline", true)