%
% PARSEINTERVALS
% 
% [parintervals, success, errormessage] = parseintervals(interval_list, {verbose})
% 
% Parses a comma delimited list of intervals to an array of degrees, their 
% associated accidentals and whether or not they are present (presence 
% denoted by a '*' in front of an interval in the input interval_list). 
%
% Success = 1 if symbols parsed correctly, 0 otherwise.
%
% If optional argument 'verbose' is 1, function prints any errormessage to 
% the screen.
% 
% calls parseinterval
% 
% returns:  intervals     (array of int: [degree,accidental,present])
%           success       (boolean)
%           errormessage  (string) 
% 
%
% Author: Christopher Harte, March 2009
% 
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 
%
% This file is part of the C4DM Chord Toolkit V2.0  
%
% The C4DM Chord Toolkit is free software; you can redistribute it and/or 
% modify it under the terms of the GNU General Public License as published 
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% The C4DM Chord Toolkit is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with the C4DM Toolkit; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

%
function [parintervals, success, errormessage] = parseintervals(interval_list, verbose)

% set verbose default to 0
if nargin < 2
    verbose = 0;
end

errormessage = '';

ilength = length(interval_list);

index = 1;

tempindex = 1;

tempstring = '';

success = 1;

parindex  = 1;

parintervals = [];


while index <= ilength
    
   
    while (interval_list(index) ~= ',')  
        
        tempstring(tempindex) = interval_list(index);
        tempindex = tempindex +1;
        index = index + 1;
        
        if(index > ilength)
            break;
        end
        
        if (interval_list(index) == ',') && (index == ilength)
            success = 0;
            errormessage = sprintf(['Error in parseintervals: interval list finishes with a comma "' interval_list '"\n']);
        end
            
            
    end
    
        [parintervals(parindex,1),parintervals(parindex,2),parintervals(parindex,3),ok, error] = parseinterval(tempstring);
        % concatenate error messages if there are any...
        errormessage = [errormessage error];
        
        if(ok == 1)
            tempstring = '';
            tempindex = 1;
            parindex = parindex + 1;
            index = index + 1;
        else

            errormessage = [errormessage sprintf(['Error in parseintervals: incorrect interval in list "' interval_list '"\n'])];
            success = 0;
            index = ilength +1;            
        end
    
    
end

if (success == 0) && (verbose == 1)

    fprintf(1,errormessage)
    
end    


    
              






