[fpc-devel] FindFirst

Micha Nelissen micha at neli.hopto.org
Tue Oct 10 23:04:02 CEST 2006


Hi,

Can the FindFirst optimization in 2.1.1 for unix/sysutils.pp be merged
to 2.0.5 ? Revision 4772. Not only is it more efficient, it also fixes a
bug: I cannot find a single directory with FindFirst in 2.0.5.

Furthermore, attached is a patch for another optimization: do not
allocate a record in case user is requesting info for a single file.
Apply in rtl/unix please.

Thanks,

Micha
-------------- next part --------------
Index: sysutils.pp
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- sysutils.pp	(revision 4823)
+++ sysutils.pp	(working copy)
@@ -424,9 +424,9 @@
   if not fpstat(s,st)>=3D0 then
    exit;
   WinAttr:=3DLinuxToWinAttr(PChar(s),st);
-  If ((WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=3D0) Then
+  If (f.FindHandle =3D nil) or ((WinAttr and Not(PUnixFindData(f.FindHandl=
e)^.searchattr))=3D0) Then
    Begin
-     f.Name:=3DCopy(s,PUnixFindData(f.FindHandle)^.NamePos+1,Length(s));
+     f.Name:=3DExtractFileName(s);
      f.Attr:=3DWinAttr;
      f.Size:=3Dst.st_Size;
      f.Mode:=3Dst.st_mode;
@@ -503,26 +503,27 @@
   fillchar(Rslt,sizeof(Rslt),0);
   if Path=3D'' then
     exit;
-  { Allocate UnixFindData }
-  New(UnixFindData);
-  FillChar(UnixFindData^,sizeof(UnixFindData^),0);
-  Rslt.FindHandle:=3DUnixFindData;
-  {Create Info}
-  UnixFindData^.SearchSpec :=3D Path;
-  {We always also search for readonly and archive, regardless of Attr:}
-  UnixFindData^.SearchAttr :=3D Attr or faarchive or fareadonly;
-  UnixFindData^.NamePos :=3D Length(UnixFindData^.SearchSpec);
-  while (UnixFindData^.NamePos>0) and (UnixFindData^.SearchSpec[UnixFindDa=
ta^.NamePos]<>'/') do
-   dec(UnixFindData^.NamePos);
   {Wildcards?}
   if (Pos('?',Path)=3D0)  and (Pos('*',Path)=3D0) then
    begin
      if FindGetFileInfo(Path,Rslt) then
        Result:=3D0;
-     UnixFindData^.SearchType:=3D1;
    end
-  else
-    Result:=3DFindNext(Rslt);
+  else =

+   begin
+    { Allocate UnixFindData }
+    New(UnixFindData);
+    FillChar(UnixFindData^,sizeof(UnixFindData^),0);
+    Rslt.FindHandle:=3DUnixFindData;
+    {Create Info}
+    UnixFindData^.SearchSpec :=3D Path;
+    {We always also search for readonly and archive, regardless of Attr:}
+    UnixFindData^.SearchAttr :=3D Attr or faarchive or fareadonly;
+    UnixFindData^.NamePos :=3D Length(UnixFindData^.SearchSpec);
+    while (UnixFindData^.NamePos>0) and (UnixFindData^.SearchSpec[UnixFind=
Data^.NamePos]<>'/') do
+     dec(UnixFindData^.NamePos);
+      Result:=3DFindNext(Rslt);
+   end;
 End;
 =

=20


More information about the fpc-devel mailing list