function updateFileSeparator(h,fileSeparator)
%UPDATEFILESEPARATOR - this function updates the file separator on all paths in
% the Build Info Object to the value specified in the Settings object.  This
% includes all source and include paths, as well as file paths.  
%

%   Copyright 2005-2023 The MathWorks, Inc.

arguments
    h              RTW.BuildInfo
    fileSeparator  {mustBeTextScalar, mustBeMember(fileSeparator, {'/','\'})} = h.Settings.FileSep
end

% Matlabroot and other special dirs are in the settings object.  these
% needs to be updated for proper makefile generation.
h.Settings.updateFileSeparator(fileSeparator);

% update the local properties
h.TargetPreCompLibLoc = regexprep(h.TargetPreCompLibLoc,...
                                  '[\\/]',h.Settings.FileSep);

% update all the key/val pair vectors
locUpdateKeyValPairFileSep(h,'BuildArgs');
locUpdateKeyValPairFileSep(h,'TMFTokens');

% update the include and source modules for the root buildInfo object
fsep = h.Settings.FileSep;
h.Src.updateFileSeparator(fsep);
h.Inc.updateFileSeparator(fsep);
h.Other.updateFileSeparator(fsep);

% for each of the link objects, update the filesep on all paths
for i=1:length(h.LinkObj)
    h.LinkObj(i).updateFileSeparator(fsep);
end

% for each of the model refs, update the filesep on all paths
for i=1:length(h.ModelRefs)
    h.ModelRefs(i).updateFileSeparator(fsep);
end

function locUpdateKeyValPairFileSep(h, type)

switch (type)
  case 'BuildArgs'
    kvVec = h.BuildArgs;
  case 'TMFTokens'
    kvVec = h.Tokens;
  otherwise
    % this should never happen since it is a local function, but just to be
    % safe throw an error.
    error(message('RTW:utility:invalidInputArgs',...
                   'locUpdateBuildArgFileSep'));
end

% get the groups first, if there aren't any 'paths' then there is no need
% ot get the values
grps = get(kvVec,'Group');
if isempty(grps)
    grps = {};
end
if ~iscell(grps)
    grps = {grps};
end

% find the elements that are marked as paths
idx = ~cellfun(@isempty,regexp(grps,'.*_PATH$'));    
if ~any(idx)
    return;
end

vals = get(kvVec,'Value');
if isempty(vals)
    vals = {};
end
if ~iscell(vals)
    vals = {vals};
end

vals(idx) = coder.make.internal.standardizeFileSep(vals(idx), h.Settings.FileSep);
set(kvVec,{'Value'},vals);
