diff options
author | tobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c> | 2009-07-23 20:36:14 +0000 |
---|---|---|
committer | tobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c> | 2009-07-23 20:36:14 +0000 |
commit | 3c999bea6a60e6b58dc8e9a70c08898c4efea906 (patch) | |
tree | 87c9ae640e5e2943f0aa6f9ad6297f59997a9d01 | |
parent | 483260f11f03502b20d470c416366844f1199927 (diff) | |
download | usdx-3c999bea6a60e6b58dc8e9a70c08898c4efea906.tar.gz usdx-3c999bea6a60e6b58dc8e9a70c08898c4efea906.tar.xz usdx-3c999bea6a60e6b58dc8e9a70c08898c4efea906.zip |
Buggy DirectoryFindFiles() (used by BrowseTXT/XMLFiles) removed and replaced with TSongs.FindFilesByExtension()
- DirectoryFindFiles() was not applicable as:
- it found files that contained the extension anywhere in the file, e.g. file.txt.bak
- was not recursive (caller had to explore subdirs himself)
- was not correct if ReturnAllSubDirs=false, as it also returned matching directories with IsFile=true and IsDirectory=false
git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1912 b956fd51-792f-4845-bead-9b4dfca2ff2c
Diffstat (limited to '')
-rw-r--r-- | unicode/src/base/UCommon.pas | 51 | ||||
-rw-r--r-- | unicode/src/base/USongs.pas | 105 |
2 files changed, 58 insertions, 98 deletions
diff --git a/unicode/src/base/UCommon.pas b/unicode/src/base/UCommon.pas index 187c21fb..fa0faf3c 100644 --- a/unicode/src/base/UCommon.pas +++ b/unicode/src/base/UCommon.pas @@ -82,17 +82,6 @@ procedure ZeroMemory(Destination: pointer; Length: dword); function MakeLong(a, b: word): longint; {$ENDIF} -type - TDirectoryEntry = record - Name: IPath; - IsDirectory: boolean; - IsFile: boolean; - end; - - TDirectoryEntryArray = array of TDirectoryEntry; - -function DirectoryFindFiles(Dir: IPath; Filter: UTF8String; ReturnAllSubDirs: boolean): TDirectoryEntryArray; - // A stable alternative to TList.Sort() (use TList.Sort() if applicable, see below) procedure MergeSort(List: TList; CompareFunc: TListSortCompare); @@ -298,46 +287,6 @@ end; {$ENDIF} -function DirectoryFindFiles(Dir: IPath; Filter: UTF8String; ReturnAllSubDirs: Boolean): TDirectoryEntryArray; -var - i: integer; - Iter: IFileIterator; - FileInfo: TFileInfo; - FileName: IPath; - Attrib: integer; -begin - i := 0; - Filter := UTF8LowerCase(Filter); - - // search for all files and directories - Iter := FileSystem.FileFind(Dir.Append('*'), faAnyFile); - while (Iter.HasNext) do - begin - FileInfo := Iter.Next; - FileName := FileInfo.Name; - if (not FileName.Equals('.')) and (not FileName.Equals('..')) then - begin - Attrib := Dir.Append(FileName).GetAttr(); - if ReturnAllSubDirs and ((Attrib and faDirectory) <> 0) then - begin - SetLength(Result, i + 1); - Result[i].Name := FileName; - Result[i].IsDirectory := true; - Result[i].IsFile := false; - i := i + 1; - end - else if (Filter = '') or (Pos(Filter, LowerCase(FileName.ToUTF8)) > 0) then - begin - SetLength(Result, i + 1); - Result[i].Name := FileName; - Result[i].IsDirectory := false; - Result[i].IsFile := true; - i := i + 1; - end; - end; - end; -end; - {$IFDEF FPC} function RandomRange(aMin: integer; aMax: integer): integer; begin diff --git a/unicode/src/base/USongs.pas b/unicode/src/base/USongs.pas index 61459918..49b84425 100644 --- a/unicode/src/base/USongs.pas +++ b/unicode/src/base/USongs.pas @@ -81,6 +81,8 @@ type Length: string; end; + TPathDynArray = array of IPath; + {$IFDEF USE_PSEUDO_THREAD} TSongs = class(TPseudoThread) {$ELSE} @@ -105,6 +107,7 @@ type procedure LoadSongList; // load all songs + procedure FindFilesByExtension(const Dir: IPath; const Ext: IPath; Recursive: Boolean; var Files: TPathDynArray); procedure BrowseDir(Dir: IPath); // should return number of songs in the future procedure BrowseTXTFiles(Dir: IPath); procedure BrowseXMLFiles(Dir: IPath); @@ -275,78 +278,86 @@ begin BrowseXMLFiles(Dir); end; -procedure TSongs.BrowseTXTFiles(Dir: IPath); +procedure TSongs.FindFilesByExtension(const Dir: IPath; const Ext: IPath; Recursive: Boolean; var Files: TPathDynArray); var - i: integer; - Files: TDirectoryEntryArray; - lSong: TSong; + Iter: IFileIterator; + FileInfo: TFileInfo; + FileName: IPath; begin - - try - Files := DirectoryFindFiles(Dir, '.txt', true) - except - Log.LogError('Couldn''t deal with directory/file: ' + Dir.ToNative + ' in TSongs.BrowseTXTFiles') - end; - - for i := 0 to Length(Files) - 1 do + // search for all files and directories + Iter := FileSystem.FileFind(Dir.Append('*'), faAnyFile); + while (Iter.HasNext) do begin - if Files[i].IsDirectory then + FileInfo := Iter.Next; + FileName := FileInfo.Name; + if ((FileInfo.Attr and faDirectory) <> 0) then begin - BrowseTXTFiles(Dir.Append(Files[i].Name, pdAppend)); //Recursive Call + if Recursive and (not FileName.Equals('.')) and (not FileName.Equals('..')) then + FindFilesByExtension(Dir.Append(FileName), Ext, true, Files); end else begin - lSong := TSong.Create(Dir.Append(Files[i].Name)); - - if lSong.Analyse then - SongList.add(lSong) - else + if (Ext.Equals(FileName.GetExtension(), true)) then begin - Log.LogError('AnalyseFile failed for "' + Files[i].Name.ToNative + '".'); - FreeAndNil(lSong); + SetLength(Files, Length(Files)+1); + Files[High(Files)] := Dir.Append(FileName); end; - end; end; +end; + +procedure TSongs.BrowseTXTFiles(Dir: IPath); +var + I: integer; + Files: TPathDynArray; + Song: TSong; + Extension: IPath; +begin SetLength(Files, 0); + Extension := Path('.txt'); + FindFilesByExtension(Dir, Extension, true, Files); + + for I := 0 to High(Files) do + begin + Song := TSong.Create(Files[I]); + + if Song.Analyse then + SongList.Add(Song) + else + begin + Log.LogError('AnalyseFile failed for "' + Files[I].ToNative + '".'); + FreeAndNil(Song); + end; + end; + SetLength(Files, 0); end; procedure TSongs.BrowseXMLFiles(Dir: IPath); var - i: integer; - Files: TDirectoryEntryArray; - lSong: TSong; + I: integer; + Files: TPathDynArray; + Song: TSong; + Extension: IPath; begin + SetLength(Files, 0); + Extension := Path('.xml'); + FindFilesByExtension(Dir, Extension, true, Files); - try - Files := DirectoryFindFiles(Dir, '.xml', true) - except - Log.LogError('Couldn''t deal with directory/file: ' + Dir.ToNative + ' in TSongs.BrowseXMLFiles') - end; - - for i := 0 to Length(Files) - 1 do + for I := 0 to High(Files) do begin - if Files[i].IsDirectory then - begin - BrowseXMLFiles(Dir.Append(Files[i].Name, pdAppend)); // Recursive Call - end + Song := TSong.Create(Files[I]); + + if Song.AnalyseXML then + SongList.Add(Song) else begin - lSong := TSong.Create(Dir.Append(Files[i].Name)); - - if lSong.AnalyseXML then - SongList.add(lSong) - else - begin - Log.LogError('AnalyseFile failed for "' + Files[i].Name.ToNative + '".'); - freeandnil(lSong); - end; - + Log.LogError('AnalyseFile failed for "' + Files[I].ToNative + '".'); + FreeAndNil(Song); end; end; - SetLength(Files, 0); + SetLength(Files, 0); end; (* |