inno-setuppascalscript

Get a value from key=value text file (without sections) in Inno Setup?


I want to obtain some values from a text file (Database Name, Username, Password), I found a way to do it, but not sure if is the best way, cause the password return with some kind of "tab spaces" at the end, is this the correct way or there is another way to do it more easy?

This is my text file:

javax.persistence.jdbc.url=jdbc:mysql://address=(protocol=tcp)(host=127.0.0.1)(port=3306)/A_DBNAME
javax.persistence.jdbc.user=A_USER
javax.persistence.jdbc.password=A_PASSWORD

And this is my code:

function StrSplit(Text: String; Separator: String): TArrayOfString;
var
  i, p: Integer;
  Dest: TArrayOfString; 
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := Copy(Text, 1, p-1);
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := Text;
      Text := '';
    end;
  until Length(Text)=0;
  Result := Dest
end;
    
procedure InitializeWizard;
var
 file : AnsiString;
 dbName, pass, user : TArrayOfString;
begin
  LoadStringFromFile( ExpandConstant('{src}\jdbcfile.txt'), file);  
  user := StrSplit(file, 'javax.persistence.jdbc.user=');
  user := StrSplit(user[1],'javax.persistence.jdbc.password=')         
  MsgBox('User: '+user[0],mbInformation,MB_OK);
  pass := StrSplit(file,'javax.persistence.jdbc.password=');  
  MsgBox('Pass: '+pass[1],mbInformation,MB_OK);
  dbName := StrSplit(file, ')/');
  dbName := StrSplit(dbName[1],'javax.persistence.jdbc.user=');
  MsgBox('dbName: '+dbName[0],mbInformation,MB_OK);
end;

Solution

  • Imo, it's conceptually wrong to try to parse the overall file structure (key=value) and the individual key value (.url) at the same time. Moreover you rely on a specific key order in the file, what is wrong too.

    function GetKeyValue(Lines: TStrings; Key: string): string;
    var
      I, P: Integer;
    begin
      for I := 0 to Lines.Count - 1 do
      begin
        P := Pos('=', Lines[I]);
        if (P > 0) and (CompareText(Trim(Copy(Lines[I], 1, P - 1)), Key) = 0) then
        begin
          Result := Trim(Copy(Lines[I], P + 1, Length(Lines[I]) - P));
          Exit;
        end;
      end;
      // Implicitly returns an empty string, if the key does not exist.
      // Alternatively you can also throw an exception
      // by using RaiseException function, or return some default
      // value [by adding an additional parameter to this function].
    end;
    
    procedure InitializeWizard;
    var
      Lines: TStringList;
      DbName, User, Password: string;
    begin
      Lines := TStringList.Create;
      Lines.LoadFromFile(ExpandConstant('{src}\jdbcfile.txt'));
    
      DbName := ExtractFileName(GetKeyValue(Lines, 'javax.persistence.jdbc.url'));
      User := GetKeyValue(Lines, 'javax.persistence.jdbc.user');
      Password := GetKeyValue(Lines, 'javax.persistence.jdbc.password');
    
      Lines.Free;
      // ...
    end;