Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]

Arc de Triomphe clipart (2486 bytes)INI-file Reader

Written by Roger Browne.

ini-file.zip (3469 bytes)


The class INI_FILE makes it easy to read items from INI-format ("initialization") files.

The format of INI-files is defined by Microsoft in the Windows Software Development Kit. INI-files are text files, comprising zero or more sections. The section names are enclosed in square brackets. Within each section there may be zero or more key-item pairs, separated by an equals sign. Comments are introduced by a semicolon.

Section names and key names are case-insensitive. Blank lines may appear anywhere within the file. Here's an example of a small INI-file:

; This is a comment.

To allow an INI file to be used with multiple versions of its associated application, it is not considered an error for a requested section or a requested key to be absent. In these cases, a user-supplied default value will be returned.


The public interface of class INI_FILE is very simple:

-- Commands

set_file(fn: STRING)
-- Set the name of the INI file.
-- Use a full pathname, or prepend a relative path (e.g. ".\" for the
-- current directory), or supply a simple filename (in which case
-- Windows will look in its default directories for the INI file).
require nonvoid_fn: fn /= void

set_section(sn: STRING)
-- Set the section name within the INI file.
require nonvoid_sn: sn /= void

-- Queries

string_item(key_name: STRING; default_value: STRING): STRING is
require nonvoid_key_name: key_name /= void; nonvoid_default_value: default_value /= void
ensure nonvoid_result: result /= void; result_not_aliased: result /= default_value

integer_item(key_name: STRING; default_value: INTEGER): INTEGER is
require nonvoid_key_name: key_name /= void

boolean_item(key_name: STRING; default_value: BOOLEAN): BOOLEAN is
require nonvoid_key_name: key_name /= void


Here's how to use class INI_FILE to read items from a file named "example.ini":

ini_file: expanded INI_FILE; s: STRING; i: INTEGER; b: BOOLEAN
s := ini_file.string_item("model", "unknown")
i := ini_file.integer_item("pixels", 640)
b := ini_file.boolean_item("supports_3D", false)

If you have any queries about this library, please contact the author by email (roger@eiffel.demon.co.uk).

Home Page ] [ Eiffel Archive ] [ Eiffel Classes and Clusters ]