function [name,args,sig] = extension_signatures(type)

% extension_signatures - generate extension function declarations
% ---------------------------------------------------------------
%
% [name,args,sig] = extension_signatures(type)
%
% Input:
% ------
%  type - extension types
%
% Output:
% -------
%  name - function names
%  args - function arguments
%  sig - function declarations

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

%--------------------------------
% HANDLE INPUT
%--------------------------------

%--
% normalize and check extension type
%--

type = type_norm(type);

%--------------------------------
% GET EXTENSION SIGNATURES
%--------------------------------

%--
% get function names and argument descriptions
%--

% NOTE: function names are computed from the structure of the type fun

% NOTE: input and output names are the type fun content

fun = flatten_struct(feval(type));

name = fieldnames(fun);

if (nargout < 2)
	return;
end

args = struct2cell(fun);

if (nargout < 3)
	return;
end

%--
% build function declarations using names and argument descriptions
%--
	
sig = cell(size(name));

for k = 1:length(name)

	if (isempty(args{k}))
		sig{k} = name{k}; continue;
	end

	% NOTE: if argument information is available it must contain output

	sig{k} = [args_to_str(args{k}{1},'out'), ' = ', name{k}];

	if (length(args{k}) > 1)
		sig{k} = [sig{k}, args_to_str(args{k}{2},'in')];
	end

end


%----------------------------------------------------------
% ARGS_TO_STR
%----------------------------------------------------------

function str = args_to_str(args,type)

%--
% return empty on empty
%--

% NOTE: it seems like this is never used

if (isempty(args))
	str = ''; return;
end

%--
% put arguments names into comma separated list
%--

str = '';

for k = 1:(length(args) - 1)
	str = [str, args{k}, ','];
end

str = [str, args{end}];

%--
% add brackets based on type of arguments
%--

switch (type)
	
	case ('out')
		if (length(args) > 1)
			str = ['[', str, ']'];
		end
		
	case ('in')
		str = ['(', str, ')'];
		
end
