You are looking at historical revision 45247 of this page. It may differ significantly from its current revision.

srfi-233

Description

Implements https://srfi.schemers.org/srfi-233/srfi-233.html for parsing INI configuration files.

Specification

An INI file is a simple line-based configuration format. There are many variations; this SRFI provides support for at least the following:

Comments begin with ; and are removed from lines. The comment delimiter can be overridden.

Blank lines and leading and trailing whitespace are ignored. For the purposes of this SRFI, "whitespace" means any sequence of spaces and/or tabs.

The beginning of a section is marked by a line beginning with [ and ending with ]. Sections do not nest. The name of a section is the characters between the brackets.

Other lines containing = are treated as key-value pairs within the current section or, if they precede any section line, as key-value pairs belonging to a section whose name is #f. Whitespace immediately before or after the = is ignored. If there is more than one key-value separator, all but the first are considered part of the value. The key-value separator can be overridden.

Any other lines are treated as keys whose value is #f.

API

make-ini-file-generator

[procedure] (make-ini-file-generator inport #!optional (key-value-sep #\=) (comment-delim #\;))

Returns a generator that reads one or more lines from the textual input port inport and returns a list of two symbols and a string: the current section name, the key, and the value. If no section names have been read, the section name is #f. When the port returns an end-of-file object, the generator returns an end-of-file object. It is an error to mutate any of the strings and lists returned.

The character key-value-sep specifies the separator between a key and the corresponding value. The character comment-delim specifies the delimiter before a comment. It is an error if either of them is whitespace or a newline.

Note that it is the user's responsibility to close the port.

make-ini-file-accumulator

[procedure] (make-ini-file-accumulator outport #!optional (key-value-sep #\=) (comment-delim #\;))

Returns an accumulator that writes to the textual output port outport.

If the argument passed to the accumulator is a list of three elements, a key-value line, preceded if necessary by a section line, is written.

If the argument is a single string, it is prefixed by the comment delimiter and a single space and written out.

In either case, the accumulator returns an unspecified value. It is an error if any of these strings contains a newline. If the argument is an end-of-file object, the end-of-file object is returned. It is an error to call the accumulator after that.

Note that it is the user's responsibility to close the port.

The key-value-sep and comment-delim are treated the same as in make-ini-file-generator.

Changelog

License

SPDX-License-Identifier: MIT

Copyright (c) 2022 John Cowan, Arvydas Silanskas.
Copyright (c) 2025 Lilianna Smólska

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.