TIniFile.ReadString

From Soldat Community Wiki
Revision as of 01:38, 21 August 2013 by Mighty (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
function ReadString(const Section, Ident, Default: string): string
 Section: Section name
 Ident: Property name (key)
 Default: Value used as a result when the key has not been found
 Result: Value of a key (if successful) or Default (if not)

Description

This function will try to read a string value from specified section and key
If it fails to do so for some reason, the Default value is returned.
Note that StripQuotes might affect the result!

Example

example.ini in soldatserver's root folder

[ModConfig]
Name=Knife Only
SayHi="Hello"

script code:

const
  // assuming Sandboxed Level < 2
  PATH = 'example.ini';
var
  Ini: TIniFile;
  temp: string;

begin
  Ini := File.CreateINI(PATH);
  temp := Ini.ReadString('modconfig','name','ERROR'); // temp = Knife Only
  temp := Ini.ReadString('modconfig','sayhi','ERROR'); // temp = "Hello"
  Ini.StripQuotes := TRUE;
  temp := Ini.ReadString('modconfig','name','ERROR'); // temp = Knife Only
  temp := Ini.ReadString('modconfig','sayhi','ERROR'); // temp = Hello
  
  temp := Ini.ReadString('modconfig','ImNotThere','ERROR'); // temp = ERROR
  Ini.Free;
end.