Author Topic: WriteINI Procedure  (Read 2440 times)

0 Members and 1 Guest are viewing this topic.

Offline Avkon

  • Soldier
  • **
  • Posts: 136
  • Ah, whatever.
WriteINI Procedure
« on: July 22, 2007, 08:35:50 am »
This is a small modification of DeMo's WConf procedure, in the sense that this can be used exactly in the same way as ReadINI.

Call:

Code: [Select]
WriteINI(FileName: String; Text: String);
Where FileName is the name of the INI file you want to edit, and Text contains 'SECTION key Value'. Unlike DeMo's version, the value can be any number of words.

An example would be:

Code: [Select]
WriteINI('soldat.ini'; 'NETWORK Server_Name The Devil's Playground);
Which renames your server to "The Devil's Playground".

Well, here's the code:

Code: [Select]
procedure WriteINI(FileName: String; Text: String);
var
  section, key, data, value : string;
  args, strlist : TStringArray;
  i : integer;
begin
  args := xsplit(Text, ' ');

  if (ArrayHigh(args) < 2) then begin
    WriteLn('Write Error: All parameters not specified!');
    exit;
  end;

  section := uppercase(args[0]);
  key := args[1];

  value := args[2];
  for i := 3 to ArrayHigh(args) do begin
    value := value + ' ' + args[i];
  end;

  strlist := xsplit(ReadFile(FileName), chr(13)+chr(10));
  for i := 0 to ArrayHigh(strlist) do begin
    if (MaskCheck(uppercase(strlist[i]), uppercase(key + '=*')) = True) then begin
      strlist[i] := key + '=' + value;
      WriteLn('Write Successful: ' + key + ' changed to: ' + value + '!');
      break;
    end
    else begin
      if (i = ArrayHigh(strlist)) then begin
        WriteLn('Write Error: Invalid key (' + key + ')!');
        exit;
      end;
    end;
  end;

  data := strlist[0];
  for i := 1 to ArrayHigh(strlist) do begin
    data := data + chr(13)+chr(10) + strlist[i];
  end;
  WriteFile(FileName, data);
  WriteLn('Write Successful: ' + FileName + 'updated!');
end;

Stick it in Core.pas. KeyDoN's XSplit  Function is required for WriteINI to run properly. Remember to call '/loadcon' after you've modified any INIs.

I'd appreciate any feedback.  :)