Search the location of the string in the stream

zhaozj2021-02-16  43

(* //

Title: Search the location of the string in the stream

Description: Suitable for file search, etc.

Design: zswang

Support: wjhu111@21cn.com

Date: 2004-03-21

// *)

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

Design ideas:

Take data from the stream into the buffer

Search by one by one

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

Function ScanStream (MSTREAM: TSTREAM; MSTR: STRING): Integer;

Const

CBuffersize = $ 8000;

VAR

String;

T: string;

I: integer;

L: integer;

Begin

RESULT: = -1;

IF NOT ASSIGNED (MSTREAM).

IF mstr = ''. '.

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; // Keep the last searcher of the last search ~~

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}

// esample

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

VFileStream: tfilestream;

Begin

vFileStream: = TFileStream.create ('c: /temp/temp.exe', fmsharedenynone);

Try

Label1.caption: = INTOSTR (ScanStream (vFileStream, Edit1.Text));

Finally

VFileStream.free;

END;

END;

转载请注明原文地址:https://www.9cbs.com/read-27901.html

New Post(0)