Reading Source Lines

Source files are read into buffer from where source lines are extracted. NextLine procedure moves to the next source line. If a line was read it increments counters and returns True, otherwise if there are no more source lines NextLine returns False.
Function NextLine: Boolean;
Var LineLength: Word;
begin
  If CurrentSourceFile^.CurrentLineLength <> 0 then
    begin
      Inc (CurrentSourceFile^.CurrentLineNumber);
      Inc (CurrentSourceFile^.FilePosition, CurrentSourceFile^.CurrentLineLength);
    end;
  LineLength := ReadNextLine;
  CurrentSourceFile^.CurrentLineLength := LineLength;
  If LineLength <> 0 then
    begin
      Inc (ModuleSourceLineCounter);
      Inc (LineCounter);
    end;
  CurrentSourceFile^.LineCounter := ModuleSourceLineCounter;
  NextLine := LineLength <> 0;
end;
ReadNextLine reads next source line from source file and returns its length. This procedure also checks line length and reports 'Line Too Long' error if the line has more than 128 characters which is the size of the source line buffer.
Function ReadNextLine: Word;
Var SrcPtr, DestPtr, EndPtr: PChar;
    CurrentChar: Char;
    LineLengthSpace, CurrentLineLength, BytesRead: Word;
begin
  LineLengthSpace := SizeOf (TLine);
  CurrentLineLength := 0;
  SrcPtr := FileBufferPtr;
  DestPtr := CurrentLine;
  Repeat
    EndPtr := FileBufferEnd;
    While SrcPtr <> EndPtr do
      begin
        CurrentChar := SrcPtr^;
        Inc (SrcPtr);
        Inc (CurrentLineLength);
        If CurrentChar in [#0, #10, #13, #26] then
          begin
            Case CurrentChar of
              #0, #13: Continue;
              #26: begin
                     Dec (SrcPtr);
                     Dec (CurrentLineLength);
                   end;
            end;
            DestPtr^ := #0;
            FileBufferPtr := SrcPtr;
            ErrorSourcePosition := CurrentLine;
            ReadNextLine := CurrentLineLength;
            Exit;
          end else
            begin
              DestPtr^ := CurrentChar;
              Inc (DestPtr);
              Dec (LineLengthSpace);
              If LineLengthSpace <> 0 then Continue;
              Dec (DestPtr);
              DestPtr^ := #0;
              FileBufferPtr := SrcPtr;
              ErrorSourcePosition := DestPtr;
              Error (LineTooLong);
            end;
      end;

    BytesRead := 0;
    {$I-}
      BlockRead (SourceFile, FileBuffer, SizeOf (FileBuffer), BytesRead);
    {$I+}
    FileBufferEnd := PChar (@FileBuffer) + BytesRead;
    SrcPtr := @FileBuffer;
  until BytesRead = 0;
  DestPtr^ := #0;
  FileBufferPtr := SrcPtr;
  ErrorSourcePosition := CurrentLine;
  ReadNextLine := CurrentLineLength;
end;
 
 
 
© 2017 Turbo Pascal | Privacy Policy