2008-02-12 10:20:06 +00:00
|
|
|
{: Provides temporary file functions.
|
|
|
|
|
|
|
|
Last changed: $Date$
|
|
|
|
Revision: $Rev$
|
|
|
|
Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtTempFile;
|
|
|
|
|
|
|
|
interface
|
|
|
|
function GetAppDataPath(): String;
|
|
|
|
|
|
|
|
function GetTempFile(const APrefix: String): String; overload;
|
|
|
|
function GetTempFile(const APath, AFileName, AExtension: String): String; overload;
|
|
|
|
function GetTempFile(const APath, AFileName: String): String; overload;
|
|
|
|
function GetTempAppDataFile(const ASubPath, AFileName, AExtension: String): String; overload;
|
|
|
|
function GetTempAppDataFile(const ASubPath, AFileName: String): String; overload;
|
|
|
|
|
|
|
|
function IsValidFileChar(const AChar: Char): Boolean;
|
|
|
|
function CheckValidFileName(var AFileName: String; const AReplacement: Char = #0): Boolean;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
ShlObj,
|
|
|
|
SysUtils,
|
|
|
|
Windows;
|
|
|
|
|
|
|
|
|
2013-04-09 14:48:41 +00:00
|
|
|
{$I X2UtCompilerVersion.inc}
|
|
|
|
|
2008-02-12 10:20:06 +00:00
|
|
|
|
|
|
|
function GetAppDataPath(): String;
|
|
|
|
var
|
|
|
|
path: array[0..MAX_PATH] of Char;
|
|
|
|
|
|
|
|
begin
|
|
|
|
FillChar(path, SizeOf(path), #0);
|
|
|
|
if not SHGetSpecialFolderPath(0, @path, CSIDL_APPDATA, True) then
|
|
|
|
begin
|
|
|
|
FillChar(path, SizeOf(path), #0);
|
|
|
|
GetTempPath(SizeOf(path), @path);
|
|
|
|
end;
|
|
|
|
|
|
|
|
Result := path;
|
|
|
|
if Length(Result) > 0 then
|
|
|
|
Result := IncludeTrailingPathDelimiter(Result);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-03-17 09:33:24 +00:00
|
|
|
function GetTempFile(const APrefix: String): String; overload;
|
|
|
|
var
|
|
|
|
tempPath: array[0..MAX_PATH] of Char;
|
|
|
|
tempFile: array[0..MAX_PATH] of Char;
|
|
|
|
|
|
|
|
begin
|
|
|
|
FillChar(tempPath, SizeOf(tempPath), #0);
|
|
|
|
FillChar(tempFile, SizeOf(tempFile), #0);
|
|
|
|
|
|
|
|
Windows.GetTempPath(SizeOf(tempPath), @tempPath);
|
|
|
|
Windows.GetTempFileName(@tempPath, PChar(APrefix), 0, @tempFile);
|
|
|
|
|
|
|
|
Result := String(tempFile);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2008-02-12 10:20:06 +00:00
|
|
|
function GetTempFile(const APath, AFileName, AExtension: String): String; overload;
|
|
|
|
var
|
|
|
|
iCounter: Integer;
|
|
|
|
sBase: String;
|
|
|
|
sExtension: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
iCounter := 0;
|
|
|
|
sBase := IncludeTrailingPathDelimiter(APath);
|
2008-03-17 09:33:24 +00:00
|
|
|
|
2008-02-12 10:20:06 +00:00
|
|
|
if not ForceDirectories(sBase) then
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
|
|
|
sExtension := AExtension;
|
|
|
|
if (Length(sExtension) > 0) and (AnsiPos('.', sExtension) = 0) then
|
|
|
|
sExtension := '.' + sExtension;
|
|
|
|
|
|
|
|
sBase := sBase + AFileName;
|
|
|
|
Result := sBase + sExtension;
|
|
|
|
|
|
|
|
while FileExists(Result) do
|
|
|
|
begin
|
|
|
|
Inc(iCounter);
|
|
|
|
Result := Format('%s(%d)%s', [sBase, iCounter, sExtension]);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-03-17 09:33:24 +00:00
|
|
|
|
2008-02-12 10:20:06 +00:00
|
|
|
function GetTempFile(const APath, AFileName: String): String; overload;
|
|
|
|
var
|
|
|
|
sExt: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
sExt := ExtractFileExt(AFileName);
|
|
|
|
Result := GetTempFile(APath, Copy(AFileName, 1, Length(AFileName) - Length(sExt)),
|
|
|
|
sExt);
|
|
|
|
end;
|
|
|
|
|
2008-03-17 09:33:24 +00:00
|
|
|
|
|
|
|
function GetTempAppDataFile(const ASubPath, AFileName, AExtension: String): String; overload;
|
2008-02-12 10:20:06 +00:00
|
|
|
begin
|
2008-03-17 09:33:24 +00:00
|
|
|
Result := GetTempFile(GetAppDataPath + ASubPath, AFileName, AExtension);
|
2008-02-12 10:20:06 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-17 09:33:24 +00:00
|
|
|
|
|
|
|
function GetTempAppDataFile(const ASubPath, AFileName: String): String; overload;
|
2008-02-12 10:20:06 +00:00
|
|
|
var
|
|
|
|
sExt: String;
|
|
|
|
|
|
|
|
begin
|
|
|
|
sExt := ExtractFileExt(AFileName);
|
2008-03-17 09:33:24 +00:00
|
|
|
Result := GetTempAppDataFile(ASubPath, Copy(AFileName, 1,
|
|
|
|
Length(AFileName) - Length(sExt)),
|
|
|
|
sExt);
|
2008-02-12 10:20:06 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function IsValidFileChar(const AChar: Char): Boolean;
|
|
|
|
begin
|
2013-04-09 14:48:41 +00:00
|
|
|
{$IFDEF DXE2PLUS}
|
|
|
|
Result := not CharInSet(AChar, ['\', '/', ':', '*', '?', '"', '<', '>', '|']);
|
|
|
|
{$ELSE}
|
2008-02-12 10:20:06 +00:00
|
|
|
Result := not (AChar in ['\', '/', ':', '*', '?', '"', '<', '>', '|']);
|
2013-04-09 14:48:41 +00:00
|
|
|
{$ENDIF}
|
2008-02-12 10:20:06 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-17 09:33:24 +00:00
|
|
|
|
2008-02-12 10:20:06 +00:00
|
|
|
function CheckValidFileName(var AFileName: String; const AReplacement: Char): Boolean;
|
|
|
|
var
|
|
|
|
iPos: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
|
|
|
|
for iPos := Length(AFileName) downto 1 do
|
|
|
|
if not IsValidFileChar(AFileName[iPos]) then
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
if AReplacement = #0 then
|
|
|
|
Delete(AFileName, iPos, 1)
|
|
|
|
else
|
|
|
|
AFileName[iPos] := AReplacement;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|