کد دلفی - جستجوی یک فایل برای یک الگو

functionScanFile(constfilename:String;constforString:String; caseSensitive:Boolean):LongInt; { returns position of string in file or -1, if not found } const BufferSize=$8001;{ 32K + 1 bytes } var pBuf,pEnd,pScan,pPos:Pchar; filesize:LongInt; bytesRemaining:LongInt; bytesToRead:Word; F:File; SearchFor:Pchar; oldMode:Word; begin Result:=-1;{ assume failure } if(Length(forString)=0)or(Length(filename)=0)then Exit; SearchFor:=Nil; pBuf:=Nil; { open file as binary, 1 byte recordsize } AssignFile(F,filename); oldMode:=FileMode; FileMode:=0;{ read-only access } Reset(F,1); FileMode:=oldMode; try{ allocate memory for buffer and pchar search string } SearchFor:=StrAlloc(Length(forString)+1); StrPCopy(SearchFor,forString); ifnotcaseSensitivethen{ convert to upper case } AnsiUpper(SearchFor); GetMem(pBuf,BufferSize); filesize:=System.Filesize(F); bytesRemaining:=filesize; pPos:=Nil; whilebytesRemaining>0do begin { calc how many bytes to read this round } ifbytesRemaining>=BufferSizethen bytesToRead:=Pred(BufferSize) else bytesToRead:=bytesRemaining; { read a buffer full and zero-terminate the buffer } BlockRead(F,pBuf^,bytesToRead,bytesToRead); pEnd:=@pBuf[bytesToRead]; pEnd^:=#0; { scan the buffer. Problem: buffer may contain #0 chars! So we treat it as a concatenation of zero-terminated strings. } pScan:=pBuf; whilepScan<pEnddo begin ifnotcaseSensitivethen{ convert to upper case } AnsiUpper(pScan); pPos:=StrPos(pScan,SearchFor);{ search for substring } ifpPos<>Nilthen begin{ Found it! } Result:=FileSize-bytesRemaining+LongInt(pPos) -LongInt(pBuf); Break; end; pScan:=StrEnd(pScan); Inc(pScan); end; ifpPos<>Nilthen Break; bytesRemaining:=bytesRemaining-bytesToRead; ifbytesRemaining>0then begin { no luck in this buffers load. We need to handle the case of the search string spanning two chunks of file now. We simply go back a bit in the file and read from there, thus inspecting some characters twice } Seek(F,FilePos(F)-Length(forString)); bytesRemaining:=bytesRemaining+Length(forString); end; end; finally CloseFile(F); ifSearchFor<>Nilthen StrDispose(SearchFor); ifpBuf<>Nilthen FreeMem(pBuf,BufferSize); end; end;
/ 1 نظر / 6 بازدید
همخونه دات کام

------------------------------ پورتال همخونه دات کام افتتاح شد ------------------------------ همخونه دات کام اولین و تنها وب سایت جستجوی خانه دانشجویی در ایران لطفا برای حمایت از همخونه دات کام به ما لینک دهید.