코드를 삽입 할 지점을 찾으려면 파일을 한 줄씩 파싱해야합니다. 이 같은
뭔가 :
이
function AddLineToTemplate(
FileName: string; StartLine, EndLine, AddLine: string): Boolean;
var
Lines: TArrayOfString;
Count, I, I2: Integer;
Line: string;
State: Integer;
begin
Result := True;
if not LoadStringsFromFile(FileName, Lines) then
begin
Log(Format('Error reading %s', [FileName]));
Result := False;
end
else
begin
State := 0;
Count := GetArrayLength(Lines);
for I := 0 to Count - 1 do
begin
Line := Trim(Lines[I]);
if (CompareText(Line, StartLine) = 0) then
begin
State := 1;
Log(Format('Start line found at %d', [I]));
end
else
if (State = 1) and (CompareText(Line, AddLine) = 0) then
begin
Log(Format('Line already present at %d', [I]));
State := 2;
break;
end
else
if (State = 1) and (CompareText(Line, EndLine) = 0) then
begin
Log(Format('End line found at %d, inserting', [I]));
SetArrayLength(Lines, Count + 1);
for I2 := Count - 1 downto I do
Lines[I2 + 1] := Lines[I2];
Lines[I] := AddLine;
State := 2;
if not SaveStringsToFile(FileName, Lines, False) then
begin
Log(Format('Error writting %s', [FileName]));
Result := False;
end
else
begin
Log(Format('Modifications saved to %s', [FileName]));
end;
break;
end;
end;
if Result and (State <> 2) then
begin
Log(Format('Spot to insert line was not found in %s', [FileName]));
Result := False;
end;
end;
end;
당신은 다음과 같이 사용할 수 있습니다 : 유니 코드 파일로 작업 할 때
if AddLineToTemplate(
'C:\path\to\customizations.js',
'/*DO NOT MODIFY ABOVE THIS LINE!*/',
'/*DO NOT MODIFY BELOW THIS LINE!*/',
' dependencies.push("mxdfNewTransaction.controller.mxdfNewTransactionCtrl");') then
begin
Log('Success');
end
else
begin
Log('Failure');
end;
는 LoadStringsFromFile
및 SaveStringsToFile
제한 조심하십시오. Inno Setup Reading file in Ansi and Unicode encoding을 참조하십시오.
"지정된 다른 줄"- 지정된 방법? 예제를 보여주십시오. –
@ MartinPrikryl 내 질문을 편집했습니다. 이해하는 것이 더 나을 수도 있습니다. –
나는 아직도 당신이 원하는 것을 이해하지 못합니다. 기존의'customizations.js' 파일을 수정하고 있습니까? 또는 설치 프로그램에서 파일을 완전히 만들었습니까? 첫 번째 파일 인 경우 설치 전에 파일이 어떻게 보이고 이후에 어떤 모습이어야합니까? –