Delphi

本类阅读TOP10

·分布式网络考试系统原型分析及实现
·游戏外挂设计技术探讨①
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·试题库开发中非文本数据的处理
·如何将几个DBGRID里的内容导入同一个EXCEL表中....的问题
·如何使用Delphi设计强大的服务器程序
·工人线程中关闭窗体的实现
·用DLL方式封装MDI子窗体。
·支持XP下托盘栏气球提示的托盘单元

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
搜索字符串在流中的位置

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

(*//
标题:搜索字符串在流中的位置
说明:适用于文件搜索等
设计:Zswang
支持:[email protected]
日期: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;



相关文章

相关软件