The GKeyFile struct contains only private data and should not be accessed directly.
Example: KeyFile handling:
public static int main (string[] args) {
try {
//
// Read data:
//
KeyFile file = new KeyFile ();
// Use ',' as array-element-separator instead of ';'.
file.set_list_separator (',');
// file.load_from_file ("my-keyfile.conf", KeyFileFlags.NONE);
file.load_from_data ("""
# This is an example key-value file
[OptionalGroup]
required_key = myreq
optional_key = myopt
[BasicValues]
String=mystr
Bool=true
Int=300
Double=0.0
[Lists]
BoolArray=true,true,false
StringArray=Str1,Str2
[LocalizedString]
Hi=Hello
Hi[fr]=Bonjour
Hi[de]=Hallo
Hi[es]=Hola
""", -1, KeyFileFlags.NONE);
// ** BasicValues:
string @string = file.get_string ("BasicValues", "String");
bool @bool = file.get_boolean ("BasicValues", "Bool");
int @int = file.get_integer ("BasicValues", "Int");
double @double = file.get_double ("BasicValues", "Double");
// Output:
// ``mystr``
// ``true``
// ``300``
// ``0.000000``
print ("%s\n", @string);
print ("%s\n", @bool.to_string ());
print ("%d\n", @int);
print ("%f\n", @double);
// ** Lists:
bool[] bool_array = file.get_boolean_list ("Lists", "BoolArray");
string[] string_array = file.get_string_list ("Lists", "StringArray");
// Output: ``true true false``
foreach (bool b in bool_array) {
print ("%s ", b.to_string ());
}
print ("\n");
// Output: ``"Str1" "Str2"``
foreach (string str in string_array) {
print ("\"%s\" ", str);
}
print ("\n");
// ** LocalizedString:
string hi = file.get_locale_string ("LocalizedString", "Hi", null);
// Output: ``Hello``
print ("%s\n", hi);
// ** OptionalGroup
// Check availability before accessing to avoid exceptions:
if (file.has_group ("OptionalGroup")) {
string required_key = file.get_string ("OptionalGroup", "required_key");
string? optional_key = null;
if (file.has_key ("OptionalGroup", "optional_key")) {
optional_key = file.get_string ("OptionalGroup", "optional_key");
}
// Output:
// ``myreq``
// ``myopt``
print ("%s\n", required_key);
print ("%s\n", optional_key);
}
//
// Modify data:
//
file.set_string ("BasicValues", "String", "my-new-value");
file.remove_group ("LocalizedString");
file.remove_group ("OptionalGroup");
file.remove_key ("Lists", "StringArray");
//
// List all groups & keys:
//
// Output:
// ``Key: BasicValues.String = my-new-value``
// ``Key: BasicValues.Bool = true``
// ``Key: BasicValues.Int = 300``
// ``Key: BasicValues.Double = 0.0``
// ``Key: Lists.BoolArray = true,true,false``
foreach (unowned string group in file.get_groups ()) {
foreach (unowned string key in file.get_keys (group)) {
print ("Key: %s.%s = %s\n", group, key, file.get_value (group, key));
}
}
//
// Print the modified keyfile to stdout:
//
// Output:
// ``-----``
// ``[BasicValues]``
// ``String=my-new-value``
// ``Bool=true``
// ``Int=300``
// ``Double=0.0``
// ````
// ``[Lists]``
// ``BoolArray=true,true,false``
// ``----``
string keyfile_str = file.to_data ();
print ("-----\n%s----\n", keyfile_str);
} catch (KeyFileError e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg glib-2.0 GLib.KeyFile.vala
- public bool get_boolean (string group_name, string key) throws KeyFileError
Returns the value associated with key
under
group_name
as a boolean.
- public bool[] get_boolean_list (string group_name, string key) throws KeyFileError
Returns the values associated with key
under
group_name
as booleans.
- public string get_comment (string? group_name, string? key) throws KeyFileError
Retrieves a comment above key
from group_name
.
- public double get_double (string group_name, string key) throws KeyFileError
Returns the value associated with key
under
group_name
as a double.
- public double[] get_double_list (string group_name, string key) throws KeyFileError
Returns the values associated with key
under
group_name
as doubles.
- public string[] get_groups ()
Returns all groups in the key file loaded with
this.
- public int64 get_int64 (string group_name, string key) throws KeyFileError
Returns the value associated with key
under
group_name
as a signed 64-bit integer.
- public int get_integer (string group_name, string key) throws KeyFileError
Returns the value associated with key
under
group_name
as an integer.
- public int[] get_integer_list (string group_name, string key) throws KeyFileError
Returns the values associated with key
under
group_name
as integers.
- public string[] get_keys (string group_name) throws KeyFileError
Returns all keys for the group name group_name
.
- public string? get_locale_for_key (string group_name, string key, string? locale = null)
- public string get_locale_string (string group_name, string key, string? locale = null) throws KeyFileError
Returns the value associated with key
under
group_name
translated in the given locale
if available.
- public string[] get_locale_string_list (string group_name, string key, string? locale = null) throws KeyFileError
Returns the values associated with key
under
group_name
translated in the given locale
if available.
- public string get_start_group ()
Returns the name of the start group of the file.
- public string get_string (string group_name, string key) throws KeyFileError
Returns the string value associated with key
under
group_name
.
- public string[] get_string_list (string group_name, string key) throws KeyFileError
Returns the values associated with key
under
group_name
.
- public uint64 get_uint64 (string group_name, string key) throws KeyFileError
Returns the value associated with key
under
group_name
as an unsigned 64-bit integer.
- public string get_value (string group_name, string key) throws KeyFileError
Returns the raw value associated with key
under
group_name
.
- public bool has_group (string group_name)
Looks whether the key file has the group group_name
.
- public bool has_key (string group_name, string key) throws KeyFileError
Looks whether the key file has the key key
in the group
group_name
.
- public bool load_from_bytes (Bytes bytes, KeyFileFlags flags) throws KeyFileError
Loads a key file from the data in bytes
into an empty
KeyFile structure.
- public bool load_from_data (string data, size_t length, KeyFileFlags flags) throws KeyFileError
Loads a key file from memory into an empty KeyFile
structure.
- public bool load_from_data_dirs (string file, out string full_path, KeyFileFlags flags) throws KeyFileError, FileError
This function looks for a key file named file
in the
paths returned from get_user_data_dir and
get_system_data_dirs, loads the file into
this and returns the file's full path in full_path
.
- public bool load_from_dirs (string file, string[] search_dirs, out string full_path, KeyFileFlags flags) throws KeyFileError, FileError
This function looks for a key file named file
in the
paths specified in search_dirs
, loads the file into this and returns the file's full
path in full_path
.
- public bool load_from_file (string file, KeyFileFlags flags) throws KeyFileError, FileError
Loads a key file into an empty KeyFile
structure.
- public void remove_comment (string group_name, string key) throws KeyFileError
Removes a comment above key
from group_name
.
- public void remove_group (string group_name) throws KeyFileError
Removes the specified group, group_name
, from the key
file.
- public void remove_key (string group_name, string key) throws KeyFileError
Removes key
in group_name
from the key file.
- public bool save_to_file (string filename) throws FileError
Writes the contents of this to
filename
using set_data.
- public void set_boolean (string group_name, string key, bool value)
Associates a new boolean value with key
under
group_name
.
- public void set_boolean_list (string group_name, string key, bool[] list)
Associates a list of boolean values with key
under
group_name
.
- public void set_comment (string? group_name, string? key, string comment) throws KeyFileError
Places a comment above key
from group_name
.
- public void set_double (string group_name, string key, double value)
Associates a new double value with key
under
group_name
.
- public void set_double_list (string group_name, string key, double[] list)
Associates a list of double values with key
under
group_name
.
- public void set_int64 (string group_name, string key, int64 value)
Associates a new integer value with key
under
group_name
.
- public void set_integer (string group_name, string key, int value)
Associates a new integer value with key
under
group_name
.
- public void set_integer_list (string group_name, string key, int[] list)
Associates a list of integer values with key
under
group_name
.
- public void set_list_separator (char separator)
Sets the character which is used to separate values in lists.
- public void set_locale_string (string group_name, string key, string locale, string str)
Associates a string value for key
and locale
under group_name
.
- public void set_locale_string_list (string group_name, string key, string locale, string[] list)
Associates a list of string values for key
and
locale
under group_name
.
- public void set_string (string group_name, string key, string str)
Associates a new string value with key
under
group_name
.
- public void set_string_list (string group_name, string key, string[] list)
Associates a list of string values for key
under
group_name
.
- public void set_uint64 (string group_name, string key, uint64 value)
Associates a new integer value with key
under
group_name
.
- public void set_value (string group_name, string key, string value)
Associates a new value with key
under group_name
.
- public string to_data (out size_t length = null, out Error error = null)
This function outputs this as a
string.