function range = extractLines(file, begin_marker, end_marker, varargin)
% EXTRACTLINES - List a file's content similar to MATLAB function
% dbtype, but using line markers instead of line numbers.
%
%   RANGE = CODER.EXAMPLE.EXTRACTLINES(FILE,BEGIN_MARKER,END_MARKER,...)
%
%   FILE         - Name of the file
%   BEGIN_MARKER - Begin with the line containing this marker
%   END_MARKER   - End with the line containing this marker
%   VARARGIN1    - Include line with begin marker (true by default)
%   VARARGIN2    - Include line with end marker (false by default)
% 
%   RANGE        - [begin_line_number, end_line_number]
%
% Example usage:
%
%   >> range = coder.example.extractLines('file.c','<S1>/Or','<S2>/And',true,false);
%
%   Lists to the MATLAB display the file contents of file.c starting with
%   the line containing '<S1>/Or' until but not including the line
%   containing '<S2>/And', and returns the corresponding line numbers.
%   Empty lines are removed following to the begin marker and leading to
%   the end marker if they are not included.

%
% Copyright 1994-2023 The MathWorks, Inc.

if nargin < 3
    DAStudio.error('rtwdemos:general:NeedThreeOrMoreArgs')
elseif nargin == 3
    inc_begin_marker = true;
    inc_end_marker   = false;
elseif nargin == 4
    inc_begin_marker = varargin{1};
    inc_end_marker   = 0;
else 
    inc_begin_marker = varargin{1};
    inc_end_marker   = varargin{2};
end

if isempty(begin_marker)
    DAStudio.error('rtwdemos:rtwdemodbtype:EmptyBeginMarker')
elseif isempty(end_marker) && inc_end_marker
    DAStudio.error('rtwdemos:rtwdemodbtype:EmptyEndMarker')
elseif exist(file,'file') == 0
    DAStudio.error('rtwdemos:rtwdemodbtype:UnknownFile', file)
end

fid=fopen(file,'r');

foundBeginMarker = false;
foundEndMarker   = false;
foundFirstNonWS  = false;

linenum = 0;
numemptyendlines = 0;
numemptybeginlines = 0;

while 1
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    linenum = linenum + 1;
    % remember the line containing the begin marker
    if ~foundBeginMarker && ~isempty(strfind(tline, begin_marker))
        foundBeginMarker = true;
        beginline = linenum;
    elseif foundBeginMarker
        % remember how many empty lines from the begin marker to the
        % next significant line so that we can skip them if we're not
        % including the start marker
        if ~foundFirstNonWS && isempty(deblank(tline))
            numemptybeginlines = numemptybeginlines + 1;
        else
            foundFirstNonWS = true;
        end
    end
    % remember the line containing the end marker (if necessary)
    if ~isempty(end_marker) && ~isempty(strfind(tline, end_marker))
        foundEndMarker = true;
        break
    end
    % remember how many empty lines leading up to the end marker so that
    % we can skip them if we're not including the end marker.
    if isempty(deblank(tline))
        numemptyendlines = numemptyendlines + 1;
    else
        numemptyendlines = 0;
    end
end
endline = linenum;

fclose(fid);

if ~foundBeginMarker
    DAStudio.error('rtwdemos:rtwdemodbtype:MissingBeginMarker', begin_marker)
end

% adjust begin line for empty lines if necessary
if ~inc_begin_marker
    beginline = beginline + numemptybeginlines + 1;
end

% adjust end line for empty lines if necessary
if inc_end_marker || ~foundEndMarker
    endline = endline - numemptyendlines;
else
    endline = endline - numemptyendlines - 1;
end

fid=fopen(file,'r');
tempfile=tempname;
tempFid=fopen(tempfile, 'w');
linenum = 0;
newline = sprintf('\n');

while 1
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    linenum = linenum + 1;
    if linenum >= beginline && linenum <= endline
        fwrite(tempFid, [tline newline]);
    end
end

fclose(fid);
fclose(tempFid);
type(tempfile);
delete(tempfile);


end
