DXPUtils.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // FDXPOptions
  2. {
  3. DXP utility functions.
  4. Licensed under MPL (http://www.mozilla.org/MPL/)
  5. Copyright 2003 - Eric Grange
  6. }
  7. unit DXPUtils;
  8. interface
  9. uses Forms, Classes, Windows;
  10. function ExecuteAndWait(cmdLine : String; visibility : Word;
  11. timeout : Cardinal = MaxInt;
  12. killAppOnTimeOut : Boolean = True) : Integer;
  13. function GetTemporaryFilesPath : String;
  14. function GetTemporaryFileName : String;
  15. function FindFileInPaths(const fileName, paths : String) : String;
  16. function PathsToString(const paths : TStrings) : String;
  17. procedure StringToPaths(const pathsString : String; paths : TStrings);
  18. function MacroExpandPath(const aPath : String) : String;
  19. // -----------------------------------------------------------------
  20. // -----------------------------------------------------------------
  21. // -----------------------------------------------------------------
  22. implementation
  23. // -----------------------------------------------------------------
  24. // -----------------------------------------------------------------
  25. // -----------------------------------------------------------------
  26. uses Dialogs, SysUtils, DXPGlobals;
  27. // ExecuteAndWait
  28. //
  29. function ExecuteAndWait(cmdLine : String; visibility : Word;
  30. timeout : Cardinal = MaxInt;
  31. killAppOnTimeOut : Boolean = True) : Integer;
  32. var
  33. waitResult : Cardinal;
  34. startupInfo: TStartupInfo;
  35. processInfo: TProcessInformation;
  36. app : String;
  37. exitCode : Cardinal;
  38. begin
  39. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  40. with StartupInfo do begin
  41. cb:=SizeOf(TStartupInfo);
  42. dwFlags:=(STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK);
  43. wShowWindow:=visibility;
  44. end;
  45. app:=Copy(cmdLine, 1, Pos(' ', cmdLine)-1);
  46. if CreateProcess(PChar(app), PChar(cmdLine), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil,
  47. startupInfo, processInfo) then begin
  48. try
  49. repeat
  50. waitResult:=WaitForSingleObject(ProcessInfo.hProcess, 500);
  51. if waitResult<>WAIT_TIMEOUT then Break;
  52. Application.ProcessMessages;
  53. Dec(timeOut, 500);
  54. until timeOut<=0;
  55. if waitResult<>WAIT_OBJECT_0 then begin
  56. Result:=GetLastError;
  57. if killAppOnTimeOut then begin
  58. TerminateProcess(ProcessInfo.hProcess, 0);
  59. WaitForSingleObject(ProcessInfo.hProcess, 1000);
  60. end;
  61. end else begin
  62. GetExitCodeProcess(ProcessInfo.hProcess, exitCode);
  63. Result:=exitCode;
  64. end;
  65. finally
  66. CloseHandle(ProcessInfo.hProcess);
  67. CloseHandle(ProcessInfo.hThread);
  68. end;
  69. end else begin
  70. RaiseLastOSError;
  71. Result:=-1;
  72. end;
  73. end;
  74. // GetTemporaryFilesPath
  75. //
  76. function GetTemporaryFilesPath : String;
  77. begin
  78. SetLength(Result, 512);
  79. Setlength(Result, GetTempPath(510, PChar(Result)));
  80. end;
  81. // GetTemporaryFileName
  82. //
  83. function GetTemporaryFileName : String;
  84. begin
  85. SetLength(Result, 512);
  86. GetTempFileName(PChar(GetTemporaryFilesPath), 'DXP-', 0, PChar(Result));
  87. Result:=StrPas(PChar(Result));
  88. end;
  89. // FindFileInPaths
  90. //
  91. function FindFileInPaths(const fileName, paths : String) : String;
  92. var
  93. i : Integer;
  94. sl : TStringList;
  95. begin
  96. sl:=TStringList.Create;
  97. try
  98. sl.Delimiter:=';';
  99. sl.CommaText:=paths;
  100. for i:=0 to sl.Count-1 do begin
  101. if FileExists(sl[i]+'\'+fileName) then begin
  102. Result:=sl[i]+'\'+fileName;
  103. Exit;
  104. end;
  105. end;
  106. finally
  107. sl.Free;
  108. end;
  109. Result:='';
  110. end;
  111. // PathsToString
  112. //
  113. function PathsToString(const paths : TStrings) : String;
  114. var
  115. i : Integer;
  116. begin
  117. Result:='';
  118. for i:=0 to paths.Count-1 do if paths[i]<>'' then
  119. Result:=Result+paths[i]+';';
  120. if Result<>'' then
  121. SetLength(Result, Length(Result)-1);
  122. end;
  123. // StringToPaths
  124. //
  125. procedure StringToPaths(const pathsString : String; paths : TStrings);
  126. var
  127. i, p, n : Integer;
  128. begin
  129. paths.BeginUpdate;
  130. paths.Clear;
  131. p:=1;
  132. for i:=1 to Length(pathsString) do begin
  133. if pathsString[i]=';' then begin
  134. n:=i-p;
  135. if n>0 then
  136. paths.Add(Copy(pathsString, p, n));
  137. p:=i+1;
  138. end;
  139. end;
  140. n:=Length(pathsString)-p+1;
  141. if n>0 then
  142. paths.Add(Copy(pathsString, p, n));
  143. paths.EndUpdate;
  144. end;
  145. // MacroExpandPath
  146. //
  147. function MacroExpandPath(const aPath : String) : String;
  148. begin
  149. Result:=aPath;
  150. Result:=StringReplace(aPath, '$FreePascal', vFPC_RootPath, [rfReplaceAll, rfIgnoreCase]);
  151. Result:=StringReplace(aPath, '$FPC', vFPC_RootPath, [rfReplaceAll, rfIgnoreCase]);
  152. end;
  153. end.