搜索字符串在流中的位置

王朝delphi·作者佚名  2006-01-09
宽屏版  字体: |||超大  

(*//

标题:搜索字符串在流中的位置

说明:适用于文件搜索等

设计:Zswang

支持:wjhu111@21cn.com

日期:2004-03-21

//*)

(*//============================================================================

设计思路:

从流中将数据取到缓冲中

再逐一对缓冲进行搜索

============================================================================//*)

function ScanStream(mStream: TStream; mStr: string): Integer;

const

cBufferSize = $8000;

var

S: string;

T: string;

I: Integer;

L: Integer;

begin

Result := -1;

if not Assigned(mStream) then Exit;

if mStr = '' then Exit;

L := Length(mStr);

mStream.Position := 0;

SetLength(S, cBufferSize);

T := '';

for I := 1 to mStream.Size div cBufferSize do begin

mStream.Read(S[1], cBufferSize);

Result := Pos(mStr, T + S) - 1; //保留上次搜索的尾部字符~~

T := Copy(S, cBufferSize - L, MaxInt);

if Result >= 0 then begin

Result := Result + Pred(I) * cBufferSize - Length(T);

Exit;

end;

end;

I := mStream.Size mod cBufferSize;

SetLength(S, I);

if I > 0 then begin

mStream.Read(S[1], I);

Result := Pos(mStr, T + S) - 1;

if Result >= 0 then begin

Result := Result + mStream.Size - I - Length(T);

Exit;

end;

end;

end; { ScanStream }

//Example

procedure TForm1.Button1Click(Sender: TObject);

var

vFileStream: TFileStream;

begin

vFileStream := TFileStream.Create('C:\temp\temp.exe', fmShareDenyNone);

try

Label1.Caption := IntToStr(ScanStream(vFileStream, Edit1.Text));

finally

vFileStream.Free;

end;

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有