Czy da sie to jeszcze zoptymalizowac/skrocic?

0

Witam, czy widzicie moze jakies sposoby optymalizacji, czy skrocenia tych funckji, ale tak aby nie platac zwyklego uzytkownika w dodatkowe pojecia? Moze da sie obie funkcje zamienic w jedna?

function HasFiles(const Path, Mask: string; Recursion: Boolean = True; EndAt: Integer = 1; _Count: Integer = 0): Integer;
var
  SRec: TSearchRec;
  sPath, sSearch: string;
  res: Integer;
begin
  Result := _Count;

  sPath := ITB(Path);
  sSearch := sPath + Mask;

  if FindFirst(sSearch, faAnyFile - faDirectory, SRec) = 0 then
    repeat
      Inc(Result);
      if Result = EndAt then
        Exit;
    until FindNext(SRec) <> 0;
  FindClose(SRec);

  if Recursion then
  begin
    res := FindFirst(sSearch, faArchive + faHidden + faDirectory + faReadOnly, SRec);
    while res = 0 do
    begin
      if (SRec.Attr and faDirectory <> 0) and (SRec.Name <> '.') and (SRec.Name <> '..') then
      begin
        Result := HasFiles(sPath + srec.Name, Mask, Recursion, EndAt, Result);
        if Result = EndAt then
          Exit;
      end;
      res := FindNext(SRec);
    end;
    FindClose(SRec);
  end;
end;



function HasDirs(const Path, Mask: string; Recursion: Boolean = True; EndAt: Integer = 1; _Count: Integer = 0): Integer;
var
  SRec: TSearchRec;
  sPath, sSearch: string;
  res: Integer;
begin
  Result := _Count;

  sPath := ITB(Path);
  sSearch := sPath + Mask;

  if FindFirst(sSearch, faArchive + faHidden + faDirectory + faReadOnly, SRec) = 0 then
    repeat
      if (SRec.Attr and faDirectory <> 0) and (SRec.Name <> '.') and (SRec.Name <> '..') then
      begin
        Inc(Result);
        if Result = EndAt then
          Exit;
      end;
    until FindNext(SRec) <> 0;
  FindClose(SRec);

  if Recursion then
  begin
    res := FindFirst(sSearch, faArchive + faHidden + faDirectory + faReadOnly, SRec);
    while res = 0 do
    begin
      if (SRec.Attr and faDirectory <> 0) and (SRec.Name <> '.') and (SRec.Name <> '..') then
      begin
        Result := HasDirs(sPath + srec.Name, Mask, Recursion, EndAt, Result);
        if Result = EndAt then
          Exit;
      end;
      res := FindNext(SRec);
    end;
    FindClose(SRec);
  end;
end;

P.S. Wprowadzilem male poprawki bo byl blad.

0

Nie sądze.. gdyż nie opisałeś wystarczająco tych funkcji .. - trzeba się domyślać :[ .. ale na oko wyglądają dobrze.

0

Moze cos takiego ?

function HasFiles(const Path,Mask:string;Recursion:Boolean=True;EndAt:Integer=1;_Count:Integer=0):Integer;
var SRec:TSearchRec;
var sPath:string;
var res,Attr:Integer;
begin
  Result:=_Count;
  sPath:=ITB(Path);
  if Recursion then Attr:=faArchive+faHidden+faDirectory+faReadOnly
  else Attr:=faAnyFile-faDirectory;
  res:=FindFirst(sPath+Mask,Attr,SRec);
  if res=0 then
  begin
    while res=0 do
    begin
      if (faDirectory and SRec.Attr)=0 then Inc(Result)
      else if (Recursion)and(SRec.name<>'.')and(SRec.name<>'..') then
      begin
        Result:=HasFiles(sPath+srec.name,Mask,Recursion,EndAt,Result);
      end;
      if Result=EndAt then Exit;
      res:=FindNext(SRec);
    end;
    FindClose(SRec);
  end;
end;
0

Dzieki, rzeczywiscie myslalem nad czym w tym stylu, ale jeszcze chcialbym polaczyc te dwie funkcje w jedna, tylko musial bym dac jakis parametr, a z kolei nie byloby to juz takie przejrzyste.

Chociaz ewentualnie moze byc argument Dirs ustawiony na True lub False :-|

1 użytkowników online, w tym zalogowanych: 0, gości: 1