2
0

osutil.inc 6.8 KB

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