osutil.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. <What does this file>
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { ---------------------------------------------------------------------
  13. Environment variable auxiliary routines
  14. ---------------------------------------------------------------------}
  15. Const
  16. FPC_EnvCount : Integer = -1;
  17. Function FPCCountEnvVar(EP : PPChar) : integer;
  18. begin
  19. If (FPC_EnvCount=-1) then
  20. begin
  21. FPC_EnvCount:=0;
  22. If (EP<>Nil) then
  23. While (EP^<>Nil) do
  24. begin
  25. Inc(FPC_EnvCount);
  26. Inc(EP);
  27. end;
  28. end;
  29. Result:=FPC_EnvCount;
  30. end;
  31. Function FPCGetEnvVarFromP(EP : PPChar; EnvVar : String) : String;
  32. var
  33. hp : ppchar;
  34. lenvvar,hs : string;
  35. eqpos : longint;
  36. begin
  37. lenvvar:=upcase(envvar);
  38. hp:=EP;
  39. Result:='';
  40. If (hp<>Nil) then
  41. while assigned(hp^) do
  42. begin
  43. hs:=strpas(hp^);
  44. eqpos:=pos('=',hs);
  45. if upcase(copy(hs,1,eqpos-1))=lenvvar then
  46. begin
  47. Result:=copy(hs,eqpos+1,length(hs)-eqpos);
  48. exit;
  49. end;
  50. inc(hp);
  51. end;
  52. end;
  53. Function FPCGetEnvStrFromP(EP : PPChar; Index : Integer) : String;
  54. begin
  55. Result:='';
  56. while assigned(EP^) and (Index>1) do
  57. begin
  58. Dec(Index);
  59. inc(EP);
  60. end;
  61. If Assigned(EP^) then
  62. Result:=StrPas(EP^);
  63. end;
  64. { ---------------------------------------------------------------------
  65. Application name
  66. ---------------------------------------------------------------------}
  67. Function ApplicationName : String;
  68. begin
  69. If Assigned(OnGetApplicationName) then
  70. Result:=OnGetApplicationName()
  71. else
  72. Result:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
  73. end;
  74. { ---------------------------------------------------------------------
  75. Default implementations for AppConfigDir implementation.
  76. ---------------------------------------------------------------------}
  77. Function DGetAppConfigDir(Global : Boolean) : String;
  78. begin
  79. Result:=ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
  80. end;
  81. Function DGetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  82. begin
  83. Result:=ExtractFilePath(ParamStr(0));
  84. If SubDir then
  85. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  86. Result:=Result+ApplicationName+ConfigExtension;
  87. end;
  88. Function GetAppConfigFile(Global : Boolean) : String;
  89. begin
  90. Result:=GetAppConfigFile(Global,False);
  91. end;
  92. { ---------------------------------------------------------------------
  93. Fallback implementations for AppConfigDir implementation.
  94. ---------------------------------------------------------------------}
  95. {
  96. If a particular OS does it different:
  97. - set the HAVE_OSCONFIG define before including sysutils.inc.
  98. - implement the functions.
  99. Default config assumes a DOS-like configuration.
  100. }
  101. {$ifndef HAS_OSCONFIG}
  102. Function GetAppConfigDir(Global : Boolean) : String;
  103. begin
  104. Result:=DGetAppConfigDir(Global);
  105. end;
  106. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  107. begin
  108. Result:=DGetAppConfigFile(Global,Subdir);
  109. end;
  110. {$endif}
  111. { ---------------------------------------------------------------------
  112. Get temporary directory name
  113. ---------------------------------------------------------------------}
  114. {$ifndef HAS_TEMPDIR}
  115. Function GetTempDir(Global : Boolean) : String;
  116. begin
  117. If Assigned(OnGetTempDir) then
  118. Result:=OnGetTempDir(Global)
  119. else
  120. begin
  121. Result:=GetEnvironmentVariable('TEMP');
  122. If (Result='') Then
  123. Result:=GetEnvironmentVariable('TMP');
  124. end;
  125. if (Result<>'') then
  126. Result:=IncludeTrailingPathDelimiter(Result);
  127. end;
  128. {$endif}
  129. Function GetTempDir : String;
  130. begin
  131. Result:=GetTempDir(True);
  132. end;
  133. { ---------------------------------------------------------------------
  134. Get temporary file name
  135. ---------------------------------------------------------------------}
  136. {$ifndef HAS_TEMPFILE}
  137. Function GetTempFileName(Const Dir,Prefix : String) : String;
  138. Var
  139. I : Integer;
  140. Start : String;
  141. begin
  142. If Assigned(OnGetTempFile) then
  143. Result:=OnGetTempFile(Dir,Prefix)
  144. else
  145. begin
  146. If (Dir='') then
  147. Start:=GetTempDir
  148. else
  149. Start:=IncludeTrailingPathDelimiter(Dir);
  150. If (Prefix='') then
  151. Start:=Start+'TMP'
  152. else
  153. Start:=Start+Prefix;
  154. I:=0;
  155. Repeat
  156. Result:=Format('%s%.5d.tmp',[Start,I]);
  157. Inc(I);
  158. Until not FileExists(Result);
  159. end;
  160. end;
  161. {$endif}
  162. Function GetTempFileName : String;
  163. begin
  164. Result:=GetTempFileName('','');
  165. end;
  166. {
  167. $Log$
  168. Revision 1.3 2004-12-11 11:33:15 michael
  169. + Added GetEnvironmentVariableCount and GetEnvironmentString calls
  170. Revision 1.2 2004/10/10 10:28:34 michael
  171. + Implementation of GetTempDir and GetTempFileName
  172. Revision 1.1 2004/08/05 07:28:01 michael
  173. + Added getappconfigdir calls
  174. }