function caller = get_caller(fun)

% get_caller - get caller when called from extension create functions
% -------------------------------------------------------------------
%
% caller = get_caller(fun)
%
%
% Input:
% ------
%  fun - extension function
%
% Output:
% -------
%  caller - caller info structure, decorated 'dbstack' structure

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1600 $
% $Date: 2005-08-18 17:41:06 -0400 (Thu, 18 Aug 2005) $
%--------------------------------

%--
% get caller info
%--

if ((nargin < 1) || isempty(fun))
	
	%--
	% get caller of extension create
	%--

	% NOTE: 'get_caller' and the caller of this function are the first two stack elements

	stack = dbstack('-completenames');

	% disp(' '); disp(sprintf(to_xml(stack)));

	% NOTE: this happens when creating extensions from the command line

	if (length(stack) < 3)
		caller = ''; return;
	end

	caller = stack(3);
	
else
	
	% NOTE: this could change if the stack object changes
	
	caller.file = which(fun);
	
	caller.name = fun;
	
	caller.line = 0;

end

%--
% parse caller file to get extension location
%--

% TODO: the following code works on windows, encapsulate to avoid this

loc = strread(caller.file,'%s','delimiter','\\');

% NOTE: caller is an extension if it exists within an 'Extensions' directory

ix = find(strcmp(loc,'Extensions')); 

if (isempty(ix))
	caller.loc = []; return;
end

caller.loc = loc((ix + 1):(end - 1));

% disp(' '); disp(sprintf(to_xml(caller)));
