BaseUtils.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 CheckFileExists(FileName: string): Boolean;
  28. function DirExists(Dir: string): Boolean; overload;
  29. function DirExists(Dir: string; var Attrs: Integer): Boolean; overload;
  30. function ExtractFileNameOnly(Name: string): string;
  31. function FileOrDirExists(FileName: string): Boolean;
  32. function FormatBytes(Bytes: Int64; Style: TFormatBytesStyle = fbShort; UseUnitsForBytes: Boolean = True): string;
  33. function FormatPanelBytes(Bytes: Int64; Style: TFormatBytesStyle): string;
  34. procedure FreePIDL(var PIDL: PItemIDList);
  35. function StrContains(Str1, Str2: string): Boolean;
  36. procedure StrTranslate(var Str: string; Code: string);
  37. function IsUncPath(Path: string): Boolean;
  38. function AnyValidPath: string;
  39. procedure ReduceDateTimePrecision(var DateTime: TDateTime;
  40. Precision: TDateTimePrecision);
  41. function SpecialFolderLocation(Folder: Integer; var Path: string;
  42. var PIDL: PItemIDList): Boolean; overload;
  43. function SpecialFolderLocation(Folder: Integer; var Path: string): Boolean; overload;
  44. function FormatLastOSError(Message: string): string;
  45. resourcestring
  46. SNoValidPath = 'Can''t find any valid path.';
  47. SUcpPathsNotSupported = 'UNC paths are not supported.';
  48. SByte = 'B';
  49. SKiloByte = 'KB';
  50. SMegaByte = 'MB';
  51. SGigaByte = 'GB';
  52. implementation
  53. uses
  54. IEDriveInfo, DateUtils, ShellApi, SysConst, PasTools, Math;
  55. function AnyValidPath: string;
  56. var
  57. Drive: TDrive;
  58. begin
  59. for Drive := 'C' to 'Z' do
  60. if DriveInfo[Drive].Valid and
  61. (DriveInfo[Drive].DriveType = DRIVE_FIXED) and
  62. DirectoryExists(ApiPath(Drive + ':\')) then
  63. begin
  64. Result := Drive + ':\';
  65. Exit;
  66. end;
  67. for Drive := 'C' to 'Z' do
  68. if DriveInfo[Drive].Valid and
  69. (DriveInfo[Drive].DriveType = DRIVE_REMOTE) and
  70. DirectoryExists(ApiPath(Drive + ':\')) then
  71. begin
  72. Result := Drive + ':\';
  73. Exit;
  74. end;
  75. raise Exception.Create(SNoValidPath);
  76. end;
  77. function IsUncPath(Path: string): Boolean;
  78. begin
  79. Result := (Copy(Path, 1, 2) = '\\') or (Copy(Path, 1, 2) = '//');
  80. end;
  81. procedure StrTranslate(var Str: string; Code: string);
  82. var
  83. Index: Integer;
  84. begin
  85. if (Length(Code) > 0) and (Length(Code) mod 2 = 0) then
  86. for Index := 1 to Length(Code) div 2 do
  87. while Pos(Code[Index*2-1], Str) <> 0 do
  88. Str[Pos(Code[Index*2-1], Str)] := Code[Index*2];
  89. end; {StrTranslate}
  90. function StrContains(Str1, Str2: string): Boolean;
  91. var
  92. Index: Integer;
  93. begin
  94. for Index := 1 to Length(Str1) do
  95. if Pos(Str1[Index], Str2) <> 0 then
  96. begin
  97. Result := True;
  98. Exit;
  99. end;
  100. Result := False;
  101. end; {StringCountains}
  102. function FileOrDirExists(FileName: string): Boolean;
  103. var
  104. SRec : TSearchRec;
  105. begin
  106. if Length(FileName) = 0 then Result := False
  107. else
  108. begin
  109. Result := (FindFirst(ApiPath(FileName), faAnyFile, SRec) = 0);
  110. SysUtils.FindCLose(SRec);
  111. end;
  112. end; {FileOrDirExists}
  113. function CheckFileExists(FileName: string): Boolean;
  114. var
  115. SaveFileMode : Integer;
  116. F: file;
  117. begin
  118. SaveFileMode := System.FileMode;
  119. System.FileMode := 0;
  120. try
  121. AssignFile(F, ApiPath(FileName));
  122. Reset(F, 1);
  123. Result := IOResult = 0;
  124. if Result then
  125. CloseFile(F);
  126. finally
  127. System.FileMode := SaveFileMode;
  128. end;
  129. end; {CheckFileExists}
  130. function DirExists(Dir: string; var Attrs: Integer): Boolean;
  131. var
  132. SRec: TSearchRec;
  133. begin
  134. Result := ((Length(Dir) <= 3) and (Length(Dir) >= 2)) and (Dir[2] = ':');
  135. if Result then Attrs := 0
  136. else
  137. begin
  138. if FindFirst(ApiPath(Dir), faAnyFile, SRec) = 0 then
  139. begin
  140. Result := (SRec.Attr and faDirectory <> 0);
  141. Attrs := SRec.Attr;
  142. end;
  143. SysUtils.FindClose(SRec);
  144. end;
  145. end; {DirExists}
  146. function DirExists(Dir: string): Boolean;
  147. var
  148. Dummy: Integer;
  149. begin
  150. Result := DirExists(Dir, Dummy);
  151. end; {DirExists}
  152. function ExtractFileNameOnly(Name: string): string;
  153. var
  154. Ext: string;
  155. begin
  156. Result := ExtractFileName(Name);
  157. Ext := ExtractFileExt(Name);
  158. if Ext <> '' then
  159. Delete(Result, Length(Result)-Length(Ext)+1, Length(Ext));
  160. end; {ExtractFileNameOnly}
  161. function FormatBytes(Bytes: Int64; Style: TFormatBytesStyle; UseUnitsForBytes: Boolean): string;
  162. var
  163. SizeUnit: string;
  164. Value: Int64;
  165. Order: Int64;
  166. EValue: Extended;
  167. begin
  168. if (Style = fbNone) or ((Style = fbShort) and (Bytes < Int64(100*1024))) then
  169. begin
  170. Order := 1;
  171. if UseUnitsForBytes then
  172. SizeUnit := SByte;
  173. end
  174. else
  175. if (Style = fbKilobytes) or (Bytes < Int64(100*1024*1024)) then
  176. begin
  177. Order := 1024;
  178. SizeUnit := SKiloByte;
  179. end
  180. else
  181. if Bytes < Int64(Int64(100)*1024*1024*1024) then
  182. begin
  183. Order := 1024*1024;
  184. SizeUnit := SMegaByte;
  185. end
  186. else
  187. begin
  188. Order := 1024*1024*1024;
  189. SizeUnit := SGigaByte;
  190. end;
  191. Value := Bytes div Order;
  192. if (Bytes mod Order) > 0 then
  193. Inc(Value);
  194. EValue := Value;
  195. Result := FormatFloat('#,##0', EValue);
  196. if SizeUnit <> '' then
  197. Result := Result + ' ' + SizeUnit;
  198. end;
  199. function FormatPanelBytes(Bytes: Int64; Style: TFormatBytesStyle): string;
  200. begin
  201. Result := FormatBytes(Bytes, Style, (Style <> fbNone));
  202. end;
  203. procedure FreePIDL(var PIDL: PItemIDList);
  204. begin
  205. if PIDL <> nil then
  206. begin
  207. try
  208. ShellMalloc.Free(PIDL);
  209. PIDL := NIL;
  210. except
  211. PIDL := NIL;
  212. end;
  213. end;
  214. end; {FreePIDL}
  215. // duplicated in RemoteFiles.cpp
  216. procedure ReduceDateTimePrecision(var DateTime: TDateTime;
  217. Precision: TDateTimePrecision);
  218. var
  219. Y, M, D, H, N, S, MS: Word;
  220. begin
  221. if Precision = tpNone then DateTime := 0
  222. else
  223. if Precision <> tpMillisecond then
  224. begin
  225. DecodeDateTime(DateTime, Y, M, D, H, N, S, MS);
  226. case Precision of
  227. tpDay:
  228. begin
  229. H := 0;
  230. N := 0;
  231. S := 0;
  232. MS := 0;
  233. end;
  234. tpMinute:
  235. begin
  236. S := 0;
  237. MS := 0;
  238. end;
  239. tpSecond:
  240. begin
  241. MS := 0;
  242. end;
  243. end;
  244. DateTime := EncodeDate(Y, M, D) + EncodeTime(H, N, S, MS);
  245. end;
  246. end;
  247. function SpecialFolderLocation(Folder: Integer; var Path: string;
  248. var PIDL: PItemIDList): Boolean;
  249. begin
  250. SetLength(Path, MAX_PATH);
  251. Result :=
  252. (not Failed(SHGetSpecialFolderLocation(Application.Handle, Folder, PIDL))) and
  253. SHGetPathFromIDList(PIDL, PChar(Path));
  254. if Result then SetLength(Path, StrLen(PChar(Path)));
  255. end;
  256. function SpecialFolderLocation(Folder: Integer; var Path: string): Boolean;
  257. var
  258. PIDL: PItemIDList;
  259. begin
  260. Result := SpecialFolderLocation(Folder, Path, PIDL);
  261. end;
  262. function FormatLastOSError(Message: string): string;
  263. var
  264. LastError: Integer;
  265. begin
  266. Result := Message;
  267. LastError := GetLastError;
  268. if LastError <> 0 then
  269. Result := Result + #13#10 + #13#10 + Format(SOSError, [LastError, SysErrorMessage(LastError), '']);
  270. end;
  271. initialization
  272. end.