mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-17 13:49:32 +02:00
LCL Masks: use PathSpearator constant
LCL FileUtil: implemented TFileSearcher, FindAllFiles git-svn-id: trunk@13223 -
This commit is contained in:
parent
3492130ddf
commit
78a5a03772
@ -27,7 +27,7 @@ interface
|
||||
|
||||
uses
|
||||
// For Smart Linking: Do not use the LCL!
|
||||
Classes, SysUtils, LCLStrConsts;
|
||||
Classes, SysUtils, LCLStrConsts, Masks;
|
||||
|
||||
{$ifdef Windows}
|
||||
{$define CaseInsensitiveFilenames}
|
||||
@ -107,6 +107,58 @@ function FindDiskFilename(const Filename: string): string;
|
||||
function FindDiskFileCaseInsensitive(const Filename: string): string;
|
||||
function FindDefaultExecutablePath(const Executable: string): string;
|
||||
|
||||
type
|
||||
|
||||
{ TFileIterator }
|
||||
|
||||
TFileIterator = class
|
||||
private
|
||||
FPath: String;
|
||||
FLevel: Integer;
|
||||
FFileInfo: TSearchRec;
|
||||
FSearching: Boolean;
|
||||
function GetFileName: String;
|
||||
public
|
||||
procedure Stop;
|
||||
|
||||
function IsDirectory: Boolean;
|
||||
public
|
||||
property FileName: String read GetFileName;
|
||||
property FileInfo: TSearchRec read FFileInfo;
|
||||
property Level: Integer read FLevel;
|
||||
property Path: String read FPath;
|
||||
|
||||
property Searching: Boolean read FSearching;
|
||||
end;
|
||||
|
||||
TFileFoundEvent = procedure (FileIterator: TFileIterator) of object;
|
||||
TDirectoryFoundEvent = procedure (FileIterator: TFileIterator) of object;
|
||||
|
||||
{ TFileSearcher }
|
||||
|
||||
TFileSearcher = class(TFileIterator)
|
||||
private
|
||||
FOnFileFound: TFileFoundEvent;
|
||||
FOnDirectoryFound: TDirectoryFoundEvent;
|
||||
|
||||
procedure RaiseSearchingError;
|
||||
protected
|
||||
procedure DoDirectoryEnter; virtual;
|
||||
procedure DoDirectoryFound; virtual;
|
||||
procedure DoFileFound; virtual;
|
||||
public
|
||||
constructor Create;
|
||||
|
||||
procedure Search(const ASearchPath: String; ASearchMask: String = '';
|
||||
ASearchSubDirs: Boolean = True);
|
||||
public
|
||||
property OnDirectoryFound: TDirectoryFoundEvent read FOnDirectoryFound write FOnDirectoryFound;
|
||||
property OnFileFound: TFileFoundEvent read FOnFileFound write FOnFileFound;
|
||||
end;
|
||||
|
||||
function FindAllFiles(const SearchPath: String; SearchMask: String = '';
|
||||
SearchSubDirs: Boolean = True): TStringList;
|
||||
|
||||
// file actions
|
||||
function ReadFileToString(const Filename: string): string;
|
||||
function CopyFile(const SrcFilename, DestFilename: string): boolean;
|
||||
|
@ -1351,6 +1351,172 @@ begin
|
||||
Result:='';
|
||||
end;
|
||||
|
||||
type
|
||||
|
||||
{ TListFileSearcher }
|
||||
|
||||
TListFileSearcher = class(TFileSearcher)
|
||||
private
|
||||
FList: TStrings;
|
||||
protected
|
||||
procedure DoFileFound; override;
|
||||
public
|
||||
constructor Create(AList: TStrings);
|
||||
end;
|
||||
|
||||
{ TListFileSearcher }
|
||||
|
||||
procedure TListFileSearcher.DoFileFound;
|
||||
begin
|
||||
FList.Add(FileName);
|
||||
end;
|
||||
|
||||
constructor TListFileSearcher.Create(AList: TStrings);
|
||||
begin
|
||||
FList := AList;
|
||||
end;
|
||||
|
||||
function FindAllFiles(const SearchPath: String; SearchMask: String;
|
||||
SearchSubDirs: Boolean): TStringList;
|
||||
var
|
||||
Searcher: TListFileSearcher;
|
||||
begin
|
||||
Result := TStringList.Create;
|
||||
Searcher := TListFileSearcher.Create(Result);
|
||||
try
|
||||
Searcher.Search(SearchPath, SearchMask, SearchSubDirs);
|
||||
finally
|
||||
Searcher.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TFileIterator }
|
||||
|
||||
function TFileIterator.GetFileName: String;
|
||||
begin
|
||||
Result := FPath + FFileInfo.Name;
|
||||
end;
|
||||
|
||||
procedure TFileIterator.Stop;
|
||||
begin
|
||||
FSearching := False;
|
||||
end;
|
||||
|
||||
function TFileIterator.IsDirectory: Boolean;
|
||||
begin
|
||||
Result := (FFileInfo.Attr and faDirectory) <> 0;
|
||||
end;
|
||||
|
||||
{ TFileSearcher }
|
||||
|
||||
procedure TFileSearcher.RaiseSearchingError;
|
||||
begin
|
||||
raise Exception.Create('The file searcher is already searching!');
|
||||
end;
|
||||
|
||||
procedure TFileSearcher.DoDirectoryEnter;
|
||||
begin
|
||||
//
|
||||
end;
|
||||
|
||||
procedure TFileSearcher.DoDirectoryFound;
|
||||
begin
|
||||
if Assigned(FOnDirectoryFound) then OnDirectoryFound(Self);
|
||||
end;
|
||||
|
||||
procedure TFileSearcher.DoFileFound;
|
||||
begin
|
||||
if Assigned(FOnFileFound) then OnFileFound(Self);
|
||||
end;
|
||||
|
||||
constructor TFileSearcher.Create;
|
||||
begin
|
||||
FSearching := False;
|
||||
end;
|
||||
|
||||
procedure TFileSearcher.Search(const ASearchPath: String; ASearchMask: String = '';
|
||||
ASearchSubDirs: Boolean = True);
|
||||
var
|
||||
MaskList: TMaskList;
|
||||
|
||||
procedure DoSearch(const APath: String; const ALevel: Integer);
|
||||
var
|
||||
P: String;
|
||||
PathInfo: TSearchRec;
|
||||
begin
|
||||
P := APath + AllFilesMask;
|
||||
|
||||
if SysUtils.FindFirst(P, faAnyFile, PathInfo) = 0 then
|
||||
try
|
||||
begin
|
||||
repeat
|
||||
// skip special files
|
||||
if (PathInfo.Name = '.') or (PathInfo.Name = '..') or
|
||||
(PathInfo.Name = '') then Continue;
|
||||
|
||||
if (PathInfo.Attr and faDirectory) = 0 then
|
||||
begin
|
||||
if (MaskList = nil) or MaskList.Matches(PathInfo.Name) then
|
||||
begin
|
||||
FPath := APath;
|
||||
FLevel := ALevel;
|
||||
FFileInfo := PathInfo;
|
||||
DoFileFound;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
FPath := APath;
|
||||
FLevel := ALevel;
|
||||
FFileInfo := PathInfo;
|
||||
DoDirectoryFound;
|
||||
end;
|
||||
|
||||
until (SysUtils.FindNext(PathInfo) <> 0) or not FSearching;
|
||||
end;
|
||||
finally
|
||||
SysUtils.FindClose(PathInfo);
|
||||
end;
|
||||
|
||||
if ASearchSubDirs or (ALevel > 0) then // search recursively in directories
|
||||
if SysUtils.FindFirst(P, faAnyFile, PathInfo) = 0 then
|
||||
try
|
||||
begin
|
||||
repeat
|
||||
if (PathInfo.Name = '.') or (PathInfo.Name = '..') or
|
||||
(PathInfo.Name = '') or ((PathInfo.Attr and faDirectory) = 0) then Continue;
|
||||
|
||||
FPath := APath;
|
||||
FLevel := ALevel;
|
||||
FFileInfo := PathInfo;
|
||||
DoDirectoryEnter;
|
||||
if not FSearching then Break;
|
||||
|
||||
DoSearch(AppendPathDelim(APath + PathInfo.Name), Succ(ALevel));
|
||||
|
||||
until (SysUtils.FindNext(PathInfo) <> 0);
|
||||
end;
|
||||
finally
|
||||
SysUtils.FindClose(PathInfo);
|
||||
end;
|
||||
end;
|
||||
|
||||
begin
|
||||
if FSearching then RaiseSearchingError;
|
||||
|
||||
MaskList := TMaskList.Create(ASearchMask);
|
||||
// empty mask = all files mask
|
||||
if MaskList.Count = 0 then FreeAndNil(MaskList);
|
||||
|
||||
FSearching := True;
|
||||
try
|
||||
DoSearch(AppendPathDelim(ASearchPath), 0);
|
||||
finally
|
||||
FSearching := False;
|
||||
if MaskList <> nil then MaskList.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
function GetAllFilesMask: string;
|
||||
begin
|
||||
{$IFDEF WINDOWS}
|
||||
|
@ -73,7 +73,7 @@ type
|
||||
function GetCount: Integer;
|
||||
function GetItem(Index: Integer): TMask;
|
||||
public
|
||||
constructor Create(const AValue: String; ASeparator: Char = ';');
|
||||
constructor Create(const AValue: String; ASeparator: Char = PathSeparator);
|
||||
destructor Destroy; override;
|
||||
|
||||
function Matches(const AFileName: String): Boolean;
|
||||
@ -83,7 +83,7 @@ type
|
||||
end;
|
||||
|
||||
function MatchesMask(const FileName, Mask: String): Boolean;
|
||||
function MatchesMaskList(const FileName, Mask: String; Separator: Char = ';'): Boolean;
|
||||
function MatchesMaskList(const FileName, Mask: String; Separator: Char = PathSeparator): Boolean;
|
||||
|
||||
implementation
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user