function [A,count] = fread(fileID, sizeA, precision, skip, machineformat)  %#ok
%MATLAB Code Generation Library Function

%   Copyright 2013-2023 The MathWorks, Inc.

% Limitations:
%
%   1.  The precision argument must be a constant
%   2.  The types 'long', 'ulong', 'unsigned long', 'bitN' and 'ubitN' are not
%   supported in precision
%   3.  The machineformat input is not supported
%   4.  When passing C types such as int in the precision, the target and
%   production sizes for that type must agree and must map directly to a MATLAB
%   integer type.
%   5.  When reading in all of a file, the file cannot not contain more data
%   than is allowed by the largest array size for code generation.
%   6.  If sizeA is not constant or contains a non-finite element, then dynamic
%   memory allocation is required
%   7.  The char type is treated as a signed 8-bit integer.  Only values between
%   0 and 127 should be used with it.

%#codegen

% Validate nargin and set up defaults
narginchk(1,5);
eml_invariant(nargin < 5, 'Coder:toolbox:FreadMachineFormatNotSupported');

 if coder.internal.get_eml_option('NonFinitesSupport')
     DEFAULTSIZE = Inf;
 else
    %   5.  When reading in all of a file, the file cannot not contain more data
    %   than is allowed by the largest array size for code generation.
    DEFAULTSIZE = MAXINT;
end
DEFAULTPRECISION = 'uint8=>double';
DEFAULTSKIP = 0;

% Users are allowed to skip some arguments.  Handle that and dispatch to ensure
% arguments are named properly
if nargin == 1
    % fread(fid);
    [A,count] = fread(fileID, DEFAULTSIZE, DEFAULTPRECISION, DEFAULTSKIP);
elseif nargin == 2
    if ~coder.internal.isCharOrScalarString(sizeA)
        % fread(fid, sizeA);
        [A,count] = fread(fileID, sizeA, DEFAULTPRECISION, DEFAULTSKIP);
    else
        % fread(fid, precision);
        coder.internal.prefer_const(sizeA);
        [A,count] = fread(fileID, DEFAULTSIZE, sizeA, DEFAULTSKIP);
    end
elseif nargin == 3
    if ~coder.internal.isCharOrScalarString(sizeA)
        % fread(fid, sizeA, precision);
        coder.internal.prefer_const(precision);
        [A,count] = fread(fileID, sizeA, precision, DEFAULTSKIP);
    elseif ~coder.internal.isCharOrScalarString(precision)
        % fread(fid, precision, skip)
        coder.internal.prefer_const(sizeA, precision);
        [A,count] = fread(fileID, DEFAULTSIZE, sizeA, precision);
    end
else %nargin == 4: fread(fileID, sizeA, precision, skip);
    coder.internal.prefer_const(precision, skip);
    fidCheck(fileID, mfilename);
    
    % Duplicated check to ensure we have a runtime check always in MEX
    isimpFID = isImplementedFilestar(fileID);
    if coder.internal.isConst(fileID)
        eml_invariant(isimpFID, ...
            'MATLAB:notimplemented_mx');
    elseif ~isimpFID && coder.internal.hasRuntimeErrors()
        coder.internal.error('MATLAB:legacy_two_part:notimplemented_mx');
    end
    
    eml_invariant(eml_is_const(precision), ...
        'Coder:toolbox:FreadConstPrecision', ...
        'IfNotConst', 'Fail');
    eml_invariant(eml_is_const(size(sizeA)), ...
        'Coder:toolbox:InputMustBeFixedSize', ...
        'sizeA', ...
        'IfNotConst', 'Fail');
    eml_invariant(isrow(sizeA) && (isscalar(sizeA) || numel(sizeA) == 2), ...
        'MATLAB:badsize_mx', ...
        'IfNotConst', 'Fail');
    prec = coder.const(coder.internal.toCharIfString(precision));
    [A,count] = coder.internal.coderFread(fileID, sizeA, prec, skip, [DEFAULTSIZE,DEFAULTSIZE]);
end

%--------------------------------------------------------------------------

function p = isImplementedFilestar(fileID)
p = fileID ~= 0 && fileID ~= 1 && fileID ~= 2;

function m = MAXINT
    m = intmax(coder.internal.indexIntClass);

% LocalWords:  ulong ubit machineformat notimplemented badsize
