nsIINIParser

An instance of nsIINIParser can be used to read values from an INI file.
Inherits from: nsISupports Last changed in Gecko 1.8 (Firefox 1.5 / Thunderbird 1.5 / SeaMonkey 1.0)

Typically, you'll create an nsIINIParser object by calling nsIINIParserFactory.createINIParser(). Also, if you need to write to an INI file, use nsIINIParserWriter.

nsIUTF8StringEnumerator getKeys(in AUTF8String aSection);
nsIUTF8StringEnumerator getSections();
AUTF8String getString(in AUTF8String aSection, in AUTF8String aKey);

Methods

getKeys()

Returns an nsIUTF8StringEnumerator providing the keys available within the specified section of the INI file.

nsIUTF8StringEnumerator getKeys(
  in AUTF8String aSection
);
Parameters
aSection
The name of the section whose keys you wish to enumerate.
Return value

An nsIUTF8StringEnumerator object that can be used to access the section's keys.

getSections()

Returns an nsIUTF8StringEnumerator providing a list of the sections available within the INI file.

nsIUTF8StringEnumerator getSections();
Parameters

None.

Return value

An nsIUTF8StringEnumerator object that can be used to access the sections in the INI file.

getString()

Returns the string value for the specified key within a particular section of the INI file.

AUTF8String getString(
  in AUTF8String aSection,
  in AUTF8String aKey
);
Parameters
aSection
The section containing the key whose value is to be returned.
aKey
The key for which the value should be returned.
Return value

The string value of the specified key.

Examples

Getting a value for a key

This example provides a function you can call to obtain the value for a specific key in a given INI file.

/**
 * Returns the value for a given property in an INI file
 * @param {nsIFile} iniFile
 *   The ini file to get the value from
 * @param {String} section
 *   The name of the section in the ini file. INI sections are
 *   defined by square brakets and look like this: [Settings]
 *   All entries below such a section definition belong to that
 *   section (until the next section).
 * @param {String} prop
 *   The name of the property to get.
 */
function getIniValue (iniFile, section, prop) {
  var iniFact = Components.manager.getClassObjectByContractID(
    "@mozilla.org/xpcom/ini-parser-factory;1",
    Components.interfaces.nsIINIParserFactory
  );
  var iniParser = iniFact.createINIParser(iniFile);
  try {
    return iniParser.getString(section,prop);
  }
  catch(e) {
    return undefined;
  }
}
// Usage:
var lang = getIniValue(file,"Setting","Language"); 

Enumerating sections

This example gets a list of all the sections in the INI file.

// get all sections in the ini file
var sectEnum = iniParser.getSections();
// save the sections in an array
var sections = [];
while (sectEnum && sectEnum.hasMore()) {
  var section = sectEnum.getNext();
  // add an array entry for each section
  sections.push(section);
}

Enumerating keys

This example shows how to fetch the values for all the keys in the section named "Setting".

// get all keys for the "Setting" section
var keysEnum = iniParser.getKeys("Setting");
// save the keys and values in an object
var keys = {};
while (keysEnum && keysEnum.hasMore()) {
  var key = keysEnum.getNext();
  // create a property for each key and assing the ini value.
  keys[key] = iniParser.getString("Setting", key);
} 

See Also

Document Tags and Contributors

 Contributors to this page: Sheppy, Yuan.Xulei, jhammel, Kirschkern
 Last updated by: Sheppy,