lspFix.dpr 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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: 10461: lspFix.dpr
  11. {
  12. { Rev 1.0 2002.11.12 11:02:32 PM czhower
  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, S.Size ) ;
  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 i : Integer;
  55. begin
  56. Result := '';
  57. for i := 1 to ParamCount do
  58. begin
  59. Result := Result + ParamStr(i)+' ';
  60. end;
  61. Result := Trim(Result);
  62. end;
  63. function FileContents(AFileName : String) : String;
  64. var s : TStream;
  65. sz : Integer;
  66. begin
  67. s := TFileStream.Create(AFileName,fmOpenRead);
  68. try
  69. sz := s.Size;
  70. SetLength(Result,sz);
  71. s.Read(Result[1],sz);
  72. finally
  73. FreeAndNil(s);
  74. end;
  75. end;
  76. function FixTLibEntry(AOldStr : String) : String;
  77. begin
  78. Result := ExtractShortPathName(ExpandFileName(ExtractFilePath(AOldStr)));
  79. Result := Result + ExtractFileName(AOldStr);
  80. end;
  81. function RepairLSPData(AOriginalData : String): String;
  82. var s : TStrings;
  83. i : Integer;
  84. begin
  85. s := TStringList.Create;
  86. try
  87. s.Text := Trim(StringReplace(AOriginalData,ReplaceSymbol,sLineBreak,[rfReplaceAll]));
  88. i := 0;
  89. while i < s.Count do
  90. begin
  91. if ExtractFileName(s[i])=ProblemSymbol then
  92. begin
  93. s.Delete(i);
  94. end
  95. else
  96. begin
  97. if Length(s[i])>0 then
  98. begin
  99. s[i] := ReplaceSymbol + FixTLibEntry(s[i]);
  100. end
  101. else
  102. begin
  103. s.Delete(i);
  104. end;
  105. end;
  106. Inc(i);
  107. end;
  108. Result := StringReplace(s.Text,sLineBreak,' ',[rfReplaceAll]);
  109. finally
  110. FreeAndNil(s);
  111. end;
  112. end;
  113. procedure FileCreate(AFilename, AText : String; AOverwrite : Boolean = True);
  114. var s : TStream;
  115. begin
  116. s := TFileStream.Create(AFileName,fmCreate);
  117. try
  118. s.Write(AText[1],Length(AText));
  119. finally
  120. FreeAndNil(s);
  121. end;
  122. end;
  123. var lspName : String;
  124. begin
  125. try
  126. if ParamCount = 0 then
  127. begin
  128. WriteLn('This program requires that you pass the file name for the');
  129. WriteLn('linker response file that needs to be fixed. For example:');
  130. WriteLn('');
  131. WriteLn('lspFix MyLispFile.lsp');
  132. Exit;
  133. end;
  134. lspName := GetLSPFileName;
  135. if ExtractFileExt(UpperCase(lspName)) = BackupExt then
  136. begin
  137. Raise Exception.Create('You can not fix a backup file.');
  138. end;
  139. FileCopy(lspName, ChangeFileExt(lspName,BackupExt));
  140. FileCreate(lspName, RepairLSPData( FileContents(lspName)));
  141. except
  142. on E: Exception do
  143. begin
  144. WriteLn(E.Message);
  145. end;
  146. end;
  147. end.