首页 > 解决方案 > Inno设置加载字符串,获取数字并添加

问题描述

我需要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 并添加

[区域.5]

标题=世界 F1

本地=C:\风景\世界\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;

标签: inno-setup

解决方案


您首先必须阅读配置文件以获取最大区域数。请参阅以下功能。有一个单独的函数可以获取特定行的编号。达到最大数量后,您只需加 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;

推荐阅读