Struct KeyFile

Source
pub struct KeyFile { /* private fields */ }
Expand description

GKeyFile parses .ini-like config files.

GKeyFile 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. 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.

⚠️ The following code is in txt ⚠️

# this is just an example
# there can be comments before the first group

[First Group]

Name=Key File Example\tthis value shows\nescaping

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

[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_COUNTRY@MODIFIER where COUNTRY and MODIFIER are optional. As a special case, the locale C is associated with the untranslated pair key=value (since GLib 2.84). Space before and after the = character is 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 don’t 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.

Here is an example of loading a key file and reading a value:

⚠️ The following code is in c ⚠️

g_autoptr(GError) error = NULL;
g_autoptr(GKeyFile) key_file = g_key_file_new ();

if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
  {
    if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
      g_warning ("Error loading key file: %s", error->message);
    return;
  }

g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
if (val == NULL &&
    !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
  {
    g_warning ("Error finding key in key file: %s", error->message);
    return;
  }
else if (val == NULL)
  {
    // Fall back to a default value.
    val = g_strdup ("default-value");
  }

Here is an example of creating and saving a key file:

⚠️ The following code is in c ⚠️

g_autoptr(GKeyFile) key_file = g_key_file_new ();
const gchar *val = …;
g_autoptr(GError) error = NULL;

g_key_file_set_string (key_file, "Group Name", "SomeKey", val);

// Save as a file.
if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
  {
    g_warning ("Error saving key file: %s", error->message);
    return;
  }

// Or store to a GBytes for use elsewhere.
gsize data_len;
g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
if (data == NULL)
  {
    g_warning ("Error saving key file: %s", error->message);
    return;
  }
g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);

GKeyFile parses .ini-like config files.

GKeyFile 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. 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.

⚠️ The following code is in txt ⚠️

# this is just an example
# there can be comments before the first group

[First Group]

Name=Key File Example\tthis value shows\nescaping

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

[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_COUNTRY@MODIFIER where COUNTRY and MODIFIER are optional. As a special case, the locale C is associated with the untranslated pair key=value (since GLib 2.84). Space before and after the = character is 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 don’t 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.

Here is an example of loading a key file and reading a value:

⚠️ The following code is in c ⚠️

g_autoptr(GError) error = NULL;
g_autoptr(GKeyFile) key_file = g_key_file_new ();

if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
  {
    if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
      g_warning ("Error loading key file: %s", error->message);
    return;
  }

g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
if (val == NULL &&
    !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
  {
    g_warning ("Error finding key in key file: %s", error->message);
    return;
  }
else if (val == NULL)
  {
    // Fall back to a default value.
    val = g_strdup ("default-value");
  }

Here is an example of creating and saving a key file:

⚠️ The following code is in c ⚠️

g_autoptr(GKeyFile) key_file = g_key_file_new ();
const gchar *val = …;
g_autoptr(GError) error = NULL;

g_key_file_set_string (key_file, "Group Name", "SomeKey", val);

// Save as a file.
if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
  {
    g_warning ("Error saving key file: %s", error->message);
    return;
  }

// Or store to a GBytes for use elsewhere.
gsize data_len;
g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
if (data == NULL)
  {
    g_warning ("Error saving key file: %s", error->message);
    return;
  }
g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);

GLib type: Shared boxed type with reference counted clone semantics.

Implementations§

Source§

impl KeyFile

Source

pub fn as_ptr(&self) -> *mut GKeyFile

Return the inner pointer to the underlying C value.

Source

pub unsafe fn from_glib_ptr_borrow(ptr: &*mut GKeyFile) -> &Self

Borrows the underlying C value.

Source§

impl KeyFile

Source

pub fn new() -> KeyFile

Creates a new empty KeyFile object.

Use load_from_file(), load_from_data(), load_from_dirs() or load_from_data_dirs() to read an existing key file.

§Returns

an empty KeyFile. Creates a new empty KeyFile object.

Use load_from_file(), load_from_data(), load_from_dirs() or load_from_data_dirs() to read an existing key file.

§Returns

an empty KeyFile.

Source

pub fn comment( &self, group_name: Option<&str>, key: Option<&str>, ) -> Result<GString, Error>

Retrieves a comment above @key from @group_name.

If @key is NULL then @comment will be read from above @group_name. If both @key and @group_name are NULL, then @comment will be read from above the first group in the file.

Note that the returned string does not include the # comment markers, but does include any whitespace after them (on each line). It includes the line breaks between lines, but does not include the final line break.

§group_name

a group name, or NULL to get a top-level comment

§key

a key, or NULL to get a group comment

§Returns

a comment that should be freed with free() Retrieves a comment above @key from @group_name.

If @key is NULL then @comment will be read from above @group_name. If both @key and @group_name are NULL, then @comment will be read from above the first group in the file.

Note that the returned string does not include the # comment markers, but does include any whitespace after them (on each line). It includes the line breaks between lines, but does not include the final line break.

§group_name

a group name, or NULL to get a top-level comment

§key

a key, or NULL to get a group comment

§Returns

a comment that should be freed with free()

Source

pub fn double(&self, group_name: &str, key: &str) -> Result<f64, Error>

Returns the value associated with @key under @group_name as a double.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as a double then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a double, or 0.0 if the key was not found or could not be parsed. Returns the value associated with @key under @group_name as a double.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as a double then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a double, or 0.0 if the key was not found or could not be parsed.

Source

pub fn double_list( &self, group_name: &str, key: &str, ) -> Result<Vec<f64>, Error>

Returns the values associated with @key under @group_name as doubles.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as doubles then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns
the values associated with the key as a list of doubles, or `NULL` if the
key was not found or could not be parsed. The returned list of doubles
should be freed with `free()` when no longer needed.

Returns the values associated with @key under @group_name as doubles.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as doubles then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns
the values associated with the key as a list of doubles, or `NULL` if the
key was not found or could not be parsed. The returned list of doubles
should be freed with `free()` when no longer needed.
Source

pub fn int64(&self, group_name: &str, key: &str) -> Result<i64, Error>

Returns the value associated with @key under @group_name as a signed 64-bit integer.

This is similar to integer() but can return 64-bit results without truncation.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a signed 64-bit integer, or 0 if the key was not found or could not be parsed. Returns the value associated with @key under @group_name as a signed 64-bit integer.

This is similar to integer() but can return 64-bit results without truncation.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a signed 64-bit integer, or 0 if the key was not found or could not be parsed.

Source

pub fn integer(&self, group_name: &str, key: &str) -> Result<i32, Error>

Returns the value associated with @key under @group_name as an integer.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as an integer, or is out of range for a gint, then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as an integer, or 0 if the key was not found or could not be parsed. Returns the value associated with @key under @group_name as an integer.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as an integer, or is out of range for a gint, then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as an integer, or 0 if the key was not found or could not be parsed.

Source

pub fn integer_list( &self, group_name: &str, key: &str, ) -> Result<Vec<i32>, Error>

Returns the values associated with @key under @group_name as integers.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as integers, or are out of range for gint, then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns
the values associated with the key as a list of integers, or `NULL` if
the key was not found or could not be parsed. The returned list of
integers should be freed with `free()` when no longer needed.

Returns the values associated with @key under @group_name as integers.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as integers, or are out of range for gint, then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns
the values associated with the key as a list of integers, or `NULL` if
the key was not found or could not be parsed. The returned list of
integers should be freed with `free()` when no longer needed.
Source

pub fn locale_for_key( &self, group_name: &str, key: &str, locale: Option<&str>, ) -> Option<GString>

Returns the actual locale which the result of locale_string() or locale_string_list() came from.

If calling locale_string() or locale_string_list() with exactly the same @self, @group_name, @key and @locale, the result of those functions will have originally been tagged with the locale that is the result of this function.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

the locale from the file, or NULL if the key was not found or the entry in the file was was untranslated Returns the actual locale which the result of locale_string() or locale_string_list() came from.

If calling locale_string() or locale_string_list() with exactly the same @self, @group_name, @key and @locale, the result of those functions will have originally been tagged with the locale that is the result of this function.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

the locale from the file, or NULL if the key was not found or the entry in the file was was untranslated

Source

pub fn start_group(&self) -> Option<GString>

Returns the name of the start group of the file.

§Returns

The start group of the key file. Returns the name of the start group of the file.

§Returns

The start group of the key file.

Source

pub fn uint64(&self, group_name: &str, key: &str) -> Result<u64, Error>

Returns the value associated with @key under @group_name as an unsigned 64-bit integer.

This is similar to integer() but can return large positive results without truncation.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as an unsigned 64-bit integer, or 0 if the key was not found or could not be parsed. Returns the value associated with @key under @group_name as an unsigned 64-bit integer.

This is similar to integer() but can return large positive results without truncation.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as an unsigned 64-bit integer, or 0 if the key was not found or could not be parsed.

Source

pub fn value(&self, group_name: &str, key: &str) -> Result<GString, Error>

Returns the raw value associated with @key under @group_name.

Use string() to retrieve an unescaped UTF-8 string.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a newly allocated string or NULL if the specified key cannot be found. Returns the raw value associated with @key under @group_name.

Use string() to retrieve an unescaped UTF-8 string.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a newly allocated string or NULL if the specified key cannot be found.

Source

pub fn has_group(&self, group_name: &str) -> bool

Looks whether the key file has the group @group_name.

§group_name

a group name

§Returns

true if @group_name is a part of @self, false otherwise. Looks whether the key file has the group @group_name.

§group_name

a group name

§Returns

true if @group_name is a part of @self, false otherwise.

Source

pub fn load_from_bytes( &self, bytes: &Bytes, flags: KeyFileFlags, ) -> Result<(), Error>

Loads a key file from the data in @bytes into an empty KeyFile structure.

If the object cannot be created then a KeyFileError is returned.

§bytes

a Bytes

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise Loads a key file from the data in @bytes into an empty KeyFile structure.

If the object cannot be created then a KeyFileError is returned.

§bytes

a Bytes

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

Source

pub fn load_from_data( &self, data: &str, flags: KeyFileFlags, ) -> Result<(), Error>

Loads a key file from memory into an empty KeyFile structure.

If the object cannot be created then a KeyFileError is returned.

§data

key file loaded in memory

§length

the length of @data in bytes (or (gsize)-1 if data is nul-terminated)

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise Loads a key file from memory into an empty KeyFile structure.

If the object cannot be created then a KeyFileError is returned.

§data

key file loaded in memory

§length

the length of @data in bytes (or (gsize)-1 if data is nul-terminated)

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

Source

pub fn load_from_file( &self, file: impl AsRef<Path>, flags: KeyFileFlags, ) -> Result<(), Error>

Loads a key file into an empty KeyFile structure.

If the OS returns an error when opening or reading the file, a FileError is returned. If there is a problem parsing the file, a KeyFileError is returned.

This function will never return a [error@GLib.KeyFileError.NOT_FOUND] error. If the @file is not found, [error@GLib.FileError.NOENT] is returned.

§file

the path of a filename to load, in the GLib filename encoding

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise Loads a key file into an empty KeyFile structure.

If the OS returns an error when opening or reading the file, a FileError is returned. If there is a problem parsing the file, a KeyFileError is returned.

This function will never return a [error@GLib.KeyFileError.NOT_FOUND] error. If the @file is not found, [error@GLib.FileError.NOENT] is returned.

§file

the path of a filename to load, in the GLib filename encoding

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

Source

pub fn remove_comment( &self, group_name: Option<&str>, key: Option<&str>, ) -> Result<(), Error>

Removes a comment above @key from @group_name.

If @key is NULL then @comment will be removed above @group_name. If both @key and @group_name are NULL, then @comment will be removed above the first group in the file.

§group_name

a group name, or NULL to get a top-level comment

§key

a key, or NULL to get a group comment

§Returns

true if the comment was removed, false otherwise Removes a comment above @key from @group_name.

If @key is NULL then @comment will be removed above @group_name. If both @key and @group_name are NULL, then @comment will be removed above the first group in the file.

§group_name

a group name, or NULL to get a top-level comment

§key

a key, or NULL to get a group comment

§Returns

true if the comment was removed, false otherwise

Source

pub fn remove_group(&self, group_name: &str) -> Result<(), Error>

Removes the specified group, @group_name, from the key file.

§group_name

a group name

§Returns

true if the group was removed, false otherwise Removes the specified group, @group_name, from the key file.

§group_name

a group name

§Returns

true if the group was removed, false otherwise

Source

pub fn remove_key(&self, group_name: &str, key: &str) -> Result<(), Error>

Removes @key in @group_name from the key file.

§group_name

a group name

§key

a key name to remove

§Returns

true if the key was removed, false otherwise Removes @key in @group_name from the key file.

§group_name

a group name

§key

a key name to remove

§Returns

true if the key was removed, false otherwise

Source

pub fn set_boolean(&self, group_name: &str, key: &str, value: bool)

Associates a new boolean value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

true or false Associates a new boolean value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

true or false

Source

pub fn set_comment( &self, group_name: Option<&str>, key: Option<&str>, comment: &str, ) -> Result<(), Error>

Places a comment above @key from @group_name.

If @key is NULL then @comment will be written above @group_name. If both @key and @group_name are NULL, then @comment will be written above the first group in the file.

Note that this function prepends a # comment marker to each line of @comment.

§group_name

a group name, or NULL to write a top-level comment

§key

a key, or NULL to write a group comment

§comment

a comment

§Returns

true if the comment was written, false otherwise Places a comment above @key from @group_name.

If @key is NULL then @comment will be written above @group_name. If both @key and @group_name are NULL, then @comment will be written above the first group in the file.

Note that this function prepends a # comment marker to each line of @comment.

§group_name

a group name, or NULL to write a top-level comment

§key

a key, or NULL to write a group comment

§comment

a comment

§Returns

true if the comment was written, false otherwise

Source

pub fn set_double(&self, group_name: &str, key: &str, value: f64)

Associates a new double value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

a double value Associates a new double value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

a double value

Source

pub fn set_int64(&self, group_name: &str, key: &str, value: i64)

Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value

Source

pub fn set_integer(&self, group_name: &str, key: &str, value: i32)

Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value

Source

pub fn set_list_separator(&self, separator: Char)

Sets the character which is used to separate values in lists.

Typically ; or , are used as separators. The default list separator is ;.

§separator

the separator Sets the character which is used to separate values in lists.

Typically ; or , are used as separators. The default list separator is ;.

§separator

the separator

Source

pub fn set_locale_string( &self, group_name: &str, key: &str, locale: &str, string: &str, )

Associates a string value for @key and @locale under @group_name.

If the translation for @key cannot be found then it is created.

If @locale is C then the untranslated value is set (since GLib 2.84).

§group_name

a group name

§key

a key

§locale

a locale identifier

§string

a string Associates a string value for @key and @locale under @group_name.

If the translation for @key cannot be found then it is created.

If @locale is C then the untranslated value is set (since GLib 2.84).

§group_name

a group name

§key

a key

§locale

a locale identifier

§string

a string

Source

pub fn set_string(&self, group_name: &str, key: &str, string: &str)

Associates a new string value with @key under @group_name.

If @key cannot be found then it is created. If @group_name cannot be found then it is created. Unlike set_value(), this function handles characters that need escaping, such as newlines.

§group_name

a group name

§key

a key

§string

a string Associates a new string value with @key under @group_name.

If @key cannot be found then it is created. If @group_name cannot be found then it is created. Unlike set_value(), this function handles characters that need escaping, such as newlines.

§group_name

a group name

§key

a key

§string

a string

Source

pub fn set_uint64(&self, group_name: &str, key: &str, value: u64)

Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value Associates a new integer value with @key under @group_name.

If @key cannot be found then it is created.

§group_name

a group name

§key

a key

§value

an integer value

Source

pub fn set_value(&self, group_name: &str, key: &str, value: &str)

Associates a new value with @key under @group_name.

If @key cannot be found then it is created. If @group_name cannot be found then it is created. To set an UTF-8 string which may contain characters that need escaping (such as newlines or spaces), use set_string().

§group_name

a group name

§key

a key

§value

a string Associates a new value with @key under @group_name.

If @key cannot be found then it is created. If @group_name cannot be found then it is created. To set an UTF-8 string which may contain characters that need escaping (such as newlines or spaces), use set_string().

§group_name

a group name

§key

a key

§value

a string

Source§

impl KeyFile

Source

pub fn save_to_file<T: AsRef<Path>>(&self, filename: T) -> Result<(), Error>

Writes the contents of @self to @filename using file_set_contents().

If you need stricter guarantees about durability of the written file than are provided by file_set_contents(), use file_set_contents_full() with the return value of to_data().

This function can fail for any of the reasons that file_set_contents() may fail.

§filename

the name of the file to write to

§Returns

true if successful, false otherwise Writes the contents of @self to @filename using file_set_contents().

If you need stricter guarantees about durability of the written file than are provided by file_set_contents(), use file_set_contents_full() with the return value of to_data().

This function can fail for any of the reasons that file_set_contents() may fail.

§filename

the name of the file to write to

§Returns

true if successful, false otherwise

Source

pub fn load_from_data_dirs<T: AsRef<Path>>( &self, file: T, flags: KeyFileFlags, ) -> Result<PathBuf, Error>

Looks for a key file named @file in the paths returned from user_data_dir() and system_data_dirs(), loads the file into @self and returns the file’s full path in @full_path.

If the file could not be loaded then either a FileError or KeyFileError is returned.

§file

a relative path to a filename to open and parse

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

§full_path

return location for a string containing the full path of the file, or NULL to ignore Looks for a key file named @file in the paths returned from user_data_dir() and system_data_dirs(), loads the file into @self and returns the file’s full path in @full_path.

If the file could not be loaded then either a FileError or KeyFileError is returned.

§file

a relative path to a filename to open and parse

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

§full_path

return location for a string containing the full path of the file, or NULL to ignore

Source

pub fn load_from_dirs<T: AsRef<Path>, U: AsRef<Path>>( &self, file: T, search_dirs: &[U], flags: KeyFileFlags, ) -> Result<PathBuf, Error>

Looks for a key file named @file in the paths specified in @search_dirs, loads the file into @self and returns the file’s full path in @full_path.

If the file could not be found in any of the @search_dirs, [error@GLib.KeyFileError.NOT_FOUND] is returned. If the file is found but the OS returns an error when opening or reading the file, a FileError is returned. If there is a problem parsing the file, a KeyFileError is returned.

§file

a relative path to a filename to open and parse

§search_dirs

NULL-terminated array of directories to search

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

§full_path

return location for a string containing the full path of the file, or NULL to ignore Looks for a key file named @file in the paths specified in @search_dirs, loads the file into @self and returns the file’s full path in @full_path.

If the file could not be found in any of the @search_dirs, [error@GLib.KeyFileError.NOT_FOUND] is returned. If the file is found but the OS returns an error when opening or reading the file, a FileError is returned. If there is a problem parsing the file, a KeyFileError is returned.

§file

a relative path to a filename to open and parse

§search_dirs

NULL-terminated array of directories to search

§flags

flags from KeyFileFlags

§Returns

true if a key file could be loaded, false otherwise

§full_path

return location for a string containing the full path of the file, or NULL to ignore

Source

pub fn to_data(&self) -> GString

Outputs @self as a string.

Note that this function never reports an error.

§Returns

a newly allocated string holding the contents of the key file

§length

return location for the length of the returned string, or NULL to ignore Outputs @self as a string.

Note that this function never reports an error.

§Returns

a newly allocated string holding the contents of the key file

§length

return location for the length of the returned string, or NULL to ignore

Source

pub fn groups(&self) -> PtrSlice<GStringPtr>

Returns all groups in the key file loaded with @self.

The array of returned groups will be NULL-terminated, so @length may optionally be NULL.

§Returns

a newly-allocated NULL-terminated array of strings. Use strfreev() to free it.

§length

return location for the number of returned groups, or NULL to ignore Returns all groups in the key file loaded with @self.

The array of returned groups will be NULL-terminated, so @length may optionally be NULL.

§Returns

a newly-allocated NULL-terminated array of strings. Use strfreev() to free it.

§length

return location for the number of returned groups, or NULL to ignore

Source

pub fn keys(&self, group_name: &str) -> Result<PtrSlice<GStringPtr>, Error>

Returns all keys for the group name @group_name.

The array of returned keys will be NULL-terminated, so @length may optionally be NULL. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§Returns

a newly-allocated NULL-terminated array of strings. Use strfreev() to free it.

§length

return location for the number of keys returned, or NULL to ignore Returns all keys for the group name @group_name.

The array of returned keys will be NULL-terminated, so @length may optionally be NULL. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§Returns

a newly-allocated NULL-terminated array of strings. Use strfreev() to free it.

§length

return location for the number of keys returned, or NULL to ignore

Source

pub fn boolean(&self, group_name: &str, key: &str) -> Result<bool, Error>

Returns the value associated with @key under @group_name as a boolean.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as a boolean then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a boolean, or false if the key was not found or could not be parsed. Returns the value associated with @key under @group_name as a boolean.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the value associated with @key cannot be interpreted as a boolean then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the value associated with the key as a boolean, or false if the key was not found or could not be parsed.

Source

pub fn has_key(&self, group_name: &str, key: &str) -> Result<bool, Error>

Looks whether the key file has the key @key in the group @group_name.

Note that this function does not follow the rules for Error strictly; the return value both carries meaning and signals an error. To use this function, you must pass a Error pointer in @error, and check whether it is not NULL to see if an error occurred.

Language bindings should use value() to test whether a key exists.

§group_name

a group name

§key

a key name

§Returns

true if @key is a part of @group_name, false otherwise Looks whether the key file has the key @key in the group @group_name.

Note that this function does not follow the rules for Error strictly; the return value both carries meaning and signals an error. To use this function, you must pass a Error pointer in @error, and check whether it is not NULL to see if an error occurred.

Language bindings should use value() to test whether a key exists.

§group_name

a group name

§key

a key name

§Returns

true if @key is a part of @group_name, false otherwise

Source

pub fn boolean_list( &self, group_name: &str, key: &str, ) -> Result<Vec<bool>, Error>

Returns the values associated with @key under @group_name as booleans.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as booleans then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the values associated with the key as a list of booleans, or NULL if the key was not found or could not be parsed. The returned list of booleans should be freed with free() when no longer needed. Returns the values associated with @key under @group_name as booleans.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. Likewise, if the values associated with @key cannot be interpreted as booleans then [error@GLib.KeyFileError.INVALID_VALUE] is returned.

§group_name

a group name

§key

a key

§Returns

the values associated with the key as a list of booleans, or NULL if the key was not found or could not be parsed. The returned list of booleans should be freed with free() when no longer needed.

Source

pub fn string(&self, group_name: &str, key: &str) -> Result<GString, Error>

Returns the string value associated with @key under @group_name.

Unlike value(), this function handles escape sequences like \s.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a newly allocated string or NULL if the specified key cannot be found. Returns the string value associated with @key under @group_name.

Unlike value(), this function handles escape sequences like \s.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a newly allocated string or NULL if the specified key cannot be found.

Source

pub fn string_list( &self, group_name: &str, key: &str, ) -> Result<PtrSlice<GStringPtr>, Error>

Returns the values associated with @key under @group_name.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a NULL-terminated string array or NULL if the specified key cannot be found. The array should be freed with strfreev(). Returns the values associated with @key under @group_name.

If the key cannot be found, [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the @group_name cannot be found, [error@GLib.KeyFileError.GROUP_NOT_FOUND] is returned.

§group_name

a group name

§key

a key

§Returns

a NULL-terminated string array or NULL if the specified key cannot be found. The array should be freed with strfreev().

Source

pub fn locale_string( &self, group_name: &str, key: &str, locale: Option<&str>, ) -> Result<GString, Error>

Returns the value associated with @key under @group_name translated in the given @locale if available.

If @locale is C then the untranslated value is returned (since GLib 2.84).

If @locale is NULL then the current locale is assumed.

If @locale is to be non-NULL, or if the current locale will change over the lifetime of the KeyFile, it must be loaded with [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the value associated with @key cannot be interpreted or no suitable translation can be found then the untranslated value is returned.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

a newly allocated string or NULL if the specified key cannot be found. Returns the value associated with @key under @group_name translated in the given @locale if available.

If @locale is C then the untranslated value is returned (since GLib 2.84).

If @locale is NULL then the current locale is assumed.

If @locale is to be non-NULL, or if the current locale will change over the lifetime of the KeyFile, it must be loaded with [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the value associated with @key cannot be interpreted or no suitable translation can be found then the untranslated value is returned.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

a newly allocated string or NULL if the specified key cannot be found.

Source

pub fn locale_string_list( &self, group_name: &str, key: &str, locale: Option<&str>, ) -> Result<PtrSlice<GStringPtr>, Error>

Returns the values associated with @key under @group_name translated in the given @locale if available.

If @locale is C then the untranslated value is returned (since GLib 2.84).

If @locale is NULL then the current locale is assumed.

If @locale is to be non-NULL, or if the current locale will change over the lifetime of the KeyFile, it must be loaded with [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the values associated with @key cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array is NULL-terminated, so @length may optionally be NULL.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

a newly allocated NULL-terminated string array or NULL if the key isn’t found. The string array should be freed with strfreev(). Returns the values associated with @key under @group_name translated in the given @locale if available.

If @locale is C then the untranslated value is returned (since GLib 2.84).

If @locale is NULL then the current locale is assumed.

If @locale is to be non-NULL, or if the current locale will change over the lifetime of the KeyFile, it must be loaded with [flags@GLib.KeyFileFlags.KEEP_TRANSLATIONS] in order to load strings for all locales.

If @key cannot be found then [error@GLib.KeyFileError.KEY_NOT_FOUND] is returned. If the values associated with @key cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array is NULL-terminated, so @length may optionally be NULL.

§group_name

a group name

§key

a key

§locale

a locale identifier or NULL to use the current locale

§Returns

a newly allocated NULL-terminated string array or NULL if the key isn’t found. The string array should be freed with strfreev().

Trait Implementations§

Source§

impl Clone for KeyFile

Source§

fn clone(&self) -> Self

Makes a clone of this shared reference.

This increments the strong reference count of the reference. Dropping the reference will decrement it again.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for KeyFile

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<KeyFile> for Value

Source§

fn from(s: KeyFile) -> Self

Converts to this type from the input type.
Source§

impl HasParamSpec for KeyFile

Source§

type ParamSpec = ParamSpecBoxed

Source§

type SetValue = KeyFile

Preferred value to be used as setter for the associated ParamSpec.
Source§

type BuilderFn = fn(_: &str) -> ParamSpecBoxedBuilder<'_, KeyFile>

Source§

fn param_spec_builder() -> Self::BuilderFn

Source§

impl Hash for KeyFile

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for KeyFile

Source§

fn cmp(&self, other: &KeyFile) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for KeyFile

Source§

fn eq(&self, other: &KeyFile) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for KeyFile

Source§

fn partial_cmp(&self, other: &KeyFile) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StaticType for KeyFile

Source§

fn static_type() -> Type

Returns the type identifier of Self.
Source§

impl Eq for KeyFile

Source§

impl StructuralPartialEq for KeyFile

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *const GSList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GList> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GPtrArray> for T

Source§

impl<T> FromGlibPtrArrayContainerAsVec<<T as GlibPtrDefault>::GlibType, *mut GSList> for T

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoClosureReturnValue for T
where T: Into<Value>,

Source§

impl<T> Property for T
where T: HasParamSpec,

Source§

type Value = T

Source§

impl<T> PropertyGet for T
where T: HasParamSpec,

Source§

type Value = T

Source§

fn get<R, F>(&self, f: F) -> R
where F: Fn(&<T as PropertyGet>::Value) -> R,

Source§

impl<T> StaticTypeExt for T
where T: StaticType,

Source§

fn ensure_type()

Ensures that the type has been registered with the type system.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TransparentType for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T> TryFromClosureReturnValue for T
where T: for<'a> FromValue<'a> + StaticType + 'static,

Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<'a, T, C, E> FromValueOptional<'a> for T
where T: FromValue<'a, Checker = C>, C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError<E>>, E: Error + Send + 'static,