# HG changeset patch # User Philip Nienhuis # Date 1360872706 -3600 # Node ID f837bdd535f7ed61256610d011da13a7079889e9 # Parent 2175c41b12d17436771b61023fdccdc984876708 textscan.m: catch wrong headerlines values, tests added diff -r 2175c41b12d1 -r f837bdd535f7 scripts/io/textscan.m --- 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 +## Copyright (C) 2010-2013 Ben Abbott ## ## 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 textread (file_in_loadpath ("textscan.m"), "", "headerlines") +%!error textread (file_in_loadpath ("textscan.m"), "", "headerlines", 'hh') +%!error textread (file_in_loadpath ("textscan.m"), "", "endofline", true)