function lObjs = getLinkObjects(h, varargin)
%GETLINKOBJECTS returns a vector of BuildInfoLinkObject handles.
% If you specify optional includeGroups and excludeGroups arguments, you 
% can selectively source or exclude groups of BuildInfoLinkObject handles.
%
% Argument          Description
% buildinfo         Build information handle returned by RTW.Buildinfo.
% 
% includeGroups     (optional) A character array or cell array of character
%                   arrays that specifies groups of BuildInfoLinkObject
%                   handles that you want the function to return.
%
% excludeGroups     (optional) A character array or cell array of character
%                   arrays that specifies groups of BuildInfoLinkObject
%                   handles that you do not want the function to return.
%

% Copyright 2005-2023 The MathWorks, Inc.
    
%check the args to make sure they all conform to the expected format
[varargin{:}] = convertStringsToChars(varargin{:});
[argsOK, incGroups, excGroups] = locCheckArgs(varargin{:});
if ~(argsOK)
    error(message('coder_compile:CoderCompile:badMethodArguments',...
                   'RTW.BuildInfo.getLinkObjects'));
end

lObjs = h.LinkObj;
Groups = get(lObjs,'Group');

% if there are no options, then return before filtering
if isempty(lObjs)
    return;
end

if ~iscell(Groups)
    Groups = {Groups};
end


if isempty(incGroups)
    includeIdx = true(length(lObjs), 1);    
else
    includeIdx = ismember(Groups, incGroups);
end

if isempty(excGroups)
    excludeIdx = false(length(lObjs), 1);    
else
    excludeIdx = ismember(Groups, excGroups);
end

includeIdx = includeIdx & (~excludeIdx);

lObjs = lObjs(includeIdx);


function [argsOK, incGroups, excGroups] = locCheckArgs(varargin)

  argsOK = true;
  incGroups = {};
  excGroups = {};

  nargs = length(varargin);
  % all of the args must be cell arrays for the caller.  If a char array
  % or scalar is sent int, just convert them to cell arrays
  
  % the first arg is the incGroups
  if (nargs >= 1)
      if ~iscellstr(varargin{1})
          if ~ischar(varargin{1})
              % this is an incompatible data type
              argsOK = false;
              return;
          end
          % Only use this arg if it is not ''.
          if ~isempty(varargin{1})
              incGroups = varargin(1);
          end
      else
          %get the unique list, just in case there are dups
          incGroups = unique(varargin{1});
      end
  end

  % the second arg is the excGroups
  if (nargs >= 2)
      if ~iscellstr(varargin{2})
          if ~ischar(varargin{2})
              % this is an incompatible data type
              argsOK = false;
              return;
          end
          % Only use this arg if it is not ''.
          if ~isempty(varargin{2})
              excGroups = varargin(2);
          end
      else
          %get the unique list, just in case there are dups
          excGroups = unique(varargin{2});
      end
  end
