Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

search - Find and read specific string from config file with Pascal Script in Inno Setup

I have quite long config file and I need to extract specific strings from the file. What I want to extract/read is InstallDir for specific number position e.g. for 20540.

I know how to find string in INI or XML, but cannot handle this form of file.

Piece of the file that shows structure:

"212280"
{
    "InstallDir"        "D:\XYZ\stu\opr"
    "UpdateKBtoDL"      "0"
    "HasAllLocalContent"        "1"
    "UpToDate"      "1"
    "DisableAutoUpdate"     "0"
}
"20540"
{
    "UpdateKBtoDL"      "0"
    "InstallDir"        "C:\ABC\def\ghi"
    "HasAllLocalContent"        "1"
    "UpToDate"      "1"
    "maintenance_time"      "1339663134"
    "DisableAutoUpdate"     "0"
}
"4560"
{
    "UpdateKBtoDL"      "0"
    "HasAllLocalContent"        "0"
    "UpToDate"      "0"
    "InstallDir"        ""
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'll need to write your own parser. This might be one possible implementation:

[Code]
function GetInstallDir(const FileName, Section: string): string;
var
  S: string;
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;    
  Lines: TArrayOfString;
begin
  Result := '';
  S := '"' + Section + '"'; // AddQuotes is broken somehow...
  if LoadStringsFromFile(FileName, Lines) then
  begin
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do
      if Trim(Lines[SectionLine]) = S then
      begin
        if (SectionLine < LineCount) and (Trim(Lines[SectionLine + 1]) = '{') then
          for DirLine := SectionLine to LineCount - 1 do
          begin
            if (Pos('"InstallDir"', Lines[DirLine]) > 0) and
              (StringChangeEx(Lines[DirLine], '"InstallDir"', '', True) > 0) then
            begin
              S := RemoveQuotes(Trim(Lines[DirLine]));
              StringChangeEx(S, '\', '', True);
              Result := S;
              Exit;
            end;
            if Trim(Lines[DirLine]) = '}' then
              Exit;
          end;
        Exit;
      end;
  end;
end;

procedure InitializeWizard;
begin                         
  MsgBox(GetInstallDir('d:File.almostjson', '20540'), mbInformation, MB_OK);
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...