From 5556e69a2dc9c31c021e06f943b1f00753b7cdc2 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 12 Feb 2008 10:20:06 +0000 Subject: [PATCH] Added: X2UtTempFile Fixed: no inline directives for older Delphi versions --- X2UtMisc.pas | 4 +- X2UtTempFile.pas | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 X2UtTempFile.pas diff --git a/X2UtMisc.pas b/X2UtMisc.pas index c11ceef..f18eab9 100644 --- a/X2UtMisc.pas +++ b/X2UtMisc.pas @@ -56,10 +56,10 @@ uses function InRange(const AValue, AMin, AMax: Int64): Int64; overload; //:$ 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 - function RectHeight(const ARect: TRect): Integer; inline; + function RectHeight(const ARect: TRect): Integer; {$IFDEF VER180}inline;{$ENDIF} implementation diff --git a/X2UtTempFile.pas b/X2UtTempFile.pas new file mode 100644 index 0000000..478f5d2 --- /dev/null +++ b/X2UtTempFile.pas @@ -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.