base_sys.pas

来自「Delphi脚本控件」· PAS 代码 · 共 3,083 行 · 第 1/5 页

PAS
3,083 行
字号
    Push(I);
end;

procedure TPAXStack.Delete(I: Integer);
var
  Index, J: Integer;
begin
  Index := IndexOf(I);
  if Index = -1 then
    Exit;
  for J := Index to Card - 1 do
    fItems[J] := fItems[J+1];
  Dec(Card);
end;

procedure TPAXStack.Push(P: Pointer);
begin
  if Card = L then
  begin
    SetLength(fItems, Card + DeltaStackSize);
    L := Length(fItems) - 1;
  end;

  Inc(Card);
  fItems[Card] := Integer(P);
end;

function TPAXStack.Pop: Integer;
begin
  result := fItems[Card];
  Dec(Card);
end;

function TPAXStack.PopPointer: Pointer;
begin
  result := Pointer(fItems[Card]);
  Dec(Card);
end;

function TPAXStack.Top: Integer;
begin
  result := fItems[Card];
end;

procedure TPAXStack.SaveToStream(f: TStream);
begin
  f.Write(Card, SizeOf(Card));
  f.Write(fItems[1], Card*SizeOf(Integer));
end;

procedure TPAXStack.LoadFromStream(f: TStream);
begin
  f.Read(Card, SizeOf(Card));

  SetLength(fItems, Card + DeltaStackSize);
  L := Length(fItems) - 1;

  f.Read(fItems[1], Card*SizeOf(Integer));
end;

function TPAXStack.StackPtr: Pointer;
begin
  if Card > 0 then
    result := @fItems[Card]
  else
    result := nil;
end;

procedure TPAXStack.CopyFrom(S: TPAXStack);
var
  I: Integer;
begin
  SetLength(fItems, S.Card + DeltaStackSize);
  L := Length(fItems) - 1;

  for I:=1 to S.Card do
    fItems[I] := S.fItems[I];
  Card := S.Card;
end;

constructor TPAXFastStack.Create;
begin
  Card := 0;
end;

procedure TPAXFastStack.Clear;
begin
  Card := 0;
end;

function TPAXFastStack.GetItem(I: Integer): Integer;
begin
  result := fItems[I];
end;

function TPAXFastStack.Push(I: Integer): Integer;
begin
  Inc(Card);
  fItems[Card] := I;
  result := I;
end;

procedure TPAXFastStack.Push(P: Pointer);
begin
  Inc(Card);
  fItems[Card] := Integer(P);
end;

function TPAXFastStack.Pop: Integer;
begin
  result := fItems[Card];
  Dec(Card);
end;

function TPAXFastStack.PopPointer: Pointer;
begin
  result := Pointer(fItems[Card]);
  Dec(Card);
end;

function TPAXFastStack.Top: Integer;
begin
  result := fItems[Card];
end;

function TPAXFastStack.IndexOf(I: Integer): Integer;
var
  J: Integer;
begin
  for J:=1 to Card do
    if fItems[J] = I then
    begin
      result := J;
      Exit;
    end;
  result := -1;
end;

function TPAXFastStack.PushUnique(I: Integer): Integer;
begin
  result := I;
  if IndexOf(I) = -1 then
    Push(I);
end;

procedure TPAXFastStack.Delete(I: Integer);
var
  Index, J: Integer;
begin
  Index := IndexOf(I);
  if Index = -1 then
    Exit;
  for J := Index to Card - 1 do
    fItems[J] := fItems[J+1];
  Dec(Card);
end;

constructor TPAXNameList.Create;
begin
  inherited Create;
  HashArray := TPAXHashArray.Create;
  inherited Add('');
  NameIndex_Create := Add('Create');
  NameIndex_New := Add('New');
end;

destructor TPAXNameList.Destroy;
begin
  HashArray.Free;
  inherited;
end;

function TPAXNameList.GetSize: Integer;
var
  I: Integer;
begin
  result := 0;
  for I:=0 to Count - 1 do
    result := result + Length(Strings[I]) + 8;
  for I:=0 to MaxHash do
    result := result + HashArray.A[I].Count * 4;
end;

function TPAXNameList.Add(const S: string): Integer;
var
  _HashNumber: Integer;

function IndexOfName: Integer;
var
  I, NameIndex: Integer;
begin
  result := -1;
  for I:=0 to HashArray.A[_HashNumber].Count - 1 do
  begin
    NameIndex := Integer(HashArray.A[_HashNumber].Items[I]);
    if NameIndex >= 0 then
    if NameIndex < Count then
    if S = Strings[NameIndex] then
    begin
      result := NameIndex;
      Exit;
    end;
  end;
end;

begin
  if S = '' then
  begin
    result := 0;
    Exit;
  end;

  _HashNumber := HashNumber(S);

  result := IndexOfName;
  if result = -1 then
  begin
    result := inherited Add(S);
    HashArray.AddName(S, result, _HashNumber);
  end;
end;

function ArrayCreate(const P: array of Integer): Variant;
var
  Bounds: array of Integer;
  I, J, L: Integer;
begin
  L := Length(P);
  SetLength(Bounds, L + L);
  J := 0;
  for I:=0 to L - 1 do
  begin
    Bounds[J] := 0;
    Bounds[J+1] := P[I];
    Inc(J, 2);
  end;
  result := VarArrayCreate(Bounds, varVariant);
end;

procedure ArrayPut(V: PVariant; const Indexes: array of Integer; const Val: Variant);
var
  I, J, L, K1, K2: Integer;
  P: PVariant;
  R: array[1..100] of Integer;
begin
  L := VarArrayDimCount(V^);

{ if L = 1 then
  begin
    I := Indexes[0];
    V^[I] := Val;
    Exit;
  end; }

  R[1] := 1;
  for I:=1 to L do
  begin
    K1 := VarArrayLowBound(V^, I);
    K2 := VarArrayHighBound(V^, I);

    R[I + 1] := R[I] * (K2 - K1 + 1);
  end;

  J := 0;
  for I:=1 to L do
    J := J + R[I] * Indexes[I-1];

  P := VarArrayLock(V^);
  try
    P := ShiftPointer(P, J * _SizeVariant);
    P^ := Val;
  finally
    VarArrayUnlock(V^);
  end;
end;

function ArrayGet(V: PVariant; const Indexes: array of Integer): PVariant;
var
  I, J, L, K1, K2: Integer;
  P: PVariant;
  R: array[1..100] of Integer;
begin
  L := VarArrayDimCount(V^);

  R[1] := 1;
  for I:=1 to L do
  begin
    K1 := VarArrayLowBound(V^, I);
    K2 := VarArrayHighBound(V^, I);

    R[I + 1] := R[I] * (K2 - K1 + 1);
  end;

  J := 0;
  for I:=1 to L do
    J := J + R[I] * Indexes[I-1];

  P := VarArrayLock(V^);
  try
    P := ShiftPointer(P, J * _SizeVariant);
    result := P;
  finally
    VarArrayUnlock(V^);
  end;
end;

procedure BackUpTextFile(const FileName: String);
var
  S: String;
  T1, T2: TextFile;
  P: Integer;
begin
  P := Pos('.', FileName);
  if P > 0 then
    S := Copy(FileName, 1, P) + '~' + Copy(FileName, P + 1, 3)
  else
    S := FileName + '.~';

  AssignFile(T1, FileName);
  AssignFile(T2, S);
  Reset(T1);
  Rewrite(T2);

  try
    while not EOF(T1) do
    begin
      Readln(T1, S);
      Writeln(T2, S);
    end;
  finally
    Close(T1);
    Close(T2);
  end;
end;

function HasPublishedProperty(C: TClass; const PropName: String; Scripter: Pointer; NeedCheckForForbidden: Boolean = true): boolean;
var
  pti: PTypeInfo;
  ptd: PTypeData;
  pProps: PPropList;
  I, nProps: Integer;
  ppi: PPropInfo;
begin
  result := false;

  if NeedCheckForForbidden then
  if Scripter <> nil then
  if TPaxBaseScripter(Scripter).ForbiddenPublishedProperties.IndexOf(C) >= 0 then
    Exit;

  pti := C.ClassInfo;

  if not Assigned(pti) then
    Exit;

  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;

  if nProps > 0 then
  begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else
    pProps := nil;

  for I:=0 to nProps - 1 do
  begin
    ppi := pProps[I];
    if StrEql(ppi^.Name, PropName) then
    begin
      result := true;
      Break;
    end;
  end;

  if nProps > 0 then
   FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

function HasPublishedPropertyEx(C: TClass; const PropName: String; var ClassType: String; Scripter: Pointer): boolean;
var
  pti: PTypeInfo;
  ptd: PTypeData;
  pProps: PPropList;
  I, nProps: Integer;
  ppi: PPropInfo;
begin
  result := false;

  if Scripter <> nil then
  if TPaxBaseScripter(Scripter).ForbiddenPublishedProperties.IndexOf(C) >= 0 then
    Exit;

  ClassType := '';

  pti := C.ClassInfo;

  if not Assigned(pti) then
    Exit;

  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;

  if nProps > 0 then
  begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else
    pProps := nil;

  for I:=0 to nProps - 1 do
  begin
    ppi := pProps[I];
    if StrEql(ppi^.Name, PropName) then
    begin
{$ifdef fp}
      if ppi^.PropType^.Kind = tkClass then
        ClassType := ppi^.PropType^.Name;
{$else}
      if ppi^.PropType^^.Kind = tkClass then
        ClassType := ppi^.PropType^^.Name;
{$endif}

      result := true;
      Break;
    end;
  end;

  if nProps > 0 then
   FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

procedure GetPublishedPropertiesEx(AClass: TClass; result: TStrings);
var
  pti: PTypeInfo;
  ptd: PTypeData;
  Loop, nProps: Integer;
  pProps: PPropList;
  ppi: PPropInfo;
  S: String;
begin
  pti := AClass.ClassInfo;
  if pti = nil then Exit;
  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;
  if nProps > 0 then begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else pProps := nil;
  for Loop:=0 to nProps - 1 do begin
    ppi := pProps[Loop];
    S := ppi^.Name;
    result.Add(S + ':' + ppi^.PropType^.Name);
  end;
  if pProps <> nil then
    FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

procedure GetPublishedProperties(AClass: TClass; result: TStrings);
var
  pti: PTypeInfo;
  ptd: PTypeData;
  Loop, nProps: Integer;
  pProps: PPropList;
  ppi: PPropInfo;
  S: String;
begin
  pti := AClass.ClassInfo;
  if pti = nil then Exit;
  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;
  if nProps > 0 then begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else pProps := nil;
  for Loop:=0 to nProps - 1 do begin
    ppi := pProps[Loop];
    S := ppi^.Name;
    result.Add(UpperCase(S));
  end;
  if pProps <> nil then
    FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

function IsPublishedEventName(const S: String): boolean;
begin
  if Length(S) > 2 then
     result := (S[1] = 'O') and (S[2] = 'n') and (S[3] in ['A'..'Z', '0'..'9'])
  else
    result := false;
end;

procedure GetPublishedPropertiesEx2(AClass: TClass; result: TStrings);
var
  pti: PTypeInfo;
  ptd: PTypeData;
  Loop, nProps: Integer;
  pProps: PPropList;
  ppi: PPropInfo;
  S: String;
begin
  pti := AClass.ClassInfo;
  if pti = nil then Exit;
  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;
  if nProps > 0 then begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else pProps := nil;
  for Loop:=0 to nProps - 1 do begin
    ppi := pProps[Loop];
    S := ppi^.Name;
    if not IsPublishedEventName(S) then
      result.Add(S);
  end;
  if pProps <> nil then
    FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

procedure GetPublishedEvents(AClass: TClass; result: TStrings);
var
  pti: PTypeInfo;
  ptd: PTypeData;
  Loop, nProps: Integer;
  pProps: PPropList;
  ppi: PPropInfo;
  S: String;
begin
  pti := AClass.ClassInfo;
  if pti = nil then Exit;
  ptd := GetTypeData(pti);
  nProps := ptd^.PropCount;
  if nProps > 0 then begin
    GetMem(pProps, SizeOf(PPropInfo) * nProps);
    GetPropInfos(pti, pProps);
  end
  else pProps := nil;
  for Loop:=0 to nProps - 1 do begin
    ppi := pProps[Loop];
    S := ppi^.Name;
    if IsPublishedEventName(S) then
      result.Add(S);
  end;
  if pProps <> nil then
    FreeMem(pProps, SizeOf(PPropInfo) * nProps);
end;

procedure GetMethodList(FromClass: TClass; MethodList: TStrings);
type
  PPointer = ^Pointer;
  PMethodRec = ^TMethodRec;
  TMethodRec = packed record
    wSize: Word;
    pCode: Pointer;
    sName: ShortString;
  end;
var
  MethodTable: PChar;
  AClass: TClass;
  MethodRec: PMethodRec;
  wCount: Word;
  nMethod: integer;
begin
  MethodList.Clear;
  AClass := FromClass;
  while (AClass <> nil) do begin
    // Get a pointer to the class's published method table
    MethodTable := PChar( Pointer( PChar( AClass ) + vmtMethodTable )^ );
    if (MethodTable <> nil) then begin
      // Get the count of the methods in the table
      Move(MethodTable^, wCount, 2);
      // Position the MethodRec pointer at the first method in the table
      // (skip over the 2-byte method count)
      MethodRec := PMethodRec(MethodTable + 2);
      // Iterate through all the published methods of this class
      for nMethod := 0 to wCount - 1 do  begin
        // Add the method name and address to the MethodList TStrings
        MethodList.AddObject(MethodRec.sName, MethodRec.pCode);
        // Skip to the next method
        MethodRec := PMethodRec(PChar(MethodRec) + MethodRec.wSize);
      end;
    end;
    // Get the ancestor (parent) class
    AClass := AClass.ClassParent;
  end;
end;

function TPAXCodeList.IndexOfKey(const Key: String): Integer;
var
  I: Integer;
begin
  result := -1;
  for I:=0 to Count - 1 do
    if Records[I].Key = Key then
    begin
      result := I;
      Exit;
    end;
end;

function TPAXCodeList.AddCode(const Key, LanguageName, Code: String): TPAXCodeRec;
var
  Index: Integer;
  R: TPAXCodeRec;
begin
  Index := IndexOfKey(Key);
  if Index = -1 then
  begin
    R := TPAXCodeRec.Create;
    R.Key := K

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?