INI File Parser and Writer
1 Creating and Parsing INI Files
make-ini
file->ini
ini->file
2 Accessing and Modifying Values
ini-get
ini-set!
3 The ini Roos Class
-!
file
file!
fail
fail!
reload
set!
get
8.17.0.6

INI File Parser and Writer🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

(define myeval (make-base-eval ’(require simple-ini roos)))

This module provides simple facilities for reading and writing INI-style configuration files, allowing interpretation of numeric and boolean values, and modification of the parsed structure.

1 Creating and Parsing INI Files🔗ℹ

procedure

(make-ini)  mc-pair?

Creates a new empty INI structure as a mutable cons pair. This is the base structure for manipulating INI contents.

procedure

(file->ini file)  mc-pair?

  file : path-string?
Reads an INI file from disk and parses it into an internal mutable cons pair (mc-pair) structure.

The parser supports:

  • Sections (e.g., [section-name])

  • Key-value pairs (e.g., key=value)

  • Comments (lines starting with ;)

  • Empty lines

The keys are stored as symbols, and values are automatically interpreted as:
  • Numbers, if the value matches a number pattern

  • Booleans, if the value is #t, true, #f, or false (case-insensitive)

  • Otherwise, as strings

procedure

(ini->file ini file)  void?

  ini : mc-pair?
  file : path-string?
Writes an INI structure (as produced by file->ini or make-ini) to the specified file.

The output preserves:
  • Section headers

  • Key-value pairs

  • Comments (prefixed with ;)

  • Empty lines

2 Accessing and Modifying Values🔗ℹ

procedure

(ini-get ini section key def-val)  any/c

  ini : mc-pair?
  section : symbol?
  key : symbol?
  def-val : any/c
Retrieves the value associated with the given key in the specified section of the INI structure. If the key is not found, returns def-val.

procedure

(ini-set! ini section key val)  mc-pair?

  ini : mc-pair?
  section : symbol?
  key : symbol?
  val : any/c
Sets the value of key in the specified section of the INI structure. If the section or key does not exist, it is created. Returns the modified INI structure.

3 The ini Roos Class🔗ℹ

Require this module for the OO implementation of this Simple INI implementation
Provides a Roos class that gives object-oriented access to INI files using the underlying file->ini parser system. The class offers methods to load, query, and update INI files using familiar object-style interactions.

procedure

(-! ini or/c)  roos-object*

  ini : roos-class*
  or/c : path-string?
Creates an ini object. If a file path is provided and the file exists, it is loaded immediately. Otherwise, an empty INI structure is created.

If no file is provided, the object operates in-memory only. Subsequent set! operations will raise an error unless a file is later specified with (file!).

procedure

(file)  (or/c path-string? #f)

Returns the current filename associated with this INI object.

procedure

(file! f)  void?

  f : path-string?
Sets the file to use as backing storage for the INI structure. Triggers a reload from disk.

procedure

(fail)  boolean?

Returns if an error will be thrown when a set! is done and no file has been set to write the contents to

procedure

(fail! yn)  boolean?

  yn : boolean?
Sets fail to the given value.

procedure

(reload)  void?

Reloads the INI content from disk, using the current file path. If the file does not exist, the content is reset to an empty INI structure.

procedure

(set! section key val)  ini

  section : symbol?
  key : symbol?
  val : any/c
Sets the value in the INI structure for the given section and key to val.

If a file is associated with the object, the structure is saved to disk immediately. If no file is set and fail is enabled, an error is raised. Returns the INI object itself.

procedure

(get section key def-val ...)  any/c

  section : symbol?
  key : symbol?
  def-val : any/c
Retrieves the value associated with the given section and key.

If not found:
  • Returns #f if no default is given and fail is disabled

  • Returns def-val if one is provided

  • Raises an error if fail is enabled and no default is given

Internal state:
  • file* — the filename (or #f)

  • content — the mutable INI structure