-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtempfiles.pas
66 lines (50 loc) · 1.14 KB
/
tempfiles.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
unit TempFiles;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TTempFileStream }
TTempFileStream = class(TFileStream)
public
constructor Create;
constructor Create(Rights: Cardinal);
destructor Destroy; override;
end;
var
{$IFDEF Unix} TempFileDir: String = '/tmp/'; {$ENDIF}
{$IFDEF Windows} TempFileDir: String = 'C:\Temp\'; {$ENDIF}
implementation
function RandomFilename(Len: Byte): String;
var
B: Byte;
begin
Result := '';
for B := 1 to Len do
Result := Result + chr(Random(52) + 65);
end;
function TempFilename: String;
begin
repeat
Result := TempFileDir + RandomFilename(16);
until not FileExists(Result);
end;
{ TTempFileStream }
constructor TTempFileStream.Create;
begin
if not DirectoryExists(TempFileDir) then
CreateDir(TempFileDir);
inherited Create(TempFilename,fmCreate);
end;
constructor TTempFileStream.Create(Rights: Cardinal);
begin
if not DirectoryExists(TempFileDir) then
CreateDir(TempFileDir);
inherited Create(TempFilename,fmCreate,Rights);
end;
destructor TTempFileStream.Destroy;
begin
inherited Destroy;
DeleteFile(Filename);
end;
end.