[ ivanho @ 05.12.2005. 16:40 ] @
Trebalo bi ta pretrazim niz html fajlova, i da izdvojim one koji sadrze tacan izraz/bilo koju od reci/sve reci iz unetog texta.... e sad...nasao sam nesto i preradio ga medjutim sve to radi mnogo sporo (u odnosu na windows-ov search...) da li neko zna bolje resenja? Code: function PretrazivanjeFajla(strFileName: string; strStaSeTrazi: widestring; intModPretrazivanje: integer): Boolean; const BufferSize= $8001; { 32K+1 bytes } var pBuf, pEnd, pScan, pPos : Pchar; lintFileSize: LongInt; lintBytesRemaining: LongInt; intBytesToRead: Integer; fileTemp: File; intoldMode: Word; strTemp: wideString; arrstrSearchFor: array of Record pcSearchFor: PChar; blnFound: boolean; end; intBrojac: integer; blnPrekid01: Boolean; begin if (Length( strStaSeTrazi ) = 0) or (Length( strFileName ) = 0) then Exit; pBuf := nil; AssignFile(fileTemp, strFileName); intoldMode := FileMode; FileMode := 0; Reset(fileTemp, 1); FileMode := intoldMode; strTemp:=WideUpperCase(strStaSeTrazi); strTemp:=StringReplace(strTemp,#200,'Č',[rfReplaceAll]); strTemp:=StringReplace(strTemp,#208,'Đ',[rfReplaceAll]); strTemp:=StringReplace(strTemp,#198,'Ć',[rfReplaceAll]); strStaSeTrazi:=strTemp; try SetLength(arrstrSearchFor, 0); if intModPretrazivanje=0 then begin SetLength(arrstrSearchFor, Length(arrstrSearchFor)+1); arrstrSearchFor[Length(arrstrSearchFor)-1].pcSearchFor:=StrAlloc(Length(strStaSeTrazi)+1); StrPCopy(arrstrSearchFor[Length(arrstrSearchFor)-1].pcSearchFor, strStaSeTrazi); arrstrSearchFor[length(arrstrSearchFor)-1].blnFound:=false; end else begin while Length(strStaSeTrazi)>0 do begin if Pos(' ', strStaSeTrazi)>0 then begin strTemp:=Copy(strStaSeTrazi, 1, Pos(' ', strStaSeTrazi) - 1); Delete(strStaSeTrazi, 1, Pos(' ', strStaSeTrazi)); end else begin strTemp:=strStaSeTrazi; Delete(strStaSeTrazi, 1, 255); end; if Length(strStaSeTrazi)>0 then begin while (strStaSeTrazi[1]=' ') do begin Delete(strStaSeTrazi, 1, 1); if Length(strStaSeTrazi)=0 then break; end; end; SetLength(arrstrSearchFor, Length(arrstrSearchFor)+1); arrstrSearchFor[Length(arrstrSearchFor)-1].pcSearchFor:=StrAlloc(Length(strTemp)+1); StrPCopy(arrstrSearchFor[Length(arrstrSearchFor)-1].pcSearchFor, strTemp); arrstrSearchFor[length(arrstrSearchFor)-1].blnFound:=false; end; end; GetMem(pBuf, BufferSize); lintFileSize := System.Filesize(fileTemp); lintBytesRemaining := lintFileSize; pPos := nil; while lintBytesRemaining > 0 do begin if lintbytesRemaining >= BufferSize then intBytesToRead := Pred(BufferSize) else intBytesToRead := lintBytesRemaining; BlockRead(fileTemp, pBuf^, intBytesToRead, intBytesToRead); pEnd := @pBuf[intBytesToRead]; pEnd^:= #0; pScan := pBuf; while pScan < pEnd do begin AnsiUpper(pScan); strTemp:=pScan; strTemp:=StringReplace(strTemp,'č','Č', [rfReplaceAll]); strTemp:=StringReplace(strTemp,'đ','Đ', [rfReplaceAll]); strTemp:=StringReplace(strTemp,'ć','Ć', [rfReplaceAll]); strTemp:=StringReplace(strTemp,char(13)+char(10),' ', [rfReplaceAll]); strTemp:=StringReplace(strTemp,' ',' ', [rfReplaceAll]); StrPCopy(pScan, strTemp); for intBrojac:=0 to Length(arrstrSearchFor)-1 do begin pPos := StrPos(pScan, arrstrSearchFor[intBrojac].pcSearchFor); if pPos <> nil then begin arrstrSearchFor[intBrojac].blnFound:=True; blnPrekid01 := ((intModPretrazivanje=0) or (intModPretrazivanje=1)) and true end; if blnPrekid01 then break; end; pScan := StrEnd(pScan); Inc(pScan); end; if blnPrekid01 then Break; lintBytesRemaining := lintBytesRemaining - intBytesToRead; if lintBytesRemaining > 0 then begin Seek(fileTemp, FilePos(fileTemp)-Length(strStaSeTrazi)); lintBytesRemaining := lintBytesRemaining + Length(strStaSeTrazi); end; end; finally CloseFile(fileTemp); if blnPrekid01 then Result:=true; if intModPretrazivanje=2 then begin Result:=True; for intBrojac:=0 to Length(arrstrSearchFor)-1 do begin Result:=Result and arrstrSearchFor[intBrojac].blnFound; end; end; for intBrojac:=0 to Length(arrstrSearchFor)-1 do begin if arrstrSearchFor[intBrojac].pcSearchFor <> nil then StrDispose(arrstrSearchFor[intBrojac].pcSearchFor); end; SetLength(arrstrSearchFor, 0); If pBuf <> nil then FreeMem(pBuf, BufferSize); end; end; |