InnoSetup支持一些INI文件操作函数,
例如GetIniString,IniKeyExists等等。。
利用这些已有的函数,读取/删除/检测键值是否存在,都没有问题。
但是,想要列出一个INI文件有哪些Sections,
某个Section下面有哪些键值,
就没法做到了。
所幸InnoSetup有个很好用的 LoadStringsFromFile 函数,
用来读取文本文件很方便,读INI自然不在话下。
然后再做一点简单的解析,就能得到一个INI中所有的sections和键值了。
实现两个函数如下:
读取INI文件中有哪些Sections, 并将其名字存放到 Arr数组里。
procedure IniGetSections(var Arr: Array of String; const Path: String);
读取INI文件的某个Section底下,有哪些键名,并将这些键名存放到Arr数组里。
procedure IniGetKeys(var Arr: Array of String; const Path: String; const Section: String);
procedure ArrayAddStr(var Arr: Array of String; Str: String);
var
Len: Longint;
begin
Len := GetArrayLength(Arr);
SetArrayLength(Arr, Len + 1);
Arr[Len] := Str;
end;
//-- INI
function IniGetSectionName(Line: String; var Name: String): Boolean;
P0, P1: Integer;
Line := TrimLeft(Line);
P0 := Pos('[', Line);
P1 := Pos(']', Line);
if (P0 = 1) and (P1 > P0 + 1) then begin
Name := Copy(Line, P0 + 1, P1 - P0 - 1);
Result := True;
end
else
Result := False;
function IniGetKeyName(Line: String; var Name: String): Boolean;
Line := TrimLeft(Line);
P0 := Pos('=', Line);
P1 := pOS(';', Line); //; is start of comment
if (P0 > 1) and ((P1 = 0) or (P1 > P0)) then begin
Name := Trim(Copy(Line, 1, P0 - 1));
Result := True;
end
else
Result := False;
i: Longint;
Name: String;
Lines: Array of String;
SetArrayLength(Arr, 0);
if not LoadStringsFromFile(Path, Lines) then exit;
for i := 0 to GetArrayLength(Lines) - 1 do begin
if IniGetSectionName(Lines[i], Name) then ArrayAddStr(Arr, Name);
end;
if IniGetSectionName(Lines[i], Name) then begin
if CompareText(Name, Section) = 0 then break;
end;
if i < GetArrayLength(Lines) then begin
{ The section is }
for i := i + 1 to GetArrayLength(Lines) - 1 do begin
if IniGetSectionName(Lines[i], Name) then break
else if IniGetKeyName(Lines[i], Name) then ArrayAddStr(Arr, Name);