function big = howbig(directory)
%HOWBIG  Number of lines in M files
%   LINES = HOWBIG(DIRECTORY) Conputes the number of code and comment lines
%   in all .M files in a DIRECTORY and its subdirectories.
%   LINES = HOWBIG applies the function to the current directory.
%
% Example
%   lines = howbig
%
% See also PWD

% Copyright (C) 2010-2014 Peter Fiala

if nargin < 1
    directory = pwd;
end

big = 0;
d = dir(directory);
dirs = d(cell2mat({d.isdir}));
for iDir = 1 : length(dirs)
    if ~strcmp(dirs(iDir).name, '.') && ~strcmp(dirs(iDir).name, '..')
        big = big + howbig(fullfile(directory, dirs(iDir).name));
    end
end

d = dir(fullfile(directory, '*.m'));
for iFile = 1 : length(d)
    fname = fullfile(directory, d(iFile).name);
    fid = fopen(fname, 'rt');
    C = textscan(fid, '%s', 'delimiter', '\n');
    C = C{1};
    b = length(C) - sum(ismember(C, ''));
    fprintf(1, '%s\t%d lines\n', fname, b);
    big = big + b;
    fclose(fid);
end
end