如何在安装过程中读取和比较txt文件中的数字

时间:2014-09-01 02:31:30

标签: inno-setup

我需要安装程序设置在设置过程中检查txt文件的第一行,并将其与我想要的任何数字进行比较。

这是txt文件:

enter image description here

这是我要编辑的代码:

function GetKeyValue(const AKeyName, AFileName, ADefault: string): string;
var  
  I: Integer;
  KeyPos: Integer;
  KeyFull: string;
  FileLines: TArrayOfString;
begin
  Result := ADefault;
  if LoadStringsFromFile(AFileName, FileLines) then
  begin
    KeyFull := AKeyName;
    for I := 0 to GetArrayLength(FileLines) - 1 do
    begin
      FileLines[I] := TrimLeft(FileLines[I]);
      KeyPos := Pos(KeyFull, FileLines[I]);
      if KeyPos > 0 then
      begin
        Result := Copy(FileLines[I], KeyPos + Length(AKeyName) + 1, MaxInt);
        Break;
      end;
    end;
  end;
end;

var
  // target version label must be declared globally
  L2Ver2: TLabel;

procedure DirEditChange(Sender: TObject);
var
  FilePath: string;
begin
  // assign the expected INF file path
  FilePath := AddBackslash(WizardForm.DirEdit.Text) + 'Sam.inf';
  // I WANT TO READ THE FIRST LINE OF THE TXT FILE AND return N/A if not found
  L2Ver2.Caption := GetKeyValue('', FilePath, 'N/A');
end;

procedure InitializeWizard;
begin
  // create the target label as before
  L2Ver2 := TLabel.Create(WizardForm);
  ...
  // bind the DirEditChange method to the directory edit's OnChange event
  WizardForm.DirEdit.OnChange := @DirEditChange;  
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // if the page has been turned to the select directory page, update the
  // label caption by firing the assigned OnChange event method manually
  if (CurPageID = wpSelectDir) then
    DirEditChange(nil);
end;

我从这篇文章中获得了代码:Inno Setup - How to read an INF file during the Setup

我不确定如何编辑function GetKeyValue以及代码的其他部分L2Ver2.Caption := GetKeyValue('', FilePath, 'N/A');

1 个答案:

答案 0 :(得分:3)

如何从文本文件中读取特定行?

LoadStringsFromFile功能外,您可以使用该功能。我编写的下一个函数加载FileName参数给出的文本文件,并尝试将该行从基于0的索引Index复制到输出参数Line。如果给定文件的加载成功并且文件有足够的行来满足请求的Index,则返回True,否则返回False。

function TryGetFileLine(const FileName: string; Index: Integer; out Line: string): Boolean;
var  
  FileLines: TArrayOfString;
begin
  // the function succeed when the file can be loaded and the count of lines is
  // greater than the requested line index (it is 0 based index, hence the line
  // count must be greater)
  Result := LoadStringsFromFile(FileName, FileLines) and (GetArrayLength(FileLines) > Index);
  // if the above succeeded, return the file line of the requested index to the
  // output parameter
  if Result then
    Line := FileLines[Index];
end;

对于较短的代码,我选择了基于0的索引,因此如果您想要读取文件的第一行,您将请求索引0,第二行索引1等等。要获得第一行,它将是:

var
  S: string;
begin
  // if the file could be opened and has at least one line, then the following
  // call succeed and the variable S will contain the first line of the file
  if TryGetFileLine('C:\File.txt', 0, S) then
    MsgBox('The first line of the given file is: ' + S, mbInformation, MB_OK);
end;

如何将字符串转换为整数?

此时至少有两种方法可以在Inno Setup中将字符串转换为32位整数。 StrToInt函数和StrToIntDef。第一个尝试将传递的字符串转换为整数,如果失败,则返回-1。第二种方法是相同的,除非转换失败,它返回def参数指定的值。

不幸的是,上述函数都不能可靠地判断给定的字符串是否已被转换而不会丢失整数范围内的一个值。请考虑以下代码:

var
  Value1: Integer;
  Value2: Integer;
begin
  Value1 := StrToInt('-1');
  Value2 := StrToInt('Non integer');

  if Value1 = Value2 then
  begin
    MsgBox('Err, the result is the same for string of value -1 and for a non ' +
      'integer string.', mbInformation, MB_OK);
  end;
end;

上面的代码说明,如果您使用StrToInt函数,您将无法确定字符串(在您的情况下是从文件中读取的行)是否包含值-1或非整数值。类似情况适用于StrToIntDef函数的def参数。

但是,如果明确检查字符串是否包含函数在转换失败时返回的值,则可以清除此问题。如果字符串S包含有效整数值,则以下函数返回True,否则返回False。如果转换成功,则转换后的值将返回到输出Value参数:

function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
  I: Integer;
begin
  I := StrToIntDef(S, -1);
  Result := (I <> -1) or (S = '-1');
  if Result then
    Value := I;
end;

此功能的用法如下所示:

var
  I: Integer;
begin
  if TryStrToInt('12345', I) then
  begin
    MsgBox('The passed string was converted to integer. Its value is: ' +
      IntToStr(I), mbInformation, MB_OK);
  end;
end;

安全到最后......

你没有提到文本文件中的版本值是什么,所以我假设它们是32位整数,并且可能在该范围内有任何值(即使我相信你真实的你&# 39; ll只使用正值,其中内置字符串到整数转换函数就足够了。)

但是,在代码库中有一个更安全的字符串到整数转换函数是可以的。因此,让我们将上述函数放在一起,尝试读取文本文件的第一行并将其转换为整数:

var
  S: string;
  I: Integer;
begin
  // if the first line of the file was successfully read and could have been
  // converted to integer, then...
  if TryGetFileLine('C:\File.txt', 0, S) and TryStrToInt(S, I) then
  begin
    MsgBox('The first line of the given file was successfully read and could ' +
      'have been converted to integer. Its value is: ' + IntToStr(I),
      mbInformation, MB_OK);
    // here the variable I contains the value that you can compare in a way
    // of your choice
  end;
end;