1
0
mirror of synced 2024-12-22 17:23:07 +01:00

Added: X2UtTempFile

Fixed: no inline directives for older Delphi versions
This commit is contained in:
Mark van Renswoude 2008-02-12 10:20:06 +00:00
parent 0a35f987f0
commit 5556e69a2d
2 changed files with 128 additions and 2 deletions

View File

@ -56,10 +56,10 @@ uses
function InRange(const AValue, AMin, AMax: Int64): Int64; overload; function InRange(const AValue, AMin, AMax: Int64): Int64; overload;
//:$ Returns the width of a rectangle //:$ Returns the width of a rectangle
function RectWidth(const ARect: TRect): Integer; inline; function RectWidth(const ARect: TRect): Integer; {$IFDEF VER180}inline;{$ENDIF}
//:$ Returns the height of a rectangle //:$ Returns the height of a rectangle
function RectHeight(const ARect: TRect): Integer; inline; function RectHeight(const ARect: TRect): Integer; {$IFDEF VER180}inline;{$ENDIF}
implementation implementation

126
X2UtTempFile.pas Normal file
View File

@ -0,0 +1,126 @@
{: 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;
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;
function GetTempFile(const APath, AFileName, AExtension: String): String; overload;
var
iCounter: Integer;
sBase: String;
sExtension: String;
begin
iCounter := 0;
sBase := IncludeTrailingPathDelimiter(APath);
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;
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;
function GetTempAppDataFile(const APath, AFileName, AExtension: String): String; overload;
begin
Result := GetTempFile(GetAppDataPath + APath, AFileName, AExtension);
end;
function GetTempAppDataFile(const APath, AFileName: String): String; overload;
var
sExt: String;
begin
sExt := ExtractFileExt(AFileName);
Result := GetTempAppDataFile(APath, Copy(AFileName, 1, Length(AFileName) - Length(sExt)),
sExt);
end;
function IsValidFileChar(const AChar: Char): Boolean;
begin
Result := not (AChar in ['\', '/', ':', '*', '?', '"', '<', '>', '|']);
end;
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.