Press enter to see results or esc to cancel.

Reading Unit File

This short procedure reads unit from file to symbol tables. If header is not valid it reports error.

Function ReadUnit (UnitFileName: PChar): Boolean;
Var UnitFile: File;
    UnitHeader: PUnitHeader;
    BytesRead, SavedPreviousUnitSegment, RestOfUnit: Word;
begin
  OpenFile (UnitFile, UnitFileName);
  UnitHeader := Ptr (MainSymbolTable.Segment, 0);
  SavedPreviousUnitSegment := UnitHeader^.PreviousUnitSegment;
  BlockRead (UnitFile, UnitHeader^, SizeOf (TUnitHeader), BytesRead);
  If (BytesRead <> SizeOf (TUnitHeader)) or (UnitHeader^.Signature <> UnitSignature)
    then Error (UnitFileFormatError);
  UnitHeader^.PreviousUnitSegment := SavedPreviousUnitSegment;
  CreateExactSymbolTableSize := True;
  RestOfUnit := UnitHeader^.JoinedBlockSize - SizeOf (TUnitHeader);
  BlockRead (UnitFile, IncreaseSymbolTable (stMain, RestOfUnit)^, RestOfUnit);
  CreateExactSymbolTableSize := False;
  Close (UnitFile);
  ReadUnit := ((UnitHeader^.Flags - InitialUnitFlags) + (InitialUnitFlags - UnitHeader^.Flags)) *
                [ufUnitFlag_0200, ufUnitFlag_0100] = [];
end;