osutil.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 VendorName : String;
  67. begin
  68. If Assigned(OnGetVendorName) then
  69. Result:=OnGetVendorName()
  70. else
  71. Result:='';
  72. end;
  73. Function ApplicationName : String;
  74. begin
  75. If Assigned(OnGetApplicationName) then
  76. Result:=OnGetApplicationName()
  77. else
  78. Result:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
  79. end;
  80. { ---------------------------------------------------------------------
  81. Default implementations for AppConfigDir implementation.
  82. ---------------------------------------------------------------------}
  83. Function DGetAppConfigDir(Global : Boolean) : String;
  84. begin
  85. Result:=ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
  86. end;
  87. Function DGetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  88. begin
  89. Result:=IncludeTrailingPathDelimiter(GetAppConfigDir(Global));
  90. if SubDir then
  91. Result:=IncludeTrailingPathDelimiter(Result+'Config');
  92. Result:=Result+ApplicationName+ConfigExtension;
  93. end;
  94. Function GetAppConfigFile(Global : Boolean) : String;
  95. begin
  96. Result:=GetAppConfigFile(Global,False);
  97. end;
  98. { ---------------------------------------------------------------------
  99. Fallback implementations for AppConfigDir implementation.
  100. ---------------------------------------------------------------------}
  101. {
  102. If a particular OS does it different:
  103. - set the HAVE_OSCONFIG define before including sysutils.inc.
  104. - implement the functions.
  105. Default config assumes a DOS-like configuration.
  106. }
  107. {$ifndef HAS_OSCONFIG}
  108. Function GetAppConfigDir(Global : Boolean) : String;
  109. begin
  110. Result:=DGetAppConfigDir(Global);
  111. end;
  112. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  113. begin
  114. Result:=DGetAppConfigFile(Global,Subdir);
  115. end;
  116. {$endif}
  117. { ---------------------------------------------------------------------
  118. Get temporary directory name
  119. ---------------------------------------------------------------------}
  120. {$ifndef HAS_TEMPDIR}
  121. Function GetTempDir(Global : Boolean) : String;
  122. begin
  123. If Assigned(OnGetTempDir) then
  124. Result:=OnGetTempDir(Global)
  125. else
  126. begin
  127. Result:=GetEnvironmentVariable('TEMP');
  128. If (Result='') Then
  129. Result:=GetEnvironmentVariable('TMP');
  130. end;
  131. if (Result<>'') then
  132. Result:=IncludeTrailingPathDelimiter(Result);
  133. end;
  134. {$endif}
  135. Function GetTempDir : String;
  136. begin
  137. Result:=GetTempDir(True);
  138. end;
  139. { ---------------------------------------------------------------------
  140. Get temporary file name
  141. ---------------------------------------------------------------------}
  142. {$ifndef HAS_TEMPFILE}
  143. Function GetTempFileName(Const Dir,Prefix : String) : String;
  144. Var
  145. I : Integer;
  146. Start : String;
  147. begin
  148. If Assigned(OnGetTempFile) then
  149. Result:=OnGetTempFile(Dir,Prefix)
  150. else
  151. begin
  152. If (Dir='') then
  153. Start:=GetTempDir
  154. else
  155. Start:=IncludeTrailingPathDelimiter(Dir);
  156. If (Prefix='') then
  157. Start:=Start+'TMP'
  158. else
  159. Start:=Start+Prefix;
  160. I:=0;
  161. Repeat
  162. Result:=Format('%s%.5d.tmp',[Start,I]);
  163. Inc(I);
  164. Until not FileExists(Result);
  165. end;
  166. end;
  167. {$endif}
  168. Function GetTempFileName : String;
  169. begin
  170. Result:=GetTempFileName('','');
  171. end;
  172. {$if not(defined(win32)) and not(defined(win64))}
  173. Function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
  174. Var
  175. P,Buf : String;
  176. L : Integer;
  177. begin
  178. P:=StrPas(Prefix);
  179. if (uUnique<>0) then
  180. P:=P+format('%.4x',[uUnique]);
  181. Buf:=GetTempFileName(StrPas(Dir),P);
  182. L:=Length(Buf);
  183. If (L>0) then
  184. Move(Buf[1],TempFileName^,L+1);
  185. if (uUnique<>0) then
  186. result:=uUnique
  187. else
  188. result:=1;
  189. end;
  190. {$endif}