function [fcnlist,loadfiles] = make_depend(fname)
% MAKE_DEPEND - Determines the functions FNAME calls.
% FNAME is the full path to the function.
% MAKE_DEPEND still crashes on some files with blank lines with tab
% characters '\t' and does not give fully correct listings of files
% which use strings that are evaluated:
% >> estr = 'my_function(2^.5);';
% >> eval(estr)
% If variables are loaded from .mat files without being named
% explicitly the variables might be confused with being
% functions. Further it does not really contex-free-parse the .m
% file but rather it assumes that there are not more than one
% statement per line and that block structures like loops and 'if's
% are spread out over several lines as in the code below.
%
% Example: make_depend('make_depend.m');
% Tested in matlab 5.3 but nothing perticular is used...
% Bjorn Gustavsson 2001-04-05
% Copyright (c) 2001 by Bjorn Gustavsson
% bjorn.gustavsson@irf.se
filedisp = 0;
delimiters = [9:13 32];
delimiters = [delimiters,'.~=+*/^()[]{}<>,;:-&|'''];
reserved_words = {'if','else','elseif','end','for','while','break', ...
'switch','case','otherwise','try','catch','return','keyboard','on','off'};
localfunction_number = 0;
commentmark = '%';
fcn_list = '';
fcn_number = 1;
variable = '';
varnumber = 1;
loadfiles = '';
loadfilenr = 1;
fp = fopen(fname,'r');
linenr = 0;
while ( 1 )
tline = fgetl(fp);
linenr = linenr+1;
if filedisp
disp(['a :',num2str(linenr),sprintf('\t'),tline])
end
if ~isstr(tline)
break,
end
tline = deblank(tline);
while ~length(tline)
tline = fgetl(fp);
if filedisp
disp(['b :',num2str(linenr),sprintf('\t'),tline])
end
linenr = linenr+1;
tline = deblank(tline);
end
strindx = findmstr(tline);
if length(strindx)
while strindx(1) == 0
strindx = strindx(2:end);
end
for j = 1:2:length(strindx)-1;
for i = strindx(j):strindx(j+1),
tline(i) = char(49);
end
end
end
[thistok,tline2] = strtok(tline);
while length(tline)
if ( thistok(1) == commentmark )
tline = '';
else
% FUNCTION DECLARATIONS
if strcmp(thistok,'function')
variable = '';
varnumber = 1;
tline = tline2;
i = find(tline=='(');
tline = tline(i+1:end);
localfunction_number = localfunction_number +1;
while ( length(tline)>1 & tline(1) ~= ')' & thistok(1)~=commentmark)
[thistok,tline] = strtok(tline,',)');
if (thistok(1)~=commentmark)
variable{varnumber} = thistok;
varnumber = varnumber+1;
end
if ( length(tline)== 0 )
tline = fgetl(fp);
linenr = linenr+1;
if filedisp
disp(['c :',num2str(linenr),sprintf('\t'),tline])
end
tline = deblank(tline);
while ~length(tline)
tline = fgetl(fp);
if filedisp
disp(['d :',num2str(linenr),sprintf('\t'),tline])
end
linenr = linenr+1;
tline = deblank(tline);
end
end
end
tline = '';
% GLOBAL DECLARATIONS
elseif strcmp(thistok,'global')
tline = tline2;
[thistok,tline] = strtok(tline,', ');
while ( length(thistok) & thistok(1)~=commentmark)
variable{varnumber} = thistok;
varnumber = varnumber+1;
[thistok,tline] = strtok(tline,', ');
end
% LOAD FILE VARIABLES
elseif strcmp(thistok,'load')
tline = tline2;
[thistok,tline] = strtok(tline,', '); % read in filename
loadfiles{loadfilenr} = thistok;
loadfilenr = loadfilenr+1;
if length(tline)
while ( length(tline) & thistok(1)~=commentmark)
[thistok,tline] = strtok(tline,', ');
if (thistok(1)~=commentmark)
variable{varnumber} = thistok;
varnumber = varnumber+1;
end
end
else
dotindx = findstr(thistok,'.');
if length(dotindx)
thistok = thistok(1:dotindx-1);
end
variable{varnumber} = thistok;
varnumber = varnumber+1;
end
% OTHER LINES
else
indx = findstr(tline,'=');
if length(indx)
% ASSIGNMENTS
if ~( any(tline(indx(1)-1)=='~<>')|tline(indx(1))==tline(indx(1)+1) )
tmpline = tline(indx+1:end);
while length(tmpline)
if findstr(tmpline,'...')
continuetoscan = 1;
else
continuetoscan = 0;
end
[thistok,tmpline] = strtok(tmpline,delimiters);
if length(thistok)
if (thistok(1)==commentmark)
tmpline = '';
else
if length(thistok)
if ~ ( is_in_cellstr(thistok,variable) | ...
is_in_cellstr(thistok,reserved_words) | ...
is_in_cellstr(thistok,fcn_list) | ...
length(findstr(thistok(1),'-.0123456789')))
fcn_list{fcn_number} = thistok;
fcn_number = fcn_number+1;
end
if ( continuetoscan & length(tmpline)==0 )
tline = fgetl(fp);
linenr = linenr+1;
if filedisp
disp(['e :',num2str(linenr),sprintf('\t'),tline])
end
tline = deblank(tline);
while ~length(tline)
tline = fgetl(fp);
if filedisp
disp(['f :',num2str(linenr),sprintf('\t'),tline])
end
linenr = linenr+1;
tline = deblank(tline);
end
continuetoscan = 0;
end
end
end
end
end
tmpline = tline(1:indx(1)-1);
[thistok,tmpline] = strtok(tmpline,delimiters);
while length(thistok)
if ~ ( is_in_cellstr(thistok,variable) )
variable{varnumber} = thistok;
varnumber = varnumber+1;
end
[thistok,tmpline] = strtok(tmpline,delimiters);
end
tline = '';
%NOT ASSIGNMENTS (IF, WHILE, FOR,...)
else
tmpline = tline;
while min(size(tmpline))
if findstr(tmpline,'...')
continuetoscan = 1;
else
continuetoscan = 0;
end
[thistok,tmpline] = strtok(tmpline,delimiters);
if length(thistok)
findx = findstr(thistok,tline);
if ~ ( tline(findx-1+length(thistok))==delimiters(end) | ...
is_in_cellstr(thistok,variable) | ...
is_in_cellstr(thistok,reserved_words) | ...
is_in_cellstr(thistok,fcn_list) | ...
length(findstr(thistok(1),'.0123456789')))
fcn_list{fcn_number} = thistok;
fcn_number = fcn_number+1;
end
if ( continuetoscan & length(tmpline)==0 )
tline = fgetl(fp);
linenr = linenr+1;
if filedisp
disp(['g :',num2str(linenr),sprintf('\t'),tline])
end
tline = deblank(tline);
while ~length(tline)
tline = fgetl(fp);
if filedisp
disp(['h :',num2str(linenr),sprintf('\t'),tline])
end
linenr = linenr+1;
tline = deblank(tline);
end
continuetoscan = 0;
end
end
end
tline = '';
end
else
tmpline = tline;
while min(size(tmpline))
if findstr(tmpline,'...')
continuetoscan = 1;
else
continuetoscan = 0;
end
[thistok,tmpline] = strtok(tmpline,delimiters);
if length(thistok)
if ~ ( is_in_cellstr(thistok,variable) | ...
is_in_cellstr(thistok,reserved_words) | ...
is_in_cellstr(thistok,fcn_list) | ...
length(findstr(thistok(1),'.0123456789')))
if strcmp(thistok,'end')
keyboard
end
fcn_list{fcn_number} = thistok;
fcn_number = fcn_number+1;
end
if ( continuetoscan & length(tmpline)==0 )
tline = fgetl(fp);
linenr = linenr+1;
if filedisp
disp(['i :',num2str(linenr),sprintf('\t'),tline])
end
tline = deblank(tline);
while ~length(tline)
tline = fgetl(fp);
if filedisp
disp(['k :',num2str(linenr),sprintf('\t'),tline])
end
linenr = linenr+1;
tline = deblank(tline);
end
continuetoscan = 0;
end
end
end
tline = '';
end
end
end
end
end
if nargout > 0
fcnlist = fcn_list';
end
if nargout > 1
loadfiles = loadfiles';
end
if nargout == 0
for i = 1:length(fcn_list)
if isstr(fcn_list{i})
s = which(fcn_list{i});
S{i} = s;
end
end
[S,I] = sort(S);
disp(' ')
for i = 1:length(fcn_list)
if isstr(fcn_list{i})
out = sprintf('%s\t\t\t%s',fcn_list{I(i)},S{i});
disp(out)
end
end
disp(' ')
end
function strindx = findmstr(tline)
% FINDMSTR - finds a matlab string in the input string TLINE
%
indx = findstr(tline,'''');
i = 1;
strindx = 0;
while (i<length(indx)),
startstr = 0;
if ( indx(i) == 1 )
strindx(i) = indx(1);
startstr = 1;
else
indx2 = findstr(tline(indx(i)-1),char([41 48:57 97:122 65:90]));
if length(indx2) == 0;
strindx(i) = indx(i);
startstr = 1;
end
end
if (startstr)
j =i+1;
while (j<=length(indx))
if (indx(j) == length(tline))
strindx(i+1) = indx(j);
i = j+1;
elseif (tline(indx(j)+1)~='''')
strindx(i+1) = indx(j);
i = j+1;
j = length(indx)+1;
else
j = j+1;
end
j = j+1;
end
else
i = i+1;
end
end
if length(strindx) < 2 ;
strindx = [];
end
function true = is_in_cellstr(in_str,in_cellstr)
% IS_CELLSTR - search after the strin IN_STR in.cellarray IN_CELLSTR
%
true = 0;
for i = 1:length(in_cellstr),
true = true+strcmp(in_str,in_cellstr{i});
end