function out = visdiff(filename1, filename2, type)
%VISDIFF Compare two files or folders
%  VISDIFF(filename1,filename2) opens the Comparison Tool and presents the
%  differences between the two files or folders.
%
%  VISDIFF(filename1,filename2,type) opens the Comparison Tool and presents
%  the differences between the two files using the specified comparison
%  type. Supported options are 'xml', 'text' and 'binary'. This option does
%  not apply when comparing folders. If you do not specify a type, visdiff
%  uses the default comparison type for your selected files.
%
%  comparison = VISDIFF(...) compares two model files and returns a
%  comparison object containing the differences between the two files. This
%  syntax does not open the Comparison Tool and is only supported for
%  comparing Simulink models.
%
%  See also comparisons.Comparison

%  Copyright 1984-2017 The MathWorks, Inc.

import comparisons.internal.util.process;
import comparisons.internal.util.APIUtils;
import comparisons.internal.compareWithoutDisplay;

narginchk(2, 3);
error(javachk('jvm'));

APIUtils.parse('visdiff', filename1, filename2);
[filename1, filename2] = convertStringsToChars(filename1, filename2);

try
    if nargout == 0
        error(javachk('swing'));
        if nargin < 3
            % By default, choose a comparison method automatically even if
            % there is a choice.
            type = true;
        end
        % Invoke the Comparison Tool, which will perform the comparison and
        % display the result.
        process(...
            @() comparisons_private('comparefiles', filename1, filename2, type)...
        );
    else
        if nargin < 3
            showchars = 60;
        elseif ischar(type) || (isstring(type) && isscalar(type))
            showchars = str2double(type);
        else
            showchars = type;
        end

        out = process(...
             @() compareWithoutDisplay(filename1, filename2, showchars)...
        );
    end
catch exception
    APIUtils.handleExceptionCallStack(exception);
end