2
0

osutil.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. Function DGetUserDir : String;
  99. begin
  100. Result:=ExtractFilePath(Paramstr(0));
  101. end;
  102. { ---------------------------------------------------------------------
  103. Fallback implementations for AppConfigDir implementation.
  104. ---------------------------------------------------------------------}
  105. {
  106. If a particular OS does it different:
  107. - set the HAVE_OSCONFIG define before including sysutils.inc.
  108. - implement the functions.
  109. Default config assumes a DOS-like configuration.
  110. }
  111. {$ifndef HAS_OSCONFIG}
  112. Function GetAppConfigDir(Global : Boolean) : String;
  113. begin
  114. Result:=DGetAppConfigDir(Global);
  115. end;
  116. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  117. begin
  118. Result:=DGetAppConfigFile(Global,Subdir);
  119. end;
  120. {$endif}
  121. { ---------------------------------------------------------------------
  122. Fallback implementations for GetUserDir implementation.
  123. ---------------------------------------------------------------------}
  124. {
  125. If a particular OS does it different:
  126. - set the HAVE_OSUSERDIR define before including sysutils.inc.
  127. - implement the function.
  128. Default makes it the application directory. Rationale is that the result
  129. will be used for config files, and it should exist. The application directory
  130. has this for sure.
  131. }
  132. {$ifndef HAS_OSUSERDIR}
  133. Function GetUserDir : String;
  134. begin
  135. Result:=DGetUserDir;
  136. end;
  137. {$endif}
  138. { ---------------------------------------------------------------------
  139. Get temporary directory name
  140. ---------------------------------------------------------------------}
  141. {$ifndef HAS_TEMPDIR}
  142. Function GetTempDir(Global : Boolean) : String;
  143. begin
  144. If Assigned(OnGetTempDir) then
  145. Result:=OnGetTempDir(Global)
  146. else
  147. begin
  148. Result:=GetEnvironmentVariable('TEMP');
  149. If (Result='') Then
  150. Result:=GetEnvironmentVariable('TMP');
  151. end;
  152. if (Result<>'') then
  153. Result:=IncludeTrailingPathDelimiter(Result);
  154. end;
  155. {$endif}
  156. Function GetTempDir : String;
  157. begin
  158. Result:=GetTempDir(True);
  159. end;
  160. { ---------------------------------------------------------------------
  161. Get temporary file name
  162. ---------------------------------------------------------------------}
  163. {$ifndef HAS_TEMPFILE}
  164. Function GetTempFileName(Const Dir,Prefix : String) : String;
  165. Var
  166. I : Integer;
  167. Start : String;
  168. begin
  169. If Assigned(OnGetTempFile) then
  170. Result:=OnGetTempFile(Dir,Prefix)
  171. else
  172. begin
  173. If (Dir='') then
  174. Start:=GetTempDir
  175. else
  176. Start:=IncludeTrailingPathDelimiter(Dir);
  177. If (Prefix='') then
  178. Start:=Start+'TMP'
  179. else
  180. Start:=Start+Prefix;
  181. I:=0;
  182. Repeat
  183. Result:=Format('%s%.5d.tmp',[Start,I]);
  184. Inc(I);
  185. Until not FileExists(Result);
  186. end;
  187. end;
  188. {$endif}
  189. Function GetTempFileName : String;
  190. begin
  191. Result:=GetTempFileName('','');
  192. end;
  193. {$if not(defined(win32)) and not(defined(win64))}
  194. Function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
  195. Var
  196. P,Buf : String;
  197. L : Integer;
  198. begin
  199. P:=StrPas(Prefix);
  200. if (uUnique<>0) then
  201. P:=P+format('%.4x',[uUnique]);
  202. Buf:=GetTempFileName(StrPas(Dir),P);
  203. L:=Length(Buf);
  204. If (L>0) then
  205. Move(Buf[1],TempFileName^,L+1);
  206. if (uUnique<>0) then
  207. result:=uUnique
  208. else
  209. result:=1;
  210. end;
  211. {$endif}