Inno setup загрузить строку, получить номер и добавить - PullRequest
0 голосов
/ 11 февраля 2020

Мне нужна настройка inno, чтобы взять строку, проверить номер Area. и добавьте +1, см. пример ниже

Original FILE:

[Area.1]
Title=World P1
Local=C:\scenery\world\p
Layer=
Active=TRUE
Required=FALSE

[Area.2]
Title=World C1
Local=C:\scenery\world\c
Layer=
Active=TRUE
Required=FALSE

[Area.3]
Title=World D1
Local=C:\scenery\world\d
Layer=
Active=TRUE
Required=FALSE

[Area.4]
Title=World E1
Local=C:\scenery\world\e
Layer=
Active=TRUE
Required=FALSE

Inno setup проверит, какая область является последней, в случае Area.4 она возьмет число, добавит +1 и добавит еще один область с дополнительным номером, чтобы иметь возможность следить за файлом, как сказано. Итак, возьмите Area.4 и добавьте

[Area.5]

Title = World F1

Local = C: \ scenery \ world \ f

Слой =

Актив = ИСТИНА

Обязательно = ЛОЖЬ

Inno Setup, прочитайте и проверьте последнюю область, после установки она останется вот так

[Area.1]
Title=World P1
Local=C:\scenery\world\p
Layer=
Active=TRUE
Required=FALSE

[Area.2]
Title=World C1
Local=C:\scenery\world\c
Layer=
Active=TRUE
Required=FALSE

[Area.3]
Title=World D1
Local=C:\scenery\world\d
Layer=
Active=TRUE
Required=FALSE

[Area.4]
Title=World E1
Local=C:\scenery\world\e
Layer=
Active=TRUE
Required=FALSE

[Area.5]
Title=World F1
Local=C:\scenery\world\f
Layer=
Active=TRUE
Required=FALSE

Я использую этот код, но он просто добавляет, мне нужен установщик, чтобы проверить число в исходном файле и изменить строки [1], добавив +1, как если бы это было сумма в PHP / mysql

function saveStringToFile(): boolean;
var
  InstallDir: string;
  fileName : string;
  lines : TArrayOfString;
begin
  if FileExists(ExpandConstant('{app}\scenery.cfg')) then
  begin
    MsgBox('Archive "scenery.cfg" found', mbInformation, MB_OK);
    Result := True;
    fileName := ExpandConstant('{app}\scenery.cfg');
    SetArrayLength(lines, 43);
  //
  lines[0] := '';
  lines[1] := '[Area.5]';
  lines[2] := 'Title=World F1';
  lines[3] := 'Local=C:\scenery\world\f';
  lines[4] := 'Layer=';
  lines[5] := 'Active=TRUE';
  lines[6] := 'Required=FALSE';
  lines[7] := '';
  //
  Result := SaveStringsToFile(filename,lines,true);
  exit;
  end
  else
  begin
    MsgBox('Archive "scenery.cfg" not found', mbInformation, MB_OK);
    Result := False;
  end;
end;

1 Ответ

0 голосов
/ 12 февраля 2020

Сначала вы должны прочитать файл конфигурации, чтобы получить максимальный номер области. Смотрите следующие функции. Есть отдельная функция для получения номера определенной строки. После того, как у вас есть максимальное количество, вам нужно просто добавить 1 и продолжить писать свои дополнительные области.

После того, как у вас есть fileName, вы можете позвонить: maxNumber := GetMaxNumber(fileName);

Функция имеет некоторые поясняющие комментарии. Есть также окна сообщений, которые вы можете раскомментировать, которые дают вам некоторую информацию о том, что происходит.

function GetAreaNumber(line : String) : Integer;
var
    pos1, pos2 : Integer;
    number : String;
begin
    // This function only gets called, when the line contains an area header.
    // Get the positions of the chracter before and after the number and
    // extract the number.
    pos1 := Pos('.', line)
    pos2 := Pos(']', line)
    number := Copy(line, pos1 + 1, pos2 - (pos1 + 1))
    //MsgBox(number, mbInformation, MB_OK)
    Result := StrToIntDef(number, 0)
end;

function GetMaxNumber(fileName : String): Integer;
var
    lines : TArrayOfString;
    linesCount, index : Integer;
    maxNumber, currentNumber : Integer;
begin
    maxNumber := 0

    if LoadStringsFromFile(fileName, lines) then
    begin
        linesCount := GetArrayLength(lines)
        //MsgBox(IntToStr(linesCount), mbInformation, MB_OK)

        // Run through all the lines from the file.
        for index := 0 to linesCount - 1 do
        begin
            // Check if the line contains an area header.
            if Pos('[Area.', lines[index]) > 0 then
            begin
                //MsgBox(lines[index], mbInformation, MB_OK)
                currentNumber := GetAreaNumber(lines[index])

                if currentNumber > maxNumber then
                begin
                    maxNumber := currentNumber
                end
            end
        end
    end;

    Result := maxNumber
end;
...