function info = parse_tag(str, sep, field)

% parse_tag - parse tag string for xbat figures
% ---------------------------------------------
%
% info = parse_tag(str, sep, field)
%
% Input:
% ------
%  str - tag string
%  sep - separator between string fields
%  field - names of fields to extract
%
% Output:
% -------
%  info - tag string info structure

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 5975 $
% $Date: 2006-08-07 18:19:44 -0400 (Mon, 07 Aug 2006) $
%--------------------------------

% TODO: develop field value version of this function that gets fields from tag

% TODO: remove defaults, these entangle code that should be independent

% TODO: send note to mathworks on strread, length of separator problem

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

%--
% set default fields
%--

% NOTE: these fields are used in the sound browser figure tags

if (nargin < 3) || isempty(field)
	field = {'type', 'user', 'library', 'sound'};
end

%--
% return empty structure on empty tag string
%--

if isempty(str)	
		
	%--
	% put fieldnames into row vector, interleave empty cells, and create struct
	%--
	
	% NOTE: this is an interesting use of comma-separated lists

	field = field(:)'; 
	
	field = [field; cell(1, length(field))];
	
	info = struct(field{:});
	
	return;
	
end

%--
% set default separator
%--

if (nargin < 2) || isempty(sep)
	sep = '::';
end

%------------------------------------------
% PARSE TAG STRING
%------------------------------------------

%--
% get separator length
%--

n = length(sep);

%--
% tokenize string and pack results into structure
%--

% NOTE: parseable strings are of the form 'field sep field sep ... sep field'

% test = strread(str,'%s',-1,'delimiter',sep);

[info.(field{1}), str] = strtok(str, sep);

for k = 2:length(field)
	[info.(field{k}), str] = strtok(str, sep);
end
	


