# HG changeset patch # User Philip Nienhuis # Date 1360872585 -3600 # Node ID 2175c41b12d17436771b61023fdccdc984876708 # Parent 444de2c0af0e111d9579c680ab496bda4adaa394 textread.m, textscan.m: catch wrong headerlines values, tests added diff -r 444de2c0af0e -r 2175c41b12d1 scripts/io/textread.m --- a/scripts/io/textread.m Wed Feb 13 21:53:39 2013 -0800 +++ b/scripts/io/textread.m Thu Feb 14 21:09:45 2013 +0100 @@ -1,4 +1,4 @@ -## Copyright (C) 2009-2012 Eric Chassande-Mottin, CNRS (France) +## Copyright (C) 2009-2013 Eric Chassande-Mottin, CNRS (France) ## ## This file is part of Octave. ## @@ -79,10 +79,22 @@ ## Skip header lines if requested headerlines = find (strcmpi (varargin, "headerlines"), 1); - ## Beware of zero valued headerline, fskipl would skip to EOF - if (! isempty (headerlines) && (varargin{headerlines + 1} > 0)) - fskipl (fid, varargin{headerlines + 1}); - varargin(headerlines:headerlines+1) = []; + if (! isempty (headerlines)) + ## Beware of missing or wrong headerline value + if (headerlines == numel (varargin) + || ! isnumeric (varargin{headerlines + 1})) + error ("missing or illegal value for 'headerlines'" ); + endif + ## Avoid conveying floats to fskipl + varargin{headerlines + 1} = round (varargin{headerlines + 1}); + ## Beware of zero valued headerline, fskipl would skip to EOF + if (varargin{headerlines + 1} > 0) + fskipl (fid, varargin{headerlines + 1}); + varargin(headerlines:headerlines+1) = []; + nargin = nargin - 2; + elseif (varargin{headerlines + 1} < 0) + warning ("textread: negative headerline value ignored"); + endif endif st_pos = ftell (fid); @@ -98,7 +110,7 @@ if (! isempty (endofline)) ## 'endofline' option set by user. if (! ischar (varargin{endofline + 1})); - error ("textread: character value required for EndOfLine"); + error ("character value required for EndOfLine"); endif else ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars @@ -188,4 +200,6 @@ %!error textread (1) %!error textread (1, "%f") %!error textread ("fname", 1) - +%!error textread (file_in_loadpath ("textread.m"), "", "headerlines") +%!error textread (file_in_loadpath ("textread.m"), "", "headerlines", 'hh') +%!error textread (file_in_loadpath ("textread.m"), "%s", "endofline", true)