lspFix.dpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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: 13394: lspFix.dpr
  11. {
  12. { Rev 1.0 11/13/2002 02:52:12 PM JPMugaas
  13. }
  14. program lspFix;
  15. {$APPTYPE CONSOLE}
  16. {This program fixes some problems with linker response files
  17. generated by DCC32 which can cause TLIB to fail}
  18. uses
  19. classes,
  20. SysUtils,
  21. Windows;
  22. const
  23. BackupExt = '.BAK';
  24. //This symbol should be removed because it's not needed and
  25. //and may be problematic if in a LIB file. It's a symbol used
  26. //in Borland's RTL library
  27. ProblemSymbol = 'SysInit.obj';
  28. ReplaceSymbol = '-+';
  29. {from
  30. http://community.borland.com/article/0,1410,15910,00.html
  31. }
  32. procedure FileCopy( Const ASourceFilename, ATargetFilename: String );
  33. var
  34. S, T: TFileStream;
  35. begin
  36. S := TFileStream.Create( ASourceFilename, fmOpenRead );
  37. try
  38. T := TFileStream.Create( ATargetFilename,
  39. fmOpenWrite or fmCreate );
  40. try
  41. T.CopyFrom(S, 0);
  42. finally
  43. T.Free;
  44. end;
  45. finally
  46. S.Free;
  47. end;
  48. end;
  49. procedure Error;
  50. begin
  51. ExitCode := 1;
  52. end;
  53. function GetLSPFileName : String;
  54. var
  55. i : Integer;
  56. begin
  57. Result := '';
  58. for i := 1 to ParamCount do
  59. begin
  60. Result := Result + ParamStr(i) + ' ';
  61. end;
  62. Result := Trim(Result);
  63. end;
  64. function FileContents(AFileName : String) : String;
  65. var
  66. s : TStream;
  67. sz : Integer;
  68. tmp: AnsiString;
  69. begin
  70. s := TFileStream.Create(AFileName,fmOpenRead);
  71. try
  72. sz := s.Size;
  73. if sz > 0 then begin
  74. SetLength(tmp, sz);
  75. s.Read(tmp[1], sz);
  76. Result := String(tmp);
  77. end;
  78. finally
  79. FreeAndNil(s);
  80. end;
  81. end;
  82. function FixTLibEntry(const AOldStr : String) : String;
  83. begin
  84. // Result := ExtractShortPathName(ExpandFileName(ExtractFilePath(AOldStr)));
  85. // Result := Result + ExtractFileName(AOldStr);
  86. Result := '"'+AOldStr+'"';
  87. end;
  88. function RepairLSPData(AOriginalData : String): String;
  89. var
  90. s : TStrings;
  91. i : Integer;
  92. begin
  93. s := TStringList.Create;
  94. try
  95. s.Text := Trim(StringReplace(AOriginalData, ReplaceSymbol, sLineBreak, [rfReplaceAll]));
  96. i := 0;
  97. while i < s.Count do
  98. begin
  99. if (Length(s[i]) = 0) or (ExtractFileName(s[i]) = ProblemSymbol) then
  100. begin
  101. s.Delete(i);
  102. end else begin
  103. s[i] := ReplaceSymbol + FixTLibEntry(s[i]);
  104. Inc(i);
  105. end;
  106. end;
  107. Result := StringReplace(s.Text, sLineBreak, ' ', [rfReplaceAll]);
  108. finally
  109. FreeAndNil(s);
  110. end;
  111. end;
  112. procedure FileCreate(AFilename, AText: String; AOverwrite : Boolean = True);
  113. var
  114. s : TStream;
  115. tmp: AnsiString;
  116. begin
  117. s := TFileStream.Create(AFileName, fmCreate);
  118. try
  119. tmp := AnsiString(AText);
  120. s.Write(PAnsiChar(tmp)^, Length(tmp));
  121. finally
  122. FreeAndNil(s);
  123. end;
  124. end;
  125. var
  126. lspName : String;
  127. data: String;
  128. begin
  129. try
  130. if ParamCount = 0 then
  131. begin
  132. WriteLn('This program requires that you pass the file name for the');
  133. WriteLn('linker response file that needs to be fixed. For example:');
  134. WriteLn('');
  135. WriteLn('lspFix MyLispFile.lsp');
  136. Exit;
  137. end;
  138. lspName := GetLSPFileName;
  139. if ExtractFileExt(UpperCase(lspName)) = BackupExt then
  140. begin
  141. raise Exception.Create('You can not fix a backup file.');
  142. end;
  143. FileCopy(lspName, ChangeFileExt(lspName, BackupExt));
  144. data := FileContents(lspName);
  145. data := RepairLSPData(data);
  146. FileCreate(lspName, data);
  147. except
  148. on E: Exception do
  149. begin
  150. WriteLn(E.Message);
  151. end;
  152. end;
  153. end.