2
0

osutil.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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:=EP^;
  62. end;
  63. { these are extremely inefficient, but not much we can do about it because
  64. changing their result type based on the supported OS-interfaces will change
  65. program behaviour if they are passed as a parameter to overloaded routines }
  66. {$ifndef SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  67. Function GetEnvironmentVariable(Const EnvVar : AnsiString) : AnsiString;
  68. begin
  69. result:=AnsiString(GetEnvironmentVariable(UnicodeString(EnvVar)));
  70. end;
  71. {$endif not SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  72. {$ifndef SYSUTILS_HAS_UNICODESTR_ENVVAR_IMPL}
  73. Function GetEnvironmentVariable(Const EnvVar : UnicodeString) : UnicodeString;
  74. begin
  75. result:=UnicodeString(GetEnvironmentVariable(AnsiString(EnvVar)));
  76. end;
  77. {$endif not SYSUTILS_HAS_UNICODESTR_ENVVAR_IMPL}
  78. { ---------------------------------------------------------------------
  79. Application name
  80. ---------------------------------------------------------------------}
  81. Function VendorName : String;
  82. begin
  83. If Assigned(OnGetVendorName) then
  84. Result:=OnGetVendorName()
  85. else
  86. Result:='';
  87. end;
  88. Function ApplicationName : String;
  89. begin
  90. If Assigned(OnGetApplicationName) then
  91. Result:=OnGetApplicationName()
  92. else
  93. Result:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
  94. end;
  95. { ---------------------------------------------------------------------
  96. Default implementations for AppConfigDir implementation.
  97. ---------------------------------------------------------------------}
  98. Function DGetAppConfigDir(Global : Boolean) : String;
  99. begin
  100. Result:=ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
  101. end;
  102. Function DGetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  103. begin
  104. Result:=IncludeTrailingPathDelimiter(GetAppConfigDir(Global));
  105. if SubDir then
  106. Result:=IncludeTrailingPathDelimiter(Result+'Config');
  107. Result:=Result+ApplicationName+ConfigExtension;
  108. end;
  109. Function GetAppConfigFile(Global : Boolean) : String;
  110. begin
  111. Result:=GetAppConfigFile(Global,False);
  112. end;
  113. Function DGetUserDir : String;
  114. begin
  115. Result:=ExtractFilePath(Paramstr(0));
  116. end;
  117. { ---------------------------------------------------------------------
  118. Fallback implementations for AppConfigDir implementation.
  119. ---------------------------------------------------------------------}
  120. {
  121. If a particular OS does it different:
  122. - set the HAVE_OSCONFIG define before including sysutils.inc.
  123. - implement the functions.
  124. Default config assumes a DOS-like configuration.
  125. }
  126. {$ifndef HAS_OSCONFIG}
  127. Function GetAppConfigDir(Global : Boolean) : String;
  128. begin
  129. Result:=DGetAppConfigDir(Global);
  130. end;
  131. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  132. begin
  133. Result:=DGetAppConfigFile(Global,Subdir);
  134. end;
  135. {$endif}
  136. { ---------------------------------------------------------------------
  137. Fallback implementations for GetUserDir implementation.
  138. ---------------------------------------------------------------------}
  139. {
  140. If a particular OS does it different:
  141. - set the HAVE_OSUSERDIR define before including sysutils.inc.
  142. - implement the function.
  143. Default makes it the application directory. Rationale is that the result
  144. will be used for config files, and it should exist. The application directory
  145. has this for sure.
  146. }
  147. {$ifndef HAS_OSUSERDIR}
  148. Function GetUserDir : String;
  149. begin
  150. Result:=DGetUserDir;
  151. end;
  152. {$endif}
  153. { ---------------------------------------------------------------------
  154. Get temporary directory name
  155. ---------------------------------------------------------------------}
  156. {$ifndef HAS_TEMPDIR}
  157. Function GetTempDir(Global : Boolean) : String;
  158. begin
  159. If Assigned(OnGetTempDir) then
  160. Result:=OnGetTempDir(Global)
  161. else
  162. begin
  163. Result:=GetEnvironmentVariable('TEMP');
  164. If (Result='') Then
  165. Result:=GetEnvironmentVariable('TMP');
  166. end;
  167. if (Result<>'') then
  168. Result:=IncludeTrailingPathDelimiter(Result);
  169. end;
  170. {$endif}
  171. Function GetTempDir : String;
  172. begin
  173. Result:=GetTempDir(True);
  174. end;
  175. { ---------------------------------------------------------------------
  176. Get temporary file name
  177. ---------------------------------------------------------------------}
  178. {$ifndef HAS_TEMPFILE}
  179. Function GetTempFileName(Const Dir,Prefix : String) : String;
  180. Var
  181. I : Integer;
  182. Start : String;
  183. begin
  184. If Assigned(OnGetTempFile) then
  185. Result:=OnGetTempFile(Dir,Prefix)
  186. else
  187. begin
  188. If (Dir='') then
  189. Start:=GetTempDir
  190. else
  191. Start:=IncludeTrailingPathDelimiter(Dir);
  192. If (Prefix='') then
  193. Start:=Start+'TMP'
  194. else
  195. Start:=Start+Prefix;
  196. I:=0;
  197. Repeat
  198. Result:=Format('%s%.5d.tmp',[Start,I]);
  199. Inc(I);
  200. Until not FileExists(Result);
  201. end;
  202. end;
  203. {$endif}
  204. Function GetTempFileName : String;
  205. begin
  206. Result:=GetTempFileName('','');
  207. end;
  208. {$if not(defined(win32)) and not(defined(win64))}
  209. Function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
  210. Var
  211. P,Buf : String;
  212. L : Integer;
  213. begin
  214. P:=StrPas(Prefix);
  215. if (uUnique<>0) then
  216. P:=P+format('%.4x',[uUnique]);
  217. Buf:=GetTempFileName(StrPas(Dir),P);
  218. L:=Length(Buf);
  219. If (L>0) then
  220. Move(Buf[1],TempFileName^,L+1);
  221. if (uUnique<>0) then
  222. result:=uUnique
  223. else
  224. result:=1;
  225. end;
  226. {$endif}