Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在Inno安装程序中使用Pascal脚本从配置文件中查找并读取特定字符串_String_Search_Find_Extract_Inno Setup - Fatal编程技术网

String 在Inno安装程序中使用Pascal脚本从配置文件中查找并读取特定字符串

String 在Inno安装程序中使用Pascal脚本从配置文件中查找并读取特定字符串,string,search,find,extract,inno-setup,String,Search,Find,Extract,Inno Setup,我有很长的配置文件,我需要从文件中提取特定的字符串。 我要提取/读取的是特定编号位置的InstallDir,例如20540 我知道如何在INI或XML中查找字符串,但无法处理这种形式的文件 显示结构的文件片段: "212280" { "InstallDir" "D:\\XYZ\\stu\\opr" "UpdateKBtoDL" "0" "HasAllLocalContent" "1" "UpToDate" "1"

我有很长的配置文件,我需要从文件中提取特定的字符串。 我要提取/读取的是特定编号位置的InstallDir,例如20540

我知道如何在INI或XML中查找字符串,但无法处理这种形式的文件

显示结构的文件片段:

"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"        ""
}

您需要编写自己的解析器。这可能是一种可能的实现方式:

[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;
[代码]
函数GetInstallDir(const FileName,Section:string):string;
变量
S:字符串;
DirLine:整数;
行数:整数;
剖面线:整数;
线条:柏油纹;
开始
结果:='';
S:='“'+节+”;//AddQuotes不知怎么坏了。。。
如果LoadStringsFromFile(文件名,行),则
开始
LineCount:=GetArrayLength(行);
对于SectionLine:=0到LineCount-1 do
如果修剪(直线[剖面线])=S,则
开始
如果(SectionLine0)和
(StringChangeEx(行[DirLine],'InstallDir','',True)>0)然后
开始
S:=删除引号(修剪(直线[DirLine]);
StringChangeEx(S,'\\','\',True);
结果:=S;
出口
结束;
如果修剪(直线[DirLine])='}',则
出口
结束;
出口
结束;
结束;
结束;
程序初始化;
开始
MsgBox(GetInstallDir('d:\File.almostjson','20540'),mbInformation,MB_OK);
结束;

除了为这种几乎是JSON类型的文件编写自己的解析器之外,别无选择。这是一个很大的工作…对。。。生活不可能简单;-)我考虑的是这样的流:查找数字位置(因为它是唯一的),然后读取数字后面的{…}块,并从该块中提取InstallDir字符串。