Press enter to see results or esc to cancel.

Conditional Defines

Turbo Pascal contains few procedures and functions that create and check conditional defines.  The first function creates initial conditional defines if certain conditions are met: MSDOSVER70CPU86 and CPU87.

Const DefaultConditionalDefines: Array [1..NumOfDefaultConditionalDefines] of TConditionalDefine = (
        (RequiredEnvironmentFlags: [enfCPU86];               Str: 'MSDOS'),
        (RequiredEnvironmentFlags: [enf04, enf02, enfCPU86]; Str: 'VER70'),
        (RequiredEnvironmentFlags: [enf04, enf02, enfCPU86]; Str: 'CPU86'),
        (RequiredEnvironmentFlags: [enfCPU87];               Str: 'CPU87'));

Procedure CreateConditionalDefines;
Var TempPtr: PChar;
    N: Byte;
begin
  TempPtr := @ConditionalDefines;
  For N := 1 to NumOfDefaultConditionalDefines do
    If InitialEnvironmentFlags * DefaultConditionalDefines [N].RequiredEnvironmentFlags <> [] then
      begin
        Move (DefaultConditionalDefines [N].Str, TempPtr^, SizeOf (DefaultConditionalDefines [N].Str));
        TempPtr := Ptr (Seg (TempPtr^), Ofs (TempPtr^) + SizeOf (DefaultConditionalDefines [N].Str));
      end;
  ConditionalDefinesEnd := PString (TempPtr);
  TempPtr := CommandLineConditionalDefines;
  Repeat
    SkipSeparators (TempPtr);
    If TempPtr^ = #0 then Exit;
    CopyToIdentifier (TempPtr);
    If Identifier [0] = #0 then Error (ErrorInInitialConditionalDdefines);
    AddConditionalDefine;
  until False;
end;

This procedure copies conditional define to the Identifier variable.

Procedure CopyToIdentifier (Var CharacterPointer: PChar);
Var Len: Byte;
begin
  Len := 0;
  Repeat
    Case UpCase (CharacterPointer^) of
      '0'..'9': If Len > 0 then
                  begin
                    Inc (Len);
                    Identifier [Len] := CharacterPointer^;
                    Inc (CharacterPointer);
                  end else Break;
      '_', 'A'..'Z': begin
                       Inc (Len);
                       Identifier [Len] := CharacterPointer^;
                       Inc (CharacterPointer);
                     end;
      else Break;
    end;
  until Len = 63;
  Identifier [0] := Char (Len);
end;

This procedure adds conditional define stored in the Identifier variable to the list of conditional defines.

Procedure AddConditionalDefine;
begin
  If IdentifierInConditionalDefines then Exit;
  If Ofs (ConditionalDefinesEnd^) - Ofs (ConditionalDefines) +
     Length (Identifier) + 1 > SizeOf (ConditionalDefines) then
       Error (TooManyConditionalSymbols);
  ConditionalDefinesEnd^ := Identifier;
  ConditionalDefinesEnd := Ptr (Seg (ConditionalDefinesEnd^),
                             Ofs (ConditionalDefinesEnd^) + Length (Identifier) + 1);
end;

This function checks if Identifier is in the list of conditional defines.

Function IdentifierInConditionalDefines: Boolean;
Var TempPtr: PString;
begin
  TempPtr := @ConditionalDefines;
  IdentifierInConditionalDefines := False;
  While TempPtr <> ConditionalDefinesEnd do
    begin
      If TempPtr^ = Identifier then
        begin
          IdentifierInConditionalDefines := True;
          DummyPtr := PChar (TempPtr);
          Exit;
        end;
      TempPtr := Ptr (Seg (TempPtr^), Ofs (TempPtr^) + Byte (TempPtr^ [0]) + 1);
    end;
end;