首页  编辑  

扫描流中的字符串位置,文件扫描

Tags: /超级猛料/Stream.File.流、文件和目录/流操作/   Date Created:

function ScanStream(mStream: TStream; mStr: string;

 mIgnoreCase: Boolean = False): 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 := '';

 if mIgnoreCase then mStr := UpperCase(mStr);

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

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

   if mIgnoreCase then S := UpperCase(S);

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

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

   if Result >= 0 then begin

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

     mStream.Seek(Result, soBeginning);

     Exit;

   end;

 end;

 I := mStream.Size mod cBufferSize;

 SetLength(S, I);

 if I > 0 then begin

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

   if mIgnoreCase then S := UpperCase(S);

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

   if Result >= 0 then begin

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

     mStream.Seek(Result, soBeginning);

     Exit;

   end;

 end;

end; { ScanStream }