infinity.iniFile¶
Provides functionality for reading and writing structured .ini files.
The file is loaded into memory when the object is created. By default the file is read and written as UTF-8; another charset can be passed to the constructor. Changes are written back to the file automatically when the object is freed, or immediately by calling updateFile().
Floating point values are always written and read with . as decimal separator.
Module: infinity.iniFile
Example:
infinity.loadModule('infinity.iniFile');
let myIniFile = new infinity.iniFile('../test.ini');
try
{
myIniFile.writeFloat('testSection', 'testSetting', 1.1);
myIniFile.writeBoolean('testSection', 'testConfig', true);
myIniFile.writeString('plusSection', 'plusSetting', 'c:/tmp/');
if(myIniFile.sectionExists('testSection')){
console.debug(myIniFile.readSection('testSection'));
//["testSetting","testConfig"]
console.debug(myIniFile.readSectionValues('testSection'));
//[{"name":"testSetting","value":"1.1"},{"name":"testConfig","value":"1"}]
console.debug(myIniFile.readFloat('testSection', 'testSetting', 2.2));
//1.1
console.debug(myIniFile.readBoolean('testSection', 'testSetting', false ));
//false
console.debug(myIniFile.readInteger('testSection', 'testConfig', 2));
//1
console.debug(myIniFile.readString('plusSection', 'plusSetting', 'D:/'));
//c:/tmp/
myIniFile.eraseSection('testSection');
}
console.debug(myIniFile.readSections());
//["plusSection"]
} finally {
myIniFile.free();
}
iniFile¶
The class for operations with ini files.
Methods:
Example:
infinity.loadModule('infinity.iniFile');
let myIniFile = new infinity.iniFile(fileName);
Methods
constructor()¶
Lets you create an INFINITY.JS ini-file object instance and loads the contents of the file into memory. If the file does not exist, an empty ini-file object is created and the file is written as soon as there are changes to save.
Signature:
constructor( fileName: string, encoding?: infinity.encoding )
Parameters:
- fileName:
stringThe name of the ini-file.
- encoding:
infinity.encoding, optionalThe encoding of the file contents. Default is
infinity.encoding.utf8.
Example:
infinity.loadModule('infinity.iniFile');
let myIniFile = new infinity.iniFile('../test.ini');
deleteKey()¶
Deletes an entry with its value from the opened ini-file. If the section contains the key more than once, only the first entry is deleted.
Signature:
deleteKey( section: string, ident: string ): void
Parameters:
-
section:
stringThe ini-file section to delete an entry from.
-
ident:
stringThe key of the entry to be deleted.
Example:
myIniFile.deleteKey('testSection', 'testSetting');
eraseSection()¶
Erases a whole section from the opened ini-file.
Signature:
eraseSection( section: string ): void
Parameters:
- section:
stringThe section to be erased.
Example:
myIniFile.eraseSection('testSection');
free()¶
Writes pending changes to the file and frees up memory occupied by the ini-file object instance. The object must not be used afterwards.
Signature:
free(): void
Example:
myIniFile.free();
readBoolean()¶
Reads a value out of the specified section and key from the opened ini-file and, if possible, returns it as a boolean, otherwise the default value is returned.
Signature:
readBoolean( section: string, ident: string, defaultValue: boolean ): boolean
Parameters:
-
section:
stringThe section to read from.
-
ident:
stringThe key to be read from.
-
defaultValue:
booleanThe default value to return in case of the read value being blank or of another type.
Return type: boolean
Example:
myIniFile.readBoolean('testSection', 'testSetting', true );
readFloat()¶
Reads a value out of the specified section and key from the opened ini-file and, if possible, returns it as a float number, otherwise the default value is returned.
Signature:
readFloat( section: string, ident: string, defaultValue: number ): number
Parameters:
-
section:
stringThe section to read from.
-
ident:
stringThe key to be read from.
-
defaultValue:
numberThe default value to return in case of the read value being blank or of another type.
Return type: number
Example:
myIniFile.readFloat('testSection', 'testSetting', 1.1);
readInteger()¶
Reads a value (up to 32 bits) out of the specified section and key from the opened ini-file and, if possible, returns it as an integer number, otherwise the default value is returned.
Signature:
readInteger( section: string, ident: string, defaultValue: number ): number
Parameters:
-
section:
stringThe section to read from.
-
ident:
stringThe key to be read from.
-
defaultValue:
numberThe default value to return in case of the read value being blank or of another type.
Return type: number
Example:
myIniFile.readInteger('testSection', 'testSetting', 3);
readInt64()¶
Reads a value (up to 64 bits) out of the specified section and key from the opened ini-file and, if possible, returns it as an integer number, otherwise the default value is returned.
Signature:
readInt64( section: string, ident: string, defaultValue: number ): number
Parameters:
-
section:
stringThe section to read from.
-
ident:
stringThe key to be read from.
-
defaultValue:
numberThe default value to return in case of the read value being blank or of another type.
Return type: number
Example:
myIniFile.readInt64('testSection', 'testSetting', 5147483647);
readSection()¶
Returns an array with the names of the setting keys of the specified section.
Signature:
readSection( section: string ): infinity.iniFile.stringArray
Parameters:
- section:
stringThe section to read from.
Return type: infinity.iniFile.stringArray
Example:
myIniFile.readSection('testSection');
readSectionValues()¶
Returns an array with the section's key names and values.
Signature:
readSectionValues( section: string ): infinity.iniFile.valueArray
Parameters:
- section:
stringThe section to read from.
Return type: infinity.iniFile.valueArray
Example:
myIniFile.readSectionValues('testSection');
readSections()¶
Returns an array of the sections present in the opened ini-file.
Signature:
readSections(): infinity.iniFile.stringArray
Return type: infinity.iniFile.stringArray
Example:
myIniFile.readSections();
readString()¶
Reads a value out of the opened ini-file and returns it as a string.
Signature:
readString( section: string, ident: string, defaultValue: string ): string
Parameters:
-
section:
stringThe section to read from.
-
ident:
stringThe key to be read from.
-
defaultValue:
stringThe default value to return in case of the read value being blank or of another type.
Return type: string
Example:
myIniFile.readString('testSection', 'testSetting', 'c:/tmp/');
sectionExists()¶
Specifies whether the given section exists inside the opened ini-file.
Signature:
sectionExists( section: string ): boolean
Parameters:
- section:
stringThe section to check for.
Return type: boolean
Example:
if(myIniFile.sectionExists('testSection')){
//...
}
updateFile()¶
Writes the current in-memory state of the ini-file to the file.
Signature:
updateFile(): void
Example:
myIniFile.updateFile();
valueExists()¶
Specifies whether the given key exists in the specified section of the opened ini-file.
Signature:
valueExists( section: string, ident: string ): boolean
Parameters:
-
section:
stringThe section to check in.
-
ident:
stringThe key to be read from.
Return type: boolean
Example:
myIniFile.valueExists('testSection', 'testSetting');
writeBoolean()¶
Writes the given boolean value to the specified key and section of the opened ini-file. The value is stored as 1 (true) or 0 (false).
Signature:
writeBoolean( section: string, ident: string, value: boolean ): void
Parameters:
-
section:
stringThe section to write to.
-
ident:
stringThe key to write to.
-
value:
booleanThe value to write.
Example:
myIniFile.writeBoolean('testSection', 'testConfig', true);
writeFloat()¶
Writes the given float value to the specified key and section of the opened ini-file.
Signature:
writeFloat( section: string, ident: string, value: number ): void
Parameters:
-
section:
stringThe section to write to.
-
ident:
stringThe key to write to.
-
value:
numberThe value to write.
Example:
myIniFile.writeFloat('testSection', 'testSetting', 1.1);
writeInteger()¶
Writes the given integer value (up to 32 bits) to the specified key and section of the opened ini-file.
Signature:
writeInteger( section: string, ident: string, value: number ): void
Parameters:
-
section:
stringThe section to write to.
-
ident:
stringThe key to write to.
-
value:
numberThe value to write.
Example:
myIniFile.writeInteger('testSection', 'testSetting', 3);
writeInt64()¶
Writes the given integer value (up to 64 bits) to the specified key and section of the opened ini-file.
Signature:
writeInt64( section: string, ident: string, value: number ): void
Parameters:
-
section:
stringThe section to write to.
-
ident:
stringThe key to write to.
-
value:
numberThe value to write.
Example:
myIniFile.writeInt64('testSection', 'testSetting', 5147483647);
writeString()¶
Writes the given string value to the specified key and section of the opened ini-file.
Signature:
writeString( section: string, ident: string, value: string ): void
Parameters:
-
section:
stringThe section to write to.
-
ident:
stringThe key to write to.
-
value:
stringThe value to write.
Example:
myIniFile.writeString('plusSection', 'plusSetting', 'c:/tmp/');
stringArray¶
Extends: Array<string>
An array of strings.
valueArray¶
Extends: Array<{name: string, value: string}>
An array of keys and values.