BaseUtils.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. unit BaseUtils;
  2. {==================================================================
  3. Basic file handling utilities / Version 1.2 / 11.1999
  4. ========================================================
  5. Description:
  6. ============
  7. Basic utilities useful for handling files or directories.
  8. Used by the components TDriveView and TDirView.
  9. Author:
  10. =======
  11. (c) Ingo Eckel 5/1998
  12. Sodener Weg 38
  13. 65812 Bad Soden
  14. Germany
  15. Modifications (for WinSCP):
  16. ===========================
  17. (c) Martin Prikryl 2001, 2002
  18. {==================================================================}
  19. interface
  20. uses
  21. SysUtils, Windows, Forms, ShlObj, PIDL, Classes, Controls;
  22. type
  23. TDateTimePrecision = (tpNone, tpDay, tpMinute, tpSecond, tpMillisecond);
  24. // order choosen so that for previous bool value, false maps to fbNone,
  25. // and true maps to new default fbKilobytes, although functionaly it is fbShort
  26. TFormatBytesStyle = (fbNone, fbKilobytes, fbShort);
  27. function ExtractFileNameOnly(Name: string): string;
  28. function FileOrDirExists(FileName: string): Boolean;
  29. function FormatBytes(Bytes: Int64; Style: TFormatBytesStyle = fbShort; UseUnitsForBytes: Boolean = True): string;
  30. function FormatPanelBytes(Bytes: Int64; Style: TFormatBytesStyle): string;
  31. procedure FreePIDL(var PIDL: PItemIDList);
  32. function StrContains(Str1, Str2: string): Boolean;
  33. function IsUncPath(Path: string): Boolean;
  34. procedure ReduceDateTimePrecision(var DateTime: TDateTime;
  35. Precision: TDateTimePrecision);
  36. function SpecialFolderLocation(Folder: Integer; var Path: string;
  37. var PIDL: PItemIDList): Boolean; overload;
  38. function SpecialFolderLocation(Folder: Integer; var Path: string): Boolean; overload;
  39. function FormatLastOSError(Message: string): string;
  40. resourcestring
  41. SNoValidPath = 'Can''t find any valid path.';
  42. SUcpPathsNotSupported = 'UNC paths are not supported.';
  43. SByte = 'B';
  44. SKiloByte = 'KB';
  45. SMegaByte = 'MB';
  46. SGigaByte = 'GB';
  47. implementation
  48. uses
  49. IEDriveInfo, DateUtils, ShellApi, SysConst, PasTools, Math;
  50. function IsUncPath(Path: string): Boolean;
  51. begin
  52. Result := (Copy(Path, 1, 2) = '\\') or (Copy(Path, 1, 2) = '//');
  53. end;
  54. function StrContains(Str1, Str2: string): Boolean;
  55. var
  56. Index: Integer;
  57. begin
  58. for Index := 1 to Length(Str1) do
  59. if Pos(Str1[Index], Str2) <> 0 then
  60. begin
  61. Result := True;
  62. Exit;
  63. end;
  64. Result := False;
  65. end; {StringCountains}
  66. function FileOrDirExists(FileName: string): Boolean;
  67. begin
  68. Result := FileExists(FileName) or DirectoryExists(FileName);
  69. end; {FileOrDirExists}
  70. function ExtractFileNameOnly(Name: string): string;
  71. var
  72. Ext: string;
  73. begin
  74. Result := ExtractFileName(Name);
  75. Ext := ExtractFileExt(Name);
  76. if Ext <> '' then
  77. Delete(Result, Length(Result)-Length(Ext)+1, Length(Ext));
  78. end; {ExtractFileNameOnly}
  79. // Windows Explorer size formatting
  80. // size properties/status bar size column
  81. // 1023 1023 bytes 1 KB
  82. // 1 KB 1,00 KB 1 KB
  83. // 2 KB 2,00 KB 2 KB
  84. // 2 KB + 1 B 2,00 KB 3 KB
  85. // 2 KB + 12 B 2,01 KB 3 KB
  86. // 10 KB - 1B 9,99 KB 10 KB
  87. // 10 KB 10,0 KB 10 KB
  88. // 12 KB 12,0 KB 12 KB
  89. // 12 KB + 1 B 12,0 KB 13 KB
  90. // 12 KB + 12 B 12,0 KB 13 KB
  91. // 12 KB + 128 B 12,1 KB 13 KB
  92. // 100 KB 100 KB 100 KB
  93. // 100 KB + 1 B 100 KB 101 KB
  94. // 500 KB 500 KB 500 KB
  95. // 1000 KB - 1 B 999 KB 1 000 KB
  96. // 1000 KB 0,97 MB 1 000 KB
  97. // 1 MB 1,00 MB 1 024 KB
  98. // 1000 MB - 1 B 999 MB 1 024 000 KB
  99. // 1000 MB 0,97 GB 1 024 000 KB
  100. // 1 GB - 1 0,99 GB 1 048 576 KB
  101. // 1 GB 1,00 GB 1 048 576 KB
  102. // 1 GB + 10 MB 1,00 GB 1 058 816 KB
  103. // 1 GB + 12 MB 1,01 GB 1 060 864 KB
  104. function FormatBytes(Bytes: Int64; Style: TFormatBytesStyle; UseUnitsForBytes: Boolean): string;
  105. var
  106. SizeUnit: string;
  107. Value: Int64;
  108. Order: Int64;
  109. EValue: Extended;
  110. Format: string;
  111. begin
  112. if (Style = fbNone) or ((Style = fbShort) and (Bytes < Int64(1024))) then
  113. begin
  114. EValue := Bytes;
  115. Format := '#,##0';
  116. if UseUnitsForBytes then
  117. Format := Format + ' "' + SByte + '"';
  118. end
  119. else
  120. if Style = fbKilobytes then
  121. begin
  122. Value := Bytes div 1024;
  123. if (Bytes mod 1024) > 0 then
  124. Inc(Value);
  125. EValue := Value;
  126. Format := '#,##0 "' + SKiloByte + '"';
  127. end
  128. else
  129. begin
  130. if Bytes < Int64(1000*1024) then
  131. begin
  132. Order := 1024;
  133. SizeUnit := SKiloByte;
  134. end
  135. else
  136. if Bytes < Int64(1000*1024*1024) then
  137. begin
  138. Order := 1024*1024;
  139. SizeUnit := SMegaByte;
  140. end
  141. else
  142. begin
  143. Order := 1024*1024*1024;
  144. SizeUnit := SGigaByte;
  145. end;
  146. EValue := Bytes / Order;
  147. if EValue >= 100 then
  148. begin
  149. EValue := Floor(EValue);
  150. Format := '#,##0';
  151. end
  152. else
  153. if EValue >= 10 then
  154. begin
  155. EValue := Floor(EValue * 10) / 10;
  156. Format := '#0.0';
  157. end
  158. else
  159. begin
  160. EValue := Floor(EValue * 100) / 100;
  161. Format := '0.00';
  162. end;
  163. Format := Format + ' "' + SizeUnit + '"';
  164. end;
  165. Result := FormatFloat(Format, EValue);
  166. end;
  167. function FormatPanelBytes(Bytes: Int64; Style: TFormatBytesStyle): string;
  168. begin
  169. Result := FormatBytes(Bytes, Style, (Style <> fbNone));
  170. end;
  171. procedure FreePIDL(var PIDL: PItemIDList);
  172. begin
  173. if PIDL <> nil then
  174. begin
  175. try
  176. ShellMalloc.Free(PIDL);
  177. PIDL := NIL;
  178. except
  179. PIDL := NIL;
  180. end;
  181. end;
  182. end; {FreePIDL}
  183. // duplicated in RemoteFiles.cpp
  184. procedure ReduceDateTimePrecision(var DateTime: TDateTime;
  185. Precision: TDateTimePrecision);
  186. var
  187. Y, M, D, H, N, S, MS: Word;
  188. begin
  189. if Precision = tpNone then DateTime := 0
  190. else
  191. if Precision <> tpMillisecond then
  192. begin
  193. DecodeDateTime(DateTime, Y, M, D, H, N, S, MS);
  194. case Precision of
  195. tpDay:
  196. begin
  197. H := 0;
  198. N := 0;
  199. S := 0;
  200. MS := 0;
  201. end;
  202. tpMinute:
  203. begin
  204. S := 0;
  205. MS := 0;
  206. end;
  207. tpSecond:
  208. begin
  209. MS := 0;
  210. end;
  211. end;
  212. DateTime := EncodeDate(Y, M, D) + EncodeTime(H, N, S, MS);
  213. end;
  214. end;
  215. function SpecialFolderLocation(Folder: Integer; var Path: string;
  216. var PIDL: PItemIDList): Boolean;
  217. begin
  218. SetLength(Path, MAX_PATH);
  219. Result :=
  220. (not Failed(SHGetSpecialFolderLocation(Application.Handle, Folder, PIDL))) and
  221. SHGetPathFromIDList(PIDL, PChar(Path));
  222. if Result then
  223. begin
  224. SetLength(Path, StrLen(PChar(Path)));
  225. end
  226. else
  227. begin
  228. Path := '';
  229. end;
  230. end;
  231. function SpecialFolderLocation(Folder: Integer; var Path: string): Boolean;
  232. var
  233. PIDL: PItemIDList;
  234. begin
  235. Result := SpecialFolderLocation(Folder, Path, PIDL);
  236. end;
  237. function FormatLastOSError(Message: string): string;
  238. var
  239. LastError: Integer;
  240. begin
  241. Result := Message;
  242. LastError := GetLastError;
  243. if LastError <> 0 then
  244. Result := Result + #13#10 + #13#10 + Format(SOSError, [LastError, SysErrorMessage(LastError), '']);
  245. end;
  246. initialization
  247. end.