⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.pas

📁 MiniHex 1.1 源程序说明 “MiniHex11SrcSource”目录中的所有文件是MiniHex 1.1的主程序; “MiniHex11SrcControls”目录中的是该软件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
begin
  GetFormIdxRange(FromFrmIdx, ToFrmIdx, FPara.AllDoc);
  FindStr := HexStrToValStr(FPara.Text);

  if FPara.UseWild then
  begin
    i := 1;
    while i <= Length(FindStr) do
    begin
      if FindStr[i] = FPara.WildChar then
      begin
        for j := 1 to FPara.WildCount - 1 do
          Insert(FPara.WildChar, FindStr, i);
        Inc(i, FPara.WildCount - 1);
      end;
      Inc(i, 1);
    end;
  end;

  for FrmIdx := FromFrmIdx to ToFrmIdx do
  begin
    if not (MHMainForm.MDIChildren[FrmIdx] is THexChForm) then Continue;
    Frm := MHMainForm.MDIChildren[FrmIdx] as THexChForm;

    GetOffsetRange(FromOffset, ToOffset, Frm, FPara.OnlyBlock, FromCur);
    Len := Length(FindStr);
    for i := FromOffset to ToOffset - Len + 1 do
    begin
      Same := True;

      for j := 0 to Len - 1 do
      begin
        if FPara.UseWild then
        begin
          if FindStr[j+1] = FPara.WildChar then Break;
        end;
        if FindStr[j+1] <> Frm.BufPointer[i+j] then
        begin
          Same := False;
          Break;
        end;
      end;

      if Same then
      begin
        FResultOffset := i;
        FResultForm := Frm;
        Result := True;
        Exit;
      end;
    end; //for i
  end;
  FResultOffset := -1;
  FResultForm := nil;
  Result := False;
end;

function TSearchHexMgr.GetNotFoundMsg: string;
begin
  Result := '没找到十六进制串“' + FPara.Text + '”,搜索完毕。';
end;

function TSearchHexMgr.HexStrToValStr(HexStr: string): string;
var
  i: Integer;
  HexValue: Cardinal;
  S: string;
begin
  Result := '';
  if Length(HexStr) mod 2 <> 0 then
    Insert('0', HexStr, Length(HexStr));

  for i := 0 to Length(HexStr) div 2 - 1 do
  begin
    S := Copy(HexStr, i*2+1, 2);
    HexValue := 0;
    StrToIntAsHex(HexValue, S);
    Result := Result + Chr(HexValue);
  end;
end;

{ TSearchIntMgr }

function TSearchIntMgr.GetIntType(Value: Int64): TIntegerType;
begin
  if (Value >=0) and (Value <= 255) then Result := itByte
  else if (Value >= 0) and (Value <= 65535) then Result := itWord
  else if (Value >= 0) and (Value <= 4294967295) then Result := itLongWord
  else if (Value >= -128) and (Value <= 127) then Result := itShortInt
  else if (Value >= -32768) and (Value <= 32767) then Result := itSmallInt
  else if (Value >= -2147483647) and (Value <= 2147483647) then Result := itLongInt
  else Result := itInt64;
end;

function TSearchIntMgr.GetIntLen(IntType: TIntegerType): Integer;
begin
  case IntType of
    itByte:      Result := 1;
    itWord:      Result := 2;
    itLongWord:  Result := 4;
    itShortInt:  Result := 1;
    itSmallInt:  Result := 2;
    itLongInt:   Result := 4;
    itInt64:     Result := 8;
    else         Result := 0;
  end;
end;

function TSearchIntMgr.Search(FromCur: Boolean): Boolean;
var
  i, Len, FrmIdx: Integer;
  IntType: TIntegerType;
  FromFrmIdx, ToFrmIdx, FromOffset, ToOffset: Integer;
  Frm: THexChForm;
  Same: Boolean;
begin
  GetFormIdxRange(FromFrmIdx, ToFrmIdx, FPara.AllDoc);
  IntType := GetIntType(FPara.Value);
  Len := GetIntLen(IntType);

  for FrmIdx := FromFrmIdx to ToFrmIdx do
  begin
    if not (MHMainForm.MDIChildren[FrmIdx] is THexChForm) then Continue;
    Frm := MHMainForm.MDIChildren[FrmIdx] as THexChForm;

    GetOffsetRange(FromOffset, ToOffset, Frm, FPara.OnlyBlock, FromCur);
    for i := FromOffset to ToOffset - Len + 1 do
    begin
      Same := True;

      case IntType of
        itByte:      Same := (FPara.Value = PByte(Frm.BufPointer+i)^); 
        itWord:      Same := (FPara.Value = PWord(Frm.BufPointer+i)^);
        itLongWord:  Same := (FPara.Value = PLongWord(Frm.BufPointer+i)^);
        itShortInt:  Same := (FPara.Value = PShortInt(Frm.BufPointer+i)^);
        itSmallInt:  Same := (FPara.Value = PSmallInt(Frm.BufPointer+i)^);
        itLongInt:   Same := (FPara.Value = PLongInt(Frm.BufPointer+i)^);
        itInt64:     Same := (FPara.Value = PInt64(Frm.BufPointer+i)^);
      end;

      if Same then
      begin
        FResultOffset := i;
        FResultForm := Frm;
        Result := True;
        Exit;
      end;
    end; //for i
  end;
  FResultOffset := -1;
  FResultForm := nil;
  Result := False;
end;

function TSearchIntMgr.GetNotFoundMsg: string;
begin
  Result := '没找到整型值“' + IntToStr(FPara.Value) + '”,搜索完毕。';
end;

{ TSearchFloatMgr }

function TSearchFloatMgr.GetFloatLen(FloatType: TFloatType): Integer;
begin
  case FloatType of
    ftSingle:    Result := 4;
    ftDouble:    Result := 8;
    ftReal48:    Result := 6;
    ftExtended:  Result := 10;
    else         Result := 4;
  end;
end;

function TSearchFloatMgr.GetFloatHexStr(Value: Extended; FloatType: TFloatType): string;
type
  PReal48 = ^Real48;
  PExtended = ^Extended;
var
  S: string;
  i: Integer;
begin
  SetLength(S, GetFloatLen(FloatType));
  case FloatType of
    ftSingle:    PSingle(@S[1])^ := Value;
    ftDouble:    PDouble(@S[1])^ := Value;
    ftReal48:    PReal48(@S[1])^ := Value;
    ftExtended:  PExtended(@S[1])^ := Value;
  end;
  Result := '';
  for i := 1 to Length(S) do
  begin
    if i <> 1 then Result := Result + ' ';
    Result := Result + IntToHex(Ord(S[i]), 2);
  end;
end;

function TSearchFloatMgr.Search(FromCur: Boolean): Boolean;
type
  PReal48 = ^Real48;
  PExtended = ^Extended;
var
  i, Len, FrmIdx: Integer;
  FromFrmIdx, ToFrmIdx, FromOffset, ToOffset: Integer;
  Frm: THexChForm;
  Same: Boolean;
  BlurValue: Extended;
begin
  GetFormIdxRange(FromFrmIdx, ToFrmIdx, FPara.AllDoc);
  Len := GetFloatLen(FPara.FloatType);
  if FPara.Blur then BlurValue := FPara.BlurValue
  else BlurValue := 0;

  for FrmIdx := FromFrmIdx to ToFrmIdx do
  begin
    if not (MHMainForm.MDIChildren[FrmIdx] is THexChForm) then Continue;
    Frm := MHMainForm.MDIChildren[FrmIdx] as THexChForm;

    GetOffsetRange(FromOffset, ToOffset, Frm, FPara.OnlyBlock, FromCur);
    for i := FromOffset to ToOffset - Len + 1 do
    begin
      Same := True;

      case FPara.FloatType of
        ftSingle:   Same := Abs(FPara.Value - PSingle(Frm.BufPointer+i)^) <= BlurValue;
        ftDouble:   Same := Abs(FPara.Value - PDouble(Frm.BufPointer+i)^) <= BlurValue;
        ftReal48:   Same := Abs(FPara.Value - PReal48(Frm.BufPointer+i)^) <= BlurValue;
        ftExtended: Same := Abs(FPara.Value - PExtended(Frm.BufPointer+i)^) <= BlurValue;
      end;

      if Same then
      begin
        FResultOffset := i;
        FResultForm := Frm;
        Result := True;
        Exit;
      end;
    end; //for i
  end;
  FResultOffset := -1;
  FResultForm := nil;
  Result := False;
end;

function TSearchFloatMgr.GetNotFoundMsg: string;
begin
  Result := '没找到浮点值“' + FloatToStr(FPara.Value) + '”,' +
            '对应十六进制串为“' + GetFloatHexStr(FPara.Value, FPara.FloatType) + '”,搜索完毕。';
end;

{ TReplaceTextMgr }

procedure TReplaceTextMgr.SetPara(Value: TReplaceTextPara);
var
  InhPara: TSearchTextPara;
begin
  InhPara.Text := Value.FromText;
  InhPara.MatchCase := Value.MatchCase;
  InhPara.WholeWord := Value.WholeWord;
  InhPara.Unicode := Value.Unicode;
  InhPara.OnlyBlock := Value.OnlyBlock;
  InhPara.AllDoc := Value.AllDoc;
  InhPara.UseWild := Value.UseWild;
  InhPara.WildChar := Value.WildChar;
  InhPara.WildCount := Value.WildCount;
  FPara := Value;
  inherited Para := InhPara;
end;

function TReplaceTextMgr.CanReplace: Boolean;
var
  R: Integer;
begin
  Result := not FPara.AskRep;
  if FPara.AskRep then
  begin
    R := MessageBox(Application.Handle, '找到一个匹配串,替换吗?', '提示', 36);
    Result := (R = ID_YES);
  end;
end;

function TReplaceTextMgr.ReplaceFirst: Boolean;
begin
  Result := Search(False);
  if Result then
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FPara.FromText), FPara.ToText);
  end else
  begin
    MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
  end;
end;

function TReplaceTextMgr.ReplaceNext: Boolean;
begin
  Result := Search(True);
  if Result then
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FPara.FromText), FPara.ToText);
  end else
  begin
    MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
  end;
end;

function TReplaceTextMgr.ReplaceAll: Boolean;
begin
  Result := Search(False);
  while Result do
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FPara.FromText), FPara.ToText);
    Result := Search(True);
  end;
  MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
end;

{ TReplaceHexMgr }

procedure TReplaceHexMgr.SetPara(Value: TReplaceHexPara);
var
  InhPara: TSearchHexPara;
begin
  InhPara.Text := Value.FromText;
  InhPara.OnlyBlock := Value.OnlyBlock;
  InhPara.AllDoc := Value.AllDoc;
  InhPara.UseWild := Value.UseWild;
  InhPara.WildChar := Value.WildChar;
  InhPara.WildCount := Value.WildCount;
  FPara := Value;
  inherited Para := InhPara;
end;

function TReplaceHexMgr.CanReplace: Boolean;
var
  R: Integer;
begin
  Result := not FPara.AskRep;
  if FPara.AskRep then
  begin
    R := MessageBox(Application.Handle, '找到一个匹配串,替换吗?', '提示', 36);
    Result := (R = ID_YES);
  end;
end;

function TReplaceHexMgr.ReplaceFirst: Boolean;
var
  FromStr, ToStr: string;
begin
  MHMainForm.ShowStatusMsg('正在搜索...');
  Result := Search(False);
  MHMainForm.ShowStatusMsg('');
  if Result then
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    FromStr := HexStrToValStr(FPara.FromText);
    ToStr := HexStrToValStr(FPara.ToText);
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FromStr), ToStr);
  end else
  begin
    MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
  end;
end;

function TReplaceHexMgr.ReplaceNext: Boolean;
var
  FromStr, ToStr: string;
begin
  MHMainForm.ShowStatusMsg('正在搜索...');
  Result := Search(True);
  MHMainForm.ShowStatusMsg('');
  if Result then
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    FromStr := HexStrToValStr(FPara.FromText);
    ToStr := HexStrToValStr(FPara.ToText);
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FromStr), ToStr);
  end else
  begin
    MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
  end;
end;

function TReplaceHexMgr.ReplaceAll: Boolean;
var
  FromStr, ToStr: string;
begin
  MHMainForm.ShowStatusMsg('正在搜索...');
  Result := Search(False);
  MHMainForm.ShowStatusMsg('');
  FromStr := HexStrToValStr(FPara.FromText);
  ToStr := HexStrToValStr(FPara.ToText);
  while Result do
  begin
    FResultForm.BringToFront;
    FResultForm.Offset := FResultOffset;
    if CanReplace then
      FResultForm.ReplaceRange(FResultOffset, Length(FromStr), ToStr);
    MHMainForm.ShowStatusMsg('正在搜索...');
    Result := Search(True);
    MHMainForm.ShowStatusMsg('');
  end;
  MessageBox(Application.Handle, PChar(GetNotFoundMsg), '提示', 48);
end;

end.

⌨️ 快捷键说明

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