Bläddra i källkod

+ Implementation of GetTempDir and GetTempFileName

michael 21 år sedan
förälder
incheckning
dfe2a0417e
3 ändrade filer med 111 tillägg och 5 borttagningar
  1. 71 1
      rtl/objpas/sysutils/osutil.inc
  2. 14 3
      rtl/objpas/sysutils/osutilsh.inc
  3. 26 1
      rtl/unix/sysutils.pp

+ 71 - 1
rtl/objpas/sysutils/osutil.inc

@@ -77,9 +77,79 @@ begin
 end;
 {$endif}
 
+{ ---------------------------------------------------------------------
+  Get temporary directory name
+  ---------------------------------------------------------------------}
+  
+{$ifndef HAS_TEMPDIR} 
+Function GetTempDir(Global : Boolean) : String;
+
+begin
+  If Assigned(OnGetTempDir) then
+    Result:=OnGetTempDir(Global)
+  else
+    begin  
+    Result:=GetEnvironmentVariable('TEMP');
+    If (Result='') Then
+      Result:=GetEnvironmentVariable('TMP');
+    end;  
+  if (Result<>'') then
+    Result:=IncludeTrailingPathDelimiter(Result);
+end;
+{$endif}
+
+Function GetTempDir : String;
+
+begin
+  Result:=GetTempDir(True);
+end;
+
+{ ---------------------------------------------------------------------
+  Get temporary file name
+  ---------------------------------------------------------------------}
+  
+
+{$ifndef HAS_TEMPFILE}
+Function GetTempFileName(Const Dir,Prefix : String) : String; 
+
+Var
+  I : Integer;
+  Start : String;
+ 
+begin
+  If Assigned(OnGetTempFile) then
+    Result:=OnGetTempFile(Dir,Prefix)
+  else
+    begin  
+    If (Dir='') then
+      Start:=GetTempDir
+    else
+      Start:=IncludeTrailingPathDelimiter(Dir);
+    If (Prefix='') then
+      Start:=Start+'TMP'
+    else
+      Start:=Start+Prefix;
+    I:=0;
+    Repeat
+      Result:=Format('%s%.5d.tmp',[Start,I]);
+      Inc(I);
+    Until not FileExists(Result);
+    end;
+end;
+{$endif}
+
+Function GetTempFileName : String;
+
+begin
+  Result:=GetTempFileName('','');
+end;
+
 {  
   $Log$
-  Revision 1.1  2004-08-05 07:28:01  michael
+  Revision 1.2  2004-10-10 10:28:34  michael
+  + Implementation of GetTempDir and GetTempFileName
+
+  Revision 1.1  2004/08/05 07:28:01  michael
   + Added getappconfigdir calls
 
 }

+ 14 - 3
rtl/objpas/sysutils/osutilsh.inc

@@ -26,6 +26,11 @@ procedure Sleep(milliseconds: Cardinal);
 function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString):integer;
 function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString):integer;
 
+
+Function GetTempDir(Global : Boolean) : String;
+Function GetTempDir : String;
+Function GetTempFileName(Const Dir,Prefix : String) : String; 
+Function GetTempFileName : String;
 Function GetAppConfigDir(Global : Boolean) : String;
 Function GetAppConfigFile(Global : Boolean) : String;
 Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
@@ -36,16 +41,22 @@ Const
   SysConfigDir    : String = '';   
   
 Type 
-  TGetAppNameEvent = Function : String;
+  TGetAppNameEvent  = Function : String;
+  TGetTempDirEvent  = Function (Global : Boolean) : String;
+  TGetTempFileEvent = Function (Const Dir,Prefix : String) : String;
 
 Var
   OnGetApplicationName : TGetAppNameEvent;
-
+  OnGetTempDir         : TGetTempDirEvent;
+  OnGetTempFile        : TGetTempFileEvent;
 
 
 {
   $Log$
-  Revision 1.7  2004-08-05 07:28:01  michael
+  Revision 1.8  2004-10-10 10:28:34  michael
+  + Implementation of GetTempDir and GetTempFileName
+
+  Revision 1.7  2004/08/05 07:28:01  michael
   + Added getappconfigdir calls
 
   Revision 1.6  2004/02/13 13:02:21  marco

+ 26 - 1
rtl/unix/sysutils.pp

@@ -24,6 +24,7 @@ interface
 {$DEFINE HAS_SLEEP}
 {$DEFINE HAS_OSERROR}
 {$DEFINE HAS_OSCONFIG}
+{$DEFINE HAS_TEMPDIR}
 {$DEFINE HASUNIX}
 
 uses
@@ -676,6 +677,27 @@ begin
     end;
 end;
 
+{****************************************************************************
+                              Initialization code
+****************************************************************************}
+
+
+Function GetTempDir(Global : Boolean) : String;
+
+begin
+  If Assigned(OnGetTempDir) then
+    Result:=OnGetTempDir(Global)
+  else
+    begin  
+    Result:=GetEnvironmentVariable('TEMP');
+    If (Result='') Then
+      Result:=GetEnvironmentVariable('TMP');
+    if (Result='') then
+      Result:='/tmp/' // fallback.
+    end;  
+  if (Result<>'') then
+    Result:=IncludeTrailingPathDelimiter(Result);
+end;
 
 {****************************************************************************
                               Initialization code
@@ -691,7 +713,10 @@ end.
 {
 
   $Log$
-  Revision 1.46  2004-08-30 11:20:39  michael
+  Revision 1.47  2004-10-10 10:28:34  michael
+  + Implementation of GetTempDir and GetTempFileName
+
+  Revision 1.46  2004/08/30 11:20:39  michael
   + Give path, not comline in ExecuteProcess
 
   Revision 1.45  2004/08/30 11:13:20  michael