INI File Parser and Writer
| (require simple-ini) | package: simple-ini |
1 Creating and Parsing INI Files
procedure
file : (or/c path-string? symbol?)
The parser supports:
Sections (e.g., [section-name])
Key-value pairs (e.g., key=value)
Comments (lines starting with ;)
Empty lines
An unrecognized non-empty line raises an exception that includes its line number and contents.
Numbers, if the value matches a number pattern
Booleans, if the value is #t, true, #f, or false (case-insensitive)
Paths written with the PATH: prefix
Explicit Racket-readable strings written with the VALUE: prefix, including strings containing newlines
Serializable Racket values written with the SER: prefix
Otherwise, as strings
procedure
(ini->file ini file [#:private? private?]) → void?
ini : is-ini? file : path-string? private? : boolean? = #f
Section headers
Key-value pairs
Comments (prefixed with ;)
Empty lines
Strings, paths, numbers, and booleans have dedicated encodings. Other values are written using serialize and must therefore be serializable.
procedure
(get-ini-file file) → path?
file : (or/c path-string? symbol?)
procedure
(is-ini? value) → boolean?
value : any/c
2 Accessing and Modifying Values
procedure
ini : is-ini? section : (or/c symbol? string?) key : (or/c symbol? string?) def-val : any/c
3 Inspecting and Iterating over INI Values
procedure
(ini-sections ini) → (listof symbol?)
ini : is-ini?
procedure
(ini-for-each proc ini) → void?
proc : procedure? ini : is-ini?
> (define settings (make-ini)) > (ini-set! settings 'server 'port 8080) #<ini-cfg>
> (ini-set! settings 'server 'tls #t) #<ini-cfg>
> (ini-sections settings) '(server)
> (ini-keys settings 'server) '(port tls)
> (ini-for-each (lambda (section key value) (printf "~a.~a = ~v~n" section key value)) settings)
server.port = 8080
server.tls = #t
4 The ini% Racket Class
| (require simple-ini/class) | package: simple-ini |
Require this module for the OO implementation of this Simple INI implementation
constructor
(new ini% [ [file file] [fail fail] [private? private?]]) → (is-a?/c ini%) file : (or/c symbol? string? path? boolean?) = #f fail : (or/c boolean?) = #f private? : (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.
Gets the current ini file. See constructor for more information.Sets the ini file to be used. See constructor for more information.Gets the value of the ’fail’ flag. See constructor for more information.Sets the value of the ’fail’ flag. See constructor for more information.Returns whether private file permissions are enabled.Enables or disables private file permissions for subsequent writes.Returns the named sections in source order, as with ini-sections.Returns the keys in section, as with ini-keys.
method
proc : procedure? Calls proc with the section, key, and value of each key-value pair, as with ini-for-each.Reloads the ini file in memory, or empties the ini structure (eq? file #f).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.
5 The ini Roos Class
| (require simple-ini/roos) | package: simple-ini |
procedure
ini : roos-class* or/c : path-string?
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)
procedure
(file! f) → void?
f : path-string?
procedure
(fail) → boolean?
procedure
(reload) → void?
procedure
(sections) → (listof symbol?)
procedure
proc : procedure?
procedure
(set! section key val) → ini
section : (or/c symbol? string?) key : (or/c symbol? string?) val : any/c
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.
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