Computil.dpr 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10019: Computil.dpr
  11. {
  12. { Rev 1.4 24/08/2004 12:41:44 ANeillans
  13. { Modified to ensure the registry object is opened in Read Only mode.
  14. }
  15. {
  16. { Rev 1.3 7/14/04 1:40:26 PM RLebeau
  17. { removed some repeating code
  18. }
  19. {
  20. { Rev 1.2 14/07/2004 21:15:38 ANeillans
  21. { Modification to allow both HKLM and HKCU to be used for fetching binary path.
  22. }
  23. {
  24. { Rev 1.1 03/05/2004 15:36:22 ANeillans
  25. { Bug fix: Rootdir blank causes AV. Changes HKEY_LOCAL_MACHINE to
  26. { HKEY_CURRENT_USER.
  27. }
  28. {
  29. { Rev 1.0 2002.11.12 10:25:38 PM czhower
  30. }
  31. program CompUtil;
  32. {$APPTYPE CONSOLE}
  33. uses
  34. Windows, SysUtils, Registry, Classes;
  35. var
  36. CmdParam, RegCompanyName, RegProductName, RegRoot, EnvName: string;
  37. ProductLanguage: Char;
  38. ProductVersion, RegVersion: Integer;
  39. procedure HPPModify;
  40. var
  41. InFile: file;
  42. OutFile: text;
  43. Line: AnsiString;
  44. Buffer: pointer;
  45. BufPtr: PAnsiChar;
  46. BufSize: longint;
  47. EOL: boolean;
  48. begin
  49. // Fix C++Builder HPP conversion bug:
  50. // - Input line in RVDefine.pas is
  51. // TRaveUnits = {$IFDEF WIN32}type{$ENDIF} TRaveFloat;
  52. //
  53. // - Invalid output line in RVDefine.hpp is
  54. // typedef TRaveUnits TRaveUnits;
  55. //
  56. // - Valid output line in RVDefine.hpp should be
  57. // typedef double TRaveUnits;
  58. { Read in RVDefine.hpp as binary }
  59. AssignFile(InFile,ParamStr(2) + 'RVDefine.hpp');
  60. Reset(InFile,1);
  61. BufSize := FileSize(InFile);
  62. GetMem(Buffer,BufSize);
  63. BlockRead(InFile,Buffer^,BufSize);
  64. CloseFile(InFile);
  65. BufPtr := Buffer;
  66. { Write out modified RVDefine.hpp as text }
  67. AssignFile(OutFile,ParamStr(2) + 'RVDefine.hpp');
  68. Rewrite(OutFile);
  69. While BufSize > 0 do begin
  70. Line := '';
  71. EOL := false;
  72. Repeat { Get a line of text }
  73. If BufPtr^ = #13 then begin
  74. Inc(BufPtr);
  75. Dec(BufSize);
  76. Inc(BufPtr);
  77. Dec(BufSize);
  78. EOL := true;
  79. end else begin
  80. Line := Line + BufPtr^;
  81. Inc(BufPtr);
  82. Dec(BufSize);
  83. end; { else }
  84. until EOL or (BufSize = 0);
  85. If Line = 'typedef TRaveUnits TRaveUnits;' then begin
  86. Line := 'typedef double TRaveUnits;';
  87. end; { if }
  88. Writeln(OutFile,Line);
  89. end; { while }
  90. CloseFile(OutFile);
  91. end; { HPPModify }
  92. procedure SetEnvPaths;
  93. var
  94. CompilerFound: boolean;
  95. SysDirFound: boolean;
  96. KeyOpened: boolean;
  97. EnvUpdated: boolean;
  98. EnvList: TStringList;
  99. SysDir: string;
  100. ShortPath: string;
  101. LongPath: string;
  102. begin
  103. CompilerFound := GetEnvironmentVariable(PChar(EnvName), nil, 0) <> 0;
  104. SysDirFound := GetEnvironmentVariable('NDWINSYS', nil, 0) <> 0;
  105. If (not CompilerFound) or (not SysDirFound) then begin
  106. EnvUpdated := False;
  107. EnvList := TStringList.Create;
  108. try
  109. If FileExists('SetEnv.bat') then begin { Read in existing file }
  110. EnvList.LoadFromFile('SetEnv.bat');
  111. end; { if }
  112. If not CompilerFound then begin { Get compiler path and add to string list }
  113. Writeln(EnvName + ', Checking Registry: ' + RegRoot);
  114. With TRegistry.Create do try
  115. RootKey := HKEY_LOCAL_MACHINE;
  116. KeyOpened := OpenKeyReadOnly(RegRoot);
  117. if not KeyOpened then begin
  118. Writeln('Resetting registry rootkey to HKCU, and retrying');
  119. RootKey := HKEY_CURRENT_USER;
  120. KeyOpened := OpenKeyReadOnly(RegRoot);
  121. End;
  122. if KeyOpened and ValueExists('RootDir') then begin
  123. LongPath := ReadString('RootDir');
  124. SetLength(ShortPath, MAX_PATH); // when casting to a PChar, be sure the string is not empty
  125. SetLength(ShortPath, GetShortPathName(PChar(LongPath), PChar(ShortPath), MAX_PATH) );
  126. If (ShortPath[1] = #0) or (Length(ShortPath) = Length(LongPath)) then begin
  127. ShortPath := LongPath;
  128. end;
  129. EnvList.Add('SET ' + EnvName + '=' + ShortPath);
  130. EnvUpdated := True;
  131. end else begin
  132. Writeln('Compiler not installed!');
  133. Halt(1);
  134. End; { else }
  135. finally
  136. Free;
  137. end; { with }
  138. end; { if }
  139. If not SysDirFound then begin { Get System Directory and add to string list }
  140. SetLength(SysDir, MAX_PATH);
  141. SetLength(SysDir, GetSystemDirectory(PChar(SysDir), MAX_PATH));
  142. EnvList.Add('SET NDWINSYS=' + SysDir);
  143. EnvUpdated := True;
  144. end; { if }
  145. If EnvUpdated then begin
  146. EnvList.SaveToFile('SetEnv.bat');
  147. End; { if }
  148. finally
  149. EnvList.Free;
  150. end; { tryf }
  151. end; { if }
  152. end; { SetPath }
  153. begin
  154. { Figure out which feature to run }
  155. CmdParam := UpperCase(ParamStr(1));
  156. // RLebeau 11/24/23: not using TWhichOption enum anymore, because it had
  157. // to be updated every time a new Delphi/C++Builder version is released.
  158. // Now parsing the input parameter and building up the required values
  159. // dynamically so the code is a bit more future-proof. It shouldn't need
  160. // to be updated again until the schema for the Registry key changes ...
  161. if CmdParam = 'HPPMODIFY' then begin
  162. HPPModify;
  163. Exit;
  164. end;
  165. if (Length(CmdParam) >= 7) and (Copy(CmdParam, 1, 5) = 'SETUP') then begin
  166. // 'SETUP' <Language> <Version>
  167. // ie 'SETUPD29' for Delphi v29.0, etc...
  168. // ie 'SETUPC29' for C++Builder v29.0, etc...
  169. ProductLanguage := CmdParam[6];
  170. if ((ProductLanguage = 'C') or (ProductLanguage = 'D')) and
  171. TryStrToInt(Copy(CmdParam, 7, MaxInt), ProductVersion) then
  172. begin
  173. // Determine Company registry key name...
  174. if ProductVersion >= 15 then begin
  175. RegCompanyName := 'Embarcadero';
  176. end
  177. else if ProductVersion >= 12 then begin
  178. RegCompanyName := 'CodeGear';
  179. end
  180. else begin
  181. RegCompanyName := 'Borland';
  182. end;
  183. // Determine Product registry key name...
  184. if ProductVersion >= 9 then begin
  185. RegProductName := 'BDS';
  186. end
  187. else if ProductLanguage = 'D' then begin
  188. RegProductName := 'Delphi';
  189. end else begin
  190. RegProductName := 'C++Builder';
  191. end;
  192. // Determine Product Version registry key name
  193. //
  194. // Note: Embarcadero resynced their internal product version
  195. // to match the compiler version in RAD Studio 13.0 Florence!
  196. //
  197. // RAD 12.0 = Product v29 -> BDS v23
  198. // RAD 13.0 = Product v37 -> BDS v37
  199. //
  200. if ProductVersion >= 37 then begin
  201. RegVersion := ProductVersion; // Product v37+ -> BDS v37+
  202. end
  203. else if ProductVersion >= 30 then begin
  204. // no Product versions
  205. RegVersion := 0;
  206. end
  207. else if ProductVersion >= 20 then begin
  208. RegVersion := ProductVersion - 6; // Product v20+ -> BDS v14+
  209. end
  210. else if ProductVersion >= 14 then begin
  211. RegVersion := ProductVersion - 7; // Product v14..19 -> BDS v7..12 (no v13)
  212. end
  213. else if ProductVersion = 13 then begin
  214. RegVersion := 0; // no Product v13
  215. end
  216. else if (ProductVersion > 9) or
  217. ((ProductVersion = 9) and (ProductLanguage = 'D')) then // (no C++Builder v9)
  218. begin
  219. RegVersion := ProductVersion - 6; // Product v9..12 (no v13) -> BDS v3..6
  220. end
  221. else if (ProductLanguage = 'D') and (ProductVersion >= 2) then begin
  222. RegVersion := ProductVersion; // Product v2..8 (no v1)
  223. end
  224. else if (ProductLanguage = 'C') and
  225. (ProductVersion >= 1) and (ProductVersion <= 6) and
  226. (ProductVersion <> 2) then
  227. begin
  228. RegVersion := ProductVersion; // Product v1..6 (no v2, v7, or v8)
  229. end else begin
  230. RegVersion := 0;
  231. end;
  232. if RegVersion > 0 then begin
  233. EnvName := Format('ND%s%d', [ProductLanguage, ProductVersion]);
  234. RegRoot := Format('Software\%s\%s\%d.0', [RegCompanyName, RegProductName, RegVersion]);
  235. SetEnvPaths;
  236. Exit;
  237. end;
  238. end;
  239. end;
  240. Writeln('Invalid Parameter');
  241. end.