function out = what_ext(d, varargin)

% what_ext - get directory content information using extensions
% -------------------------------------------------------------
%
% out = what_ext(d, x1, ..., xN)
%
% Input:
% ------
%  d - directory (def: pwd)
%  x1,...,xN - file extensions (def: '')
%
% Output:
% -------
%  out - structure with path, file extensions, and dir

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 923 $
% $Date: 2005-04-07 10:15:20 -0400 (Thu, 07 Apr 2005) $
%--------------------------------

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

%--
% set default directory
%--

if ((nargin < 1) || isempty(d))
	d = pwd;
end

%--
% set extensions to search
%--

if (length(varargin) < 1)
	ext = [];
else
	ext = varargin;
end

%-------------------------------------------------
% GET DIRECTORY AND FILE INFORMATION
%-------------------------------------------------

%---------------------------------
% set path field 
%---------------------------------

out.path = d;

%---------------------------------
% set children directory field
%---------------------------------

%--
% get directory information
%--

d = dir(d);

% NOTE: remove self and parent directory references

d = d(3:end); 

%--
% remove and keep directories
%--

D = {}; j = 1;

for k = length(d):-1:1
	
	if (d(k).isdir)
		D{j} = d(k).name;
		j = j + 1;
		d(k) = [];	
	end
	
end

%--
% set directory field
%--

out.dir = D(:);

%---------------------------------
% set extension fields
%---------------------------------

%--
% get files with specified extensions
%--

for i = 1:length(ext)
	
	%--
	% create list of selected filenames
	%--
	
	L = {}; j = 1;
	
	for k = length(d):-1:1
		
		%--
		% get extension from name
		%--
		
		ix = findstr(d(k).name,'.');
		
		% NOTE: file has no extension in name
		
		if (isempty(ix))
			continue;
		end
		
		r = d(k).name(ix(end) + 1:end);
		
		%--
		% select file based on extension
		%--
		
		% NOTE: the string comparison is case insensitive
				
		if (strcmpi(r,ext{i}))
			L{j} = d(k).name;
			j = j + 1;
			d(k) = [];
		end
		
	end
		
	%--
	% set extension field
	%--
	
	out.(ext{i}) = L(:);
	
end
