function varargout = vprintf(varargin); 
% ret = vprintf(string, args, [importance, val], [str], [options])
% More powerful version of sprintf.
%
% Input: 
%   string              A format string as used by fprintf
%                       Note: vprintf knows the special format string %v, see below
%   args                The arguments as given for fprintf
%
% Options: 
%   'cpr',val           Uses cprintf to print out the text. Uses the format defined by val
%   'imp',[imp val]     Prints only if imp>=val. If imp>=val-1 and 'string',str is given, then the output is appended to str.
%                           if val>=importance   : varargin is passed to fprintf/cprintf
%                           if val>=importance-1 : if str is given (as a string), then the output is appended to str and returned
%                           if val<importance-1  : nothing happens
%   'sze',[imp val]     Similar behaviour as for imp. But does print 'BIG SIZE SKIP\n' instead of not printing
%   'str',str           Output is appended to str and returned
%
% Special Format specifier: 
%       '%v'        passes argument to vdisp
%       '%r'        very compact printing of arrays
%
% E.g.: [str]=vprintf('Talk %i me: \n%v\n',2,[1 2 3; 3 2 1],'cpr',[.5 .5 0],'str','')
%
% See also: vdisp, fprintf, cprintf
%
% Written by: tommsch, 2018

[format,varargin] = parsem('cpr',varargin,'');
[imp,varargin] = parsem('imp',varargin,[1 1]);
[sze,varargin] = parsem('sze',varargin,[1 1]);
[str,varargin] = parsem('str',varargin,'');

if(imp(2)<imp(1)-1); 
    if(nargout==1); varargout{1}=str; end;
    return; 
end;
if(imp(2)<imp(1) && nargout==0); 
    if(nargout==1); varargout{1}=str; end;
    return; 
end;
if(sze(2)>sze(1)*4-1);
    text=sprintf('BIG SIZE SKIP.\n');
    if(imp(2)>=imp(1)) disp(text); end;
    if(nargout==1); varargout{1}=[str text]; end;
    return; 
end

%parse input string and replace '%v' by '%s%' and the corresponding argument by the output of vdisp(argument)
percentidx=strfind(varargin{1},'%');
i=1; %counts the pairs "%-something"
j=1; %counts the numbers in percentidx
while(true)
    if(j>size(percentidx,2)); break; end;
    if(varargin{1}(percentidx(j)+1)=='%'); 
        j=j+2;
    elseif(varargin{1}(percentidx(j)+1)~='v' && varargin{1}(percentidx(j)+1)~='r'); 
        i=i+1; j=j+1;
    elseif(varargin{1}(percentidx(j)+1)=='v' ); 
        varargin{i+1}=vdisp(varargin{1+i});
        varargin{1}=[varargin{1}(1:percentidx(j)-1) '%s' varargin{1}(percentidx(j)+2:end)];
        i=i+1; j=j+1; 
    elseif(varargin{1}(percentidx(j)+1)=='r' ); 
        varargin{i+1}=sprintf('%i ',varargin{i+1});
        varargin{1}=[varargin{1}(1:percentidx(j)-1) '%s' varargin{1}(percentidx(j)+2:end)];
        i=i+1; j=j+1; 
    end
end
if(size(varargin,2)~=i);  %test if number of arguments is correct
    error('Number of arguments wrong');
end


if(imp(2)>=imp(1))
    if(sze(2)>sze(1));
        fprintf('BIG SIZE SKIP.\n');
    else
        if(isempty(format))
            fprintf(varargin{:});  
        else
            cprintf(format, varargin{:});  
        end
    end
end

if(nargout==1); 
    text=sprintf(varargin{:});
    varargout{1}=[str text];
else
    
end;

end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing.   