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% Racket Class
ini%
new
get-file
set-file!
get-fail
set-fail!
get-contents
contents
reload
set!
get
4 The ini Roos Class
%-!
file
file!
fail
fail!
reload
set!
get
8.18

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. If the file does noet exist, an empty ini structure is made. If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini".

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. If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini".

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% Racket Class🔗ℹ

 (require simple-ini/class) package: simple-ini

Require this module for the OO implementation of this Simple INI implementation

class

ini% : class?

  superclass: object%

An OO wrapper around the ini functions.

constructor

(new ini% [[file file] [fail fail]])  (is-a?/c ini%)

  file : (or/c symbol? string? path? boolean?) = #f
  fail : (or/c boolean?) = #f
Creates the ini from the given file. * If (eq? file #f), an empty ini will be made. * if (symbol? file), an ini will be made or read in the users preferences folder with the given (format "~a.ini" file) as name. * Otherwise, the file will be made or read at the given location.

The fail flag determines if methods of the class will fail when some value in the ini file is written while there is no file to write to or if some non existing key is read.

method

(send an-ini get-file)  path?

Gets the current ini file. See constructor for more information.

method

(send an-ini set-file! file)  this

  file : (or/c symbol? string? path?)
Sets the ini file to be used. See constructor for more information.

method

(send an-ini get-fail)  boolean?

Gets the value of the ’fail’ flag. See constructor for more information.

method

(send an-ini set-fail! fail)  this

  fail : boolean?
Sets the value of the ’fail’ flag. See constructor for more information.

method

(send an-ini get-contents)  list?

Gets the contents of the ini structure as stored in memory.

method

(send an-ini contents)  list?

See get-contents.

method

(send an-ini reload)  this

Reloads the ini file in memory, or empties the ini structure (eq? file #f).

method

(send an-ini set! section key value)  this

  section : (or/c symbol? string?)
  key : (or/c symbol? string?)
  value : any/c?
Sets the value of the key in the given section. After the set! operation, the ini structure will be written to file. Note. Although ini files can be read from standard .ini formats, the simple-ini format will be enhanced. It wil store values in racket format, so that ’read’ can be used to read in the racket value.

method

(send an-ini get section key default-value)  any/c?

  section : (or/c symbol? string?)
  key : (or/c symbol? string?)
  default-value : any/c?
(send an-ini get section key)  any/c
  section : (or/c symbol? string?)
  key : (or/c symbol? string)
Returns the value for the given section and key combination. If this combination does not exist in the ini structure, it will return the default-value. However, if default-value is not given, it will return #f.

; end class

4 The ini Roos Class🔗ℹ

Require this module for the OO implementation of this Simple INI implementation in the ROOS OO framework
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