Package: glib
CStruct glib:key-file
Declaration(cffi:defcstruct key-file) Details The g:key-file structure lets you parse, edit or create files
containing groups of key-value pairs, which we call key files for lack of a
better name.
Several freedesktop.org specifications use key files now, for example, the Desktop Entry Specification and the Icon Theme Specification. The syntax of key files is described in detail in the Desktop Entry Specification, here is a quick summary. Key files consists of groups of key-value pairs, interspersed with comments. # this is just an example # there can be comments before the first groupLines beginning with a '#' and blank lines are considered comments. Groups are started by a header line containing the group name enclosed in '[' and ']', and ended implicitly by the start of the next group or the end of the file. Each key-value pair must be contained in a group. Key-value pairs generally have the form key=value, with the exception of localized strings, which have the form key[locale]=value, with a locale identifier of the form lang_COUNTRY@MODIFIER where COUNTRY and MODIFIER are optional. Space before and after the '=' character are ignored. Newline, tab, carriage return and backslash characters in value are escaped as n, t, r, and \, respectively. To preserve leading spaces in values, these can also be escaped as s. Key files can store strings, possibly with localized variants, integers, booleans and lists of these. Lists are separated by a separator character, typically ';' or ','. To use the list separator character in a value in a list, it has to be escaped by prefixing it with a backslash. This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some important differences:
Examples
(glib:with-key-file (keyfile)
(let ((path (glib-sys:sys-path "test/resource/key-file.ini")))
;; Load key file
(unless (g:key-file-load-from-file keyfile path :none)
(error "Error loading the key file: KEY-FILE.INI"))
;; Read string from the key file
(let ((value (g:key-file-string keyfile "First Group" "Welcome")))
(unless value
(setf value "default value"))
... )))
Here is an example of creating and saving a key file:
(glib:with-key-file (keyfile)
(let ((path (glib-sys:sys-path "test/resource/key-file.ini")))
;; Load existing key file
(g:key-file-load-from-file keyfile path :none)
;; Add string to the First Group
(setf (g:key-file-string keyfile "First Group" "SomeKey") "New Value"))
;; Save to a file
(let ((path (glib-sys:sys-path "test/out/key-file.ini")))
(unless (g:key-file-save-to-file keyfile path)
(error "Error saving key file.")))
;; Or save to data for use elsewhere
(let ((data (g:key-file-to-data keyfile)))
(unless data
(error "Error saving key file."))
... )) | See also |
2026-02-06