DemoUtils.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. unit DemoUtils;
  2. { $I ImagingOptions.inc}
  3. interface
  4. uses
  5. SysUtils,
  6. Classes,
  7. ImagingTypes,
  8. Imaging,
  9. ImagingUtility;
  10. const
  11. SDataDir = 'Data';
  12. type
  13. { Options for BuildFileList function:
  14. flFullNames - file names in result will have full path names
  15. (ExtractFileDir(Path)+FileName)
  16. flRelNames - file names in result will have names relative to
  17. ExtractFileDir(Path) dir
  18. flRecursive - adds files in subdirectories foun in Path.}
  19. TFileListOption = (flFullNames, flRelNames, flRecursive);
  20. TFileListOptions = set of TFileListOption;
  21. { This function fills Files string list with names of files found
  22. with FindFirst/FindNext functions (See details on Path/Atrr here).
  23. - BuildFileList('c:\*.*',faAnyFile, List, [flRecursive]) returns
  24. list of all files (only name.ext - no path) on C drive
  25. - BuildFileList('d:\*.*',faDirectory, List, [flFullNames]) returns
  26. list of all directories (d:\dirxxx) in root of D drive.}
  27. function BuildFileList(Path: string; Attr: LongInt; Files: TStrings;
  28. Options: TFileListOptions = []): Boolean;
  29. { }
  30. function ExpandFileTo(const FileName, BasePath: string): string;
  31. { }
  32. function SwapPathDelims(const FileName: string; const NewDelim: string = PathDelim): string;
  33. { }
  34. function GetDataDir: string;
  35. { }
  36. function GetRootDir: string;
  37. { Returns next valid image format.}
  38. function NextFormat(Format: TImageFormat): TImageFormat;
  39. implementation
  40. function BuildFileList(Path: string; Attr: LongInt;
  41. Files: TStrings; Options: TFileListOptions): Boolean;
  42. var
  43. FileMask: string;
  44. RootDir: string;
  45. Folders: TStringList;
  46. CurrentItem: LongInt;
  47. Counter: LongInt;
  48. LocAttr: LongInt;
  49. procedure BuildFolderList;
  50. var
  51. FindInfo: TSearchRec;
  52. Rslt: LongInt;
  53. begin
  54. Counter := Folders.Count - 1;
  55. CurrentItem := 0;
  56. while CurrentItem <= Counter do
  57. begin
  58. // searching for subfolders
  59. Rslt := FindFirst(Folders[CurrentItem] + '*', faDirectory, FindInfo);
  60. try
  61. while Rslt = 0 do
  62. begin
  63. if (FindInfo.Name <> '.') and (FindInfo.Name <> '..') and
  64. (FindInfo.Attr and faDirectory = faDirectory) then
  65. Folders.Add(Folders[CurrentItem] + FindInfo.Name + PathDelim);
  66. Rslt := FindNext(FindInfo);
  67. end;
  68. finally
  69. FindClose(FindInfo);
  70. end;
  71. Counter := Folders.Count - 1;
  72. Inc(CurrentItem);
  73. end;
  74. end;
  75. procedure FillFileList(CurrentCounter: LongInt);
  76. var
  77. FindInfo: TSearchRec;
  78. Res: LongInt;
  79. CurrentFolder: string;
  80. begin
  81. CurrentFolder := Folders[CurrentCounter];
  82. Res := FindFirst(CurrentFolder + FileMask, LocAttr, FindInfo);
  83. if flRelNames in Options then
  84. CurrentFolder := ExtractRelativePath(RootDir, CurrentFolder);
  85. try
  86. while Res = 0 do
  87. begin
  88. if (FindInfo.Name <> '.') and (FindInfo.Name <> '..') then
  89. begin
  90. if (flFullNames in Options) or (flRelNames in Options) then
  91. Files.Add(CurrentFolder + FindInfo.Name)
  92. else
  93. Files.Add(FindInfo.Name);
  94. end;
  95. Res := FindNext(FindInfo);
  96. end;
  97. finally
  98. FindClose(FindInfo);
  99. end;
  100. end;
  101. begin
  102. FileMask := ExtractFileName(Path);
  103. RootDir := ExtractFilePath(Path);
  104. Folders := TStringList.Create;
  105. Folders.Add(RootDir);
  106. Files.Clear;
  107. {$IFDEF DCC}
  108. {$WARN SYMBOL_PLATFORM OFF}
  109. {$ENDIF}
  110. if Attr = faAnyFile then
  111. LocAttr := faSysFile or faHidden or faArchive or faReadOnly
  112. else
  113. LocAttr := Attr;
  114. {$IFDEF DCC}
  115. {$WARN SYMBOL_PLATFORM ON}
  116. {$ENDIF}
  117. // here's the recursive search for nested folders
  118. if flRecursive in Options then
  119. BuildFolderList;
  120. if Attr <> faDirectory then
  121. for Counter := 0 to Folders.Count - 1 do
  122. FillFileList(Counter)
  123. else
  124. Files.AddStrings(Folders);
  125. Folders.Free;
  126. Result := True;
  127. end;
  128. function ExpandFileTo(const FileName, BasePath: string): string;
  129. var
  130. OldPath: string;
  131. begin
  132. OldPath:= GetCurrentDir;
  133. try
  134. if SysUtils.DirectoryExists(BasePath) then
  135. begin
  136. ChDir(BasePath);
  137. Result:= ExpandFileName(FileName);
  138. end
  139. else
  140. Result:=FileName;
  141. finally
  142. ChDir(OldPath);
  143. end;
  144. end;
  145. function SwapPathDelims(const FileName, NewDelim: string): string;
  146. begin
  147. Result := FileName;
  148. Result := StringReplace(Result, '\', NewDelim, [rfReplaceAll]);
  149. Result := StringReplace(Result, '/', NewDelim, [rfReplaceAll]);
  150. end;
  151. function GetDataDir: string;
  152. begin
  153. Result := GetAppDir + PathDelim + SDataDir;
  154. if not DirectoryExists(Result) then
  155. Result := ExtractFileDir(GetAppDir) + PathDelim + SDataDir;
  156. if not DirectoryExists(Result) then
  157. Result := ExtractFileDir(ExtractFileDir(GetAppDir)) + PathDelim + SDataDir;
  158. end;
  159. function GetRootDir: string;
  160. begin
  161. Result := ExtractFileDir(GetAppDir);
  162. if not DirectoryExists(Result + PathDelim + 'Source') then
  163. begin
  164. Result := ExtractFileDir(Result);
  165. if not DirectoryExists(Result + PathDelim + 'Source') then
  166. begin
  167. Result := ExtractFileDir(Result);
  168. if not DirectoryExists(Result + PathDelim + 'Source') then
  169. begin
  170. Result := ExtractFileDir(Result);
  171. if not DirectoryExists(Result + PathDelim + 'Source') then
  172. Result := ExtractFileDir(Result);
  173. end;
  174. end;
  175. end;
  176. end;
  177. function NextFormat(Format: TImageFormat): TImageFormat;
  178. var
  179. Info: TImageFormatInfo;
  180. begin
  181. repeat
  182. if Format < High(TImageFormat) then
  183. {$IFDEF DCC}
  184. Format := Succ(Format)
  185. {$ELSE}
  186. Format := TImageFormat(Succ(LongInt(Format)))
  187. {$ENDIF}
  188. else
  189. Format := ifIndex8;
  190. until GetImageFormatInfo(Format, Info);
  191. Result := Format;
  192. end;
  193. end.