Package: glib

CStruct glib: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 group

[First Group]

Name=Key File Example this value shows escaping

# localized strings are stored in multiple key-value pairs Welcome=Hello Welcome[de]=Hallo Welcome[fr_FR]=Bonjour Welcome[it]=Ciao Welcome[be@latin]=Hello

[Another Group]

Numbers=2;20;-200;0 Booleans=true;false;true;true
Lines 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_COUNTRYMODIFIER 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:
  • .ini files use the ';' character to begin comments, key files use the '#' character.
  • Key files do not allow for ungrouped keys meaning only comments can precede the first group.
  • Key files are always encoded in UTF-8.
  • Key and Group names are case-sensitive. For example, a group called [GROUP] is a different from [group].
  • .ini files do not have a strongly typed boolean entry type, they only have GetProfileInt(). In key files, only true and false (in lower case) are allowed.
Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same key multiple times. The last entry wins. Key files may also contain multiple groups with the same name. They are merged together. Another difference is that keys and group names in key files are not restricted to ASCII characters.

This is a list of standard group and key names for key files.
"Desktop Entry"
The name of the main group of a desktop entry file, as defined in the Desktop Entry Specification. Consult the specification for more details about the meanings of the keys below.
"Type"
A key under "Desktop Entry", whose value is a string giving the type of the desktop entry. Usually "Application", "Link", or "Directory".
"Version"
A key under "Desktop Entry", whose value is a string giving the version of the Desktop Entry Specification used for the desktop entry file.
"Name"
A key under "Desktop Entry", whose value is a localized string giving the specific name of the desktop entry.
"GenericName"
A key under "Desktop Entry", whose value is a localized string giving the generic name of the desktop entry.
"NoDisplay"
A key under "Desktop Entry", whose value is a boolean stating whether the desktop entry should be shown in menus.
"Comment"
A key under "Desktop Entry", whose value is a localized string giving the tooltip for the desktop entry.
"Icon"
A key under "Desktop Entry", whose value is a localized string giving the name of the icon to be displayed for the desktop entry.
"Hidden"
A key under "Desktop Entry", whose value is a boolean stating whether the desktop entry has been deleted by the user.
"OnlyShowIn"
A key under "Desktop Entry", whose value is a list of strings identifying the environments that should display the desktop entry.
"NotShowIn"
A key under "Desktop Entry", whose value is a list of strings identifying the environments that should not display the desktop entry.
"TryExec"
A key under "Desktop Entry", whose value is a string giving the file name of a binary on disk used to determine if the program is actually installed. It is only valid for desktop entries with the Application type.
"Exec"
A key under "Desktop Entry", whose value is a string giving the command line to execute. It is only valid for desktop entries with the Application type.
"Path"
A key under "Desktop Entry", whose value is a string containing the working directory to run the program in. It is only valid for desktop entries with the Application type.
"Terminal"
A key under "Desktop Entry", whose value is a boolean stating whether the program should be run in a terminal window. It is only valid for desktop entries with the Application type.
"MimeType"
A key under "Desktop Entry", whose value is a list of strings giving the MIME types supported by this desktop entry.
"Categories"
A key under "Desktop Entry", whose value is a list of strings giving the categories in which the desktop entry should be shown in a menu.
"StartupNotify"
A key under "Desktop Entry", whose value is a boolean stating whether the application supports the Startup Notification Protocol Specification.
"StartupWMClass"
A key under "Desktop Entry", whose value is string identifying the WM class or name hint of a window that the application will create, which can be used to emulate Startup Notification with older applications.
"URL"
A key under "Desktop Entry", whose value is a string giving the URL to access. It is only valid for desktop entries with the Link type.
"Application"
The value of the "Type", key for desktop entries representing applications.
"Link"
The value of the "Type", key for desktop entries representing links to documents.
"Directory"
The value of the "Type", key for desktop entries representing directories.

Examples

Here is an example of loading a key file and reading a value:
(g:with-key-file (keyfile)
  ;; Load the key file
  (unless (g:key-file-load-from-file keyfile "rtest-glib-key-file.ini" :none)
    (error "Error loading the key file: RTEST-GLIB-KEY-FILE.INI"))
  ;; Read a 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:
(g:with-key-file (keyfile)
  ;; Load existing key file
  (g:key-file-load-from-file keyfile "rtest-glib-key-file.ini" :none)
  ;; Add a string to the First Group
  (setf (g:key-file-string keyfile "First Group" "SomeKey") "New Value")
  ;; Save to a file
  (unless (g:key-file-save-to-file keyfile "rtest-glib-key-file-example.ini")
    (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

2025-1-12