www.pudn.com > jpegtool_matlab.rar > getint.m


% getint - retrieve an integer from a stream
%
% SYNOPSIS
%	getint(fid)
%
% DESCRIPTION
%	getint reads the next integer from fid. All characters
%	from `#' to the end of a line are ignored.

% Copyright (C) 1995-1997 Darrel Hankerson and Greg A. Harris
%
% This file is part of the JPEGtool collection of scripts for Octave
% and Matlab; see http://www.dms.auburn.edu/compression
%
% The JPEGtool collection is free software; you can redistribute it
% and/or modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2, or
% (at your option) any later version.
%
% The collection is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this package; see the file COPYING.  If not, write to the
% Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
% 02111-1307, USA.

function x = getint(fid)

octave = exist('OCTAVE_VERSION');
zero = toascii('0'); commentchar = toascii('#');
newline = toascii(sprintf('\n')); retrn = toascii(sprintf('\r'));

while (1)
  if (feof(fid)) x = NaN; return; end
  c = fread(fid, 1, 'uchar');
  if (c == commentchar)
    % Read to \r or \n.  Using fgets breaks if end-of-line is wrong.
    %if (octave) fgets(fid, 255); else fgets(fid); end
    while (c ~= newline & c ~= retrn)
      c = fread(fid, 1, 'uchar');
    end
  end
  c = c - zero;
  if (c >= 0 & c <= 9) break, end
end

x = 0;
while (1)
  x = (x * 10) + c;
  c = fread(fid, 1, 'uchar') - zero;
  if (feof(fid)) return, end
  if (c < 0 | c > 9) break, end
end