function [file, ext, dirPath] = fileext(inputFileName)

% FILEEXT - utility to parse a file name into name and extention components
%
% [file, ext, dirPath] = fileext(inputFileName);
%
% inputFileName     1x1 or 1x2 cell containing a string with name of input 
%                   file (must include file extention) and an optional
%                   field path for *.ilwd files; if 1x2 cell, second
%                   component is ignored
%
% file              file name without extention (string)
% ext               file extension (string)
% dirPath           (optional) file directory path (string)
%
% SEE ALSO: GRAVEN 

% $Id: fileext.m,v 1.7 2004/05/18 21:10:10 stuver Exp $

% INITIALIZE output variables
% IF inputFileName is a cell
%   ASSIGN fileName
%   CHECK for errors
% ELSEIF inputFileName is a string
%   ASSIGN fileName
% ELSE
%   RETURN error
% END
% FIND indicies that are '.'
% ASSIGN file
% ASSIGN ext
% RETURN

[file, ext, dirPath] = deal([]);

if strcmp(class(inputFileName), 'cell')
    fileName = inputFileName{1};
    if ~strcmp(class(fileName), 'char')
        fileName
        error('fileext: fileName must be a string')
    end
elseif strcmp(class(inputFileName), 'char')
    fileName = inputFileName;
else
    inputFileName
    error('fileext: inputFileName must be either cell or char class')
end

I = find(fileName == '.');
% do error checking to make sure that I ~= end & there is not more
% than 1 . Check to make sure I is not empty.

if isempty(I)
    inputFileName
    error(['fileext: inputFileName starts with a . and does not have a '...
            'valid directory path'])
end

if length(I) > 1 & I(1) == 1
    % There is a directory path in the file name
    D = find(fileName == '/' | fileName == '\');
    % error check that D is not empty
    if isempty(D)
        inputFileName
        error(['fileext: inputFileName starts with a . and does not have '...
                'a valid directory path'])
    end
    file = fileName(D(end)+1:I(end)-1);
    ext = fileName(I(end)+1:end);
    dirPath = fileName(1:D(end));
elseif length(I) == 1 & I(1) ~=1 & I(1) ~= length(fileName)
    % There is no directory path in filename
    file = fileName(1:I(end)-1);
    ext = fileName(I(end)+1:end);
else
    inputFileName
    error('fileext: inputFileName is improperly formatted')
end

% Insure that the strings come out as row arrays
file = file(:)';
ext = ext(:)';
dirPath = dirPath(:)';

return