osutil.inc 5.2 KB

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