Class KeyFile
- All Implemented Interfaces:
Proxy
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.
# 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, onlytrueandfalse(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:
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:
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);
-
Constructor Summary
ConstructorsConstructorDescriptionKeyFile()Creates a new emptyGLib.KeyFileobject.KeyFile(MemorySegment address) Create a KeyFile proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionstatic Quarkvoidfree()Clears all keys and groups fromkeyFile,and decreases the reference count by 1.booleangetBoolean(String groupName, String key) Returns the value associated withkeyundergroupNameas a boolean.boolean[]getBooleanList(String groupName, String key) Returns the values associated withkeyundergroupNameas booleans.getComment(@Nullable String groupName, @Nullable String key) Retrieves a comment abovekeyfromgroupName.doubleReturns the value associated withkeyundergroupNameas a double.double[]getDoubleList(String groupName, String key) Returns the values associated withkeyundergroupNameas doubles.String[]Returns all groups in the key file loaded withkeyFile.longReturns the value associated withkeyundergroupNameas a signed 64-bit integer.intgetInteger(String groupName, String key) Returns the value associated withkeyundergroupNameas an integer.int[]getIntegerList(String groupName, String key) Returns the values associated withkeyundergroupNameas integers.String[]Returns all keys for the group namegroupName.@Nullable StringgetLocaleForKey(String groupName, String key, @Nullable String locale) Returns the actual locale which the result ofgetLocaleString(java.lang.String, java.lang.String, java.lang.String)orgetLocaleStringList(java.lang.String, java.lang.String, java.lang.String)came from.getLocaleString(String groupName, String key, @Nullable String locale) Returns the value associated withkeyundergroupNametranslated in the givenlocaleif available.String[]getLocaleStringList(String groupName, String key, @Nullable String locale) Returns the values associated withkeyundergroupNametranslated in the givenlocaleif available.@Nullable StringReturns the name of the start group of the file.Returns the string value associated withkeyundergroupName.String[]getStringList(String groupName, String key) Returns the values associated withkeyundergroupName.static @Nullable TypegetType()Get the GType of the KeyFile classlongReturns the value associated withkeyundergroupNameas an unsigned 64-bit integer.Returns the raw value associated withkeyundergroupName.booleanLooks whether the key file has the groupgroupName.booleanLooks whether the key file has the keykeyin the groupgroupName.booleanloadFromBytes(byte[] bytes, Set<KeyFileFlags> flags) Loads a key file from the data inbytesinto an emptyGLib.KeyFilestructure.booleanloadFromBytes(byte[] bytes, KeyFileFlags... flags) Loads a key file from the data inbytesinto an emptyGLib.KeyFilestructure.booleanloadFromData(String data, long length, Set<KeyFileFlags> flags) Loads a key file from memory into an emptyGLib.KeyFilestructure.booleanloadFromData(String data, long length, KeyFileFlags... flags) Loads a key file from memory into an emptyGLib.KeyFilestructure.booleanloadFromDataDirs(String file, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) Looks for a key file namedfilein the paths returned fromGLib.getUserDataDir()andGLib.getSystemDataDirs().booleanloadFromDataDirs(String file, @Nullable Out<String> fullPath, KeyFileFlags... flags) Looks for a key file namedfilein the paths returned fromGLib.getUserDataDir()andGLib.getSystemDataDirs().booleanloadFromDirs(String file, @Nullable String @Nullable [] searchDirs, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) Looks for a key file namedfilein the paths specified insearchDirs,loads the file into this KeyFile and returns the file’s full path infullPath.booleanloadFromDirs(String file, @Nullable String @Nullable [] searchDirs, @Nullable Out<String> fullPath, KeyFileFlags... flags) Looks for a key file namedfilein the paths specified insearchDirs,loads the file into this KeyFile and returns the file’s full path infullPath.booleanloadFromFile(String file, Set<KeyFileFlags> flags) Loads a key file into an emptyGLib.KeyFilestructure.booleanloadFromFile(String file, KeyFileFlags... flags) Loads a key file into an emptyGLib.KeyFilestructure.ref()Increases the reference count ofkeyFile.booleanremoveComment(@Nullable String groupName, @Nullable String key) Removes a comment abovekeyfromgroupName.booleanremoveGroup(String groupName) Removes the specified group,groupName,from the key file.booleanRemoveskeyingroupNamefrom the key file.booleansaveToFile(String filename) Writes the contents of this KeyFile tofilenameusingGLib.fileSetContents(java.lang.String, byte[]).voidsetBoolean(String groupName, String key, boolean value) Associates a new boolean value withkeyundergroupName.voidsetBooleanList(String groupName, String key, @org.jspecify.annotations.Nullable boolean @Nullable [] list) Associates a list of boolean values withkeyundergroupName.booleansetComment(@Nullable String groupName, @Nullable String key, String comment) Places a comment abovekeyfromgroupName.voidAssociates a new double value withkeyundergroupName.voidsetDoubleList(String groupName, String key, @org.jspecify.annotations.Nullable double @Nullable [] list) Associates a list of double values withkeyundergroupName.voidAssociates a new integer value withkeyundergroupName.voidsetInteger(String groupName, String key, int value) Associates a new integer value withkeyundergroupName.voidsetIntegerList(String groupName, String key, @org.jspecify.annotations.Nullable int @Nullable [] list) Associates a list of integer values withkeyundergroupName.voidsetListSeparator(byte separator) Sets the character which is used to separate values in lists.voidsetLocaleString(String groupName, String key, String locale, String string) Associates a string value forkeyandlocaleundergroupName.voidsetLocaleStringList(String groupName, String key, String locale, @Nullable String @Nullable [] list) Associates a list of string values forkeyandlocaleundergroupName.voidAssociates a new string value withkeyundergroupName.voidsetStringList(String groupName, String key, @Nullable String @Nullable [] list) Associates a list of string values forkeyundergroupName.voidAssociates a new integer value withkeyundergroupName.voidAssociates a new value withkeyundergroupName.Outputs this KeyFile as a string.voidunref()Decreases the reference count of this KeyFile by 1.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
KeyFile
Create a KeyFile proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
KeyFile
public KeyFile()Creates a new emptyGLib.KeyFileobject.Use
loadFromFile(java.lang.String, java.util.Set<org.gnome.glib.KeyFileFlags>),loadFromData(java.lang.String, long, java.util.Set<org.gnome.glib.KeyFileFlags>),loadFromDirs(java.lang.String, java.lang.String[], org.javagi.base.Out<java.lang.String>, java.util.Set<org.gnome.glib.KeyFileFlags>)orloadFromDataDirs(java.lang.String, org.javagi.base.Out<java.lang.String>, java.util.Set<org.gnome.glib.KeyFileFlags>)to read an existing key file.- Since:
- 2.6
-
-
Method Details
-
getType
-
errorQuark
-
free
public void free()Clears all keys and groups fromkeyFile,and decreases the reference count by 1.If the reference count reaches zero, frees the key file and all its allocated memory.
- Since:
- 2.6
-
getBoolean
Returns the value associated withkeyundergroupNameas a boolean.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the value associated withkeycannot be interpreted as a boolean thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- 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.
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
getBooleanList
Returns the values associated withkeyundergroupNameas booleans.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the values associated withkeycannot be interpreted as booleans thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- the values associated with the key as a list of booleans, or
NULLif the key was not found or could not be parsed. The returned list of booleans should be freed withGLib.free(java.lang.foreign.MemorySegment)when no longer needed. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getComment
Retrieves a comment abovekeyfromgroupName.If
keyisNULLthencommentwill be read from abovegroupName.If bothkeyandgroupNameareNULL, thencommentwill 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.- Parameters:
groupName- a group name, orNULLto get a top-level commentkey- a key, orNULLto get a group comment- Returns:
- a comment that should be freed with
GLib.free(java.lang.foreign.MemorySegment) - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getDouble
Returns the value associated withkeyundergroupNameas a double.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the value associated withkeycannot be interpreted as a double thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- the value associated with the key as a double, or
0.0if the key was not found or could not be parsed. - Throws:
GErrorException- seeGError- Since:
- 2.12
-
getDoubleList
Returns the values associated withkeyundergroupNameas doubles.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the values associated withkeycannot be interpreted as doubles thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- the values associated with the key as a list of doubles, or
NULLif the key was not found or could not be parsed. The returned list of doubles should be freed withGLib.free(java.lang.foreign.MemorySegment)when no longer needed. - Throws:
GErrorException- seeGError- Since:
- 2.12
-
getGroups
Returns all groups in the key file loaded withkeyFile.The array of returned groups will be
NULL-terminated, solengthmay optionally beNULL.- Parameters:
length- return location for the number of returned groups, orNULLto ignore- Returns:
- a newly-allocated
NULL-terminated array of strings. UseGLib.strfreev(java.lang.foreign.MemorySegment)to free it. - Since:
- 2.6
-
getInt64
Returns the value associated withkeyundergroupNameas a signed 64-bit integer.This is similar to
getInteger(java.lang.String, java.lang.String)but can return 64-bit results without truncation.- Parameters:
groupName- a group namekey- a key- Returns:
- the value associated with the key as a signed 64-bit integer, or
0if the key was not found or could not be parsed. - Throws:
GErrorException- seeGError- Since:
- 2.26
-
getInteger
Returns the value associated withkeyundergroupNameas an integer.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the value associated withkeycannot be interpreted as an integer, or is out of range for agint, thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- the value associated with the key as an integer, or
0if the key was not found or could not be parsed. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getIntegerList
Returns the values associated withkeyundergroupNameas integers.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. Likewise, if the values associated withkeycannot be interpreted as integers, or are out of range forgint, thenGLib.KeyFileError.INVALID_VALUEis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- the values associated with the key as a list of integers, or
NULLif the key was not found or could not be parsed. The returned list of integers should be freed withGLib.free(java.lang.foreign.MemorySegment)when no longer needed. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getKeys
Returns all keys for the group namegroupName.The array of returned keys will be
NULL-terminated, solengthmay optionally beNULL. If thegroupNamecannot be found,GLib.KeyFileError.GROUP_NOT_FOUNDis returned.- Parameters:
groupName- a group namelength- return location for the number of keys returned, orNULLto ignore- Returns:
- a newly-allocated
NULL-terminated array of strings. UseGLib.strfreev(java.lang.foreign.MemorySegment)to free it. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getLocaleForKey
Returns the actual locale which the result ofgetLocaleString(java.lang.String, java.lang.String, java.lang.String)orgetLocaleStringList(java.lang.String, java.lang.String, java.lang.String)came from.If calling
getLocaleString(java.lang.String, java.lang.String, java.lang.String)orgetLocaleStringList(java.lang.String, java.lang.String, java.lang.String)with exactly the samekeyFile,groupName,keyandlocale,the result of those functions will have originally been tagged with the locale that is the result of this function.- Parameters:
groupName- a group namekey- a keylocale- a locale identifier orNULLto use the current locale- Returns:
- the locale from the file, or
NULLif the key was not found or the entry in the file was was untranslated - Since:
- 2.56
-
getLocaleString
public String getLocaleString(String groupName, String key, @Nullable String locale) throws GErrorException Returns the value associated withkeyundergroupNametranslated in the givenlocaleif available.If
localeisCthen the untranslated value is returned (since GLib 2.84).If
localeisNULLthen the current locale is assumed.If
localeis to be non-NULL, or if the current locale will change over the lifetime of theGLib.KeyFile, it must be loaded withGLib.KeyFileFlags.KEEP_TRANSLATIONSin order to load strings for all locales.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. If the value associated withkeycannot be interpreted or no suitable translation can be found then the untranslated value is returned.- Parameters:
groupName- a group namekey- a keylocale- a locale identifier orNULLto use the current locale- Returns:
- a newly allocated string or
NULLif the specified key cannot be found. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getLocaleStringList
public String[] getLocaleStringList(String groupName, String key, @Nullable String locale) throws GErrorException Returns the values associated withkeyundergroupNametranslated in the givenlocaleif available.If
localeisCthen the untranslated value is returned (since GLib 2.84).If
localeisNULLthen the current locale is assumed.If
localeis to be non-NULL, or if the current locale will change over the lifetime of theGLib.KeyFile, it must be loaded withGLib.KeyFileFlags.KEEP_TRANSLATIONSin order to load strings for all locales.If
keycannot be found thenGLib.KeyFileError.KEY_NOT_FOUNDis returned. If the values associated withkeycannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array isNULL-terminated, solengthmay optionally beNULL.- Parameters:
groupName- a group namekey- a keylocale- a locale identifier orNULLto use the current locale- Returns:
- a newly allocated
NULL-terminated string array orNULLif the key isn’t found. The string array should be freed withGLib.strfreev(java.lang.foreign.MemorySegment). - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getStartGroup
Returns the name of the start group of the file.- Returns:
- The start group of the key file.
- Since:
- 2.6
-
getString
Returns the string value associated withkeyundergroupName.Unlike
getValue(java.lang.String, java.lang.String), this function handles escape sequences like\\s.If the key cannot be found,
GLib.KeyFileError.KEY_NOT_FOUNDis returned. If thegroupNamecannot be found,GLib.KeyFileError.GROUP_NOT_FOUNDis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- a newly allocated string or
NULLif the specified key cannot be found. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getStringList
Returns the values associated withkeyundergroupName.If the key cannot be found,
GLib.KeyFileError.KEY_NOT_FOUNDis returned. If thegroupNamecannot be found,GLib.KeyFileError.GROUP_NOT_FOUNDis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- a
NULL-terminated string array orNULLif the specified key cannot be found. The array should be freed withGLib.strfreev(java.lang.foreign.MemorySegment). - Throws:
GErrorException- seeGError- Since:
- 2.6
-
getUint64
Returns the value associated withkeyundergroupNameas an unsigned 64-bit integer.This is similar to
getInteger(java.lang.String, java.lang.String)but can return large positive results without truncation.- Parameters:
groupName- a group namekey- a key- Returns:
- the value associated with the key as an unsigned 64-bit integer,
or
0if the key was not found or could not be parsed. - Throws:
GErrorException- seeGError- Since:
- 2.26
-
getValue
Returns the raw value associated withkeyundergroupName.Use
getString(java.lang.String, java.lang.String)to retrieve an unescaped UTF-8 string.If the key cannot be found,
GLib.KeyFileError.KEY_NOT_FOUNDis returned. If thegroupNamecannot be found,GLib.KeyFileError.GROUP_NOT_FOUNDis returned.- Parameters:
groupName- a group namekey- a key- Returns:
- a newly allocated string or
NULLif the specified key cannot be found. - Throws:
GErrorException- seeGError- Since:
- 2.6
-
hasGroup
Looks whether the key file has the groupgroupName.- Parameters:
groupName- a group name- Returns:
- true if
groupNameis a part ofkeyFile,false otherwise. - Since:
- 2.6
-
hasKey
Looks whether the key file has the keykeyin the groupgroupName.Note that this function does not follow the rules for
GLib.Errorstrictly; the return value both carries meaning and signals an error. To use this function, you must pass aGLib.Errorpointer inerror,and check whether it is notNULLto see if an error occurred.Language bindings should use
getValue(java.lang.String, java.lang.String)to test whether a key exists.- Parameters:
groupName- a group namekey- a key name- Returns:
- true if
keyis a part ofgroupName,false otherwise - Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromBytes
Loads a key file from the data inbytesinto an emptyGLib.KeyFilestructure.If the object cannot be created then a
GLib.KeyFileErroris returned.- Parameters:
bytes- aGLib.Bytesflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.50
-
loadFromBytes
Loads a key file from the data inbytesinto an emptyGLib.KeyFilestructure.If the object cannot be created then a
GLib.KeyFileErroris returned.- Parameters:
bytes- aGLib.Bytesflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.50
-
loadFromData
public boolean loadFromData(String data, long length, Set<KeyFileFlags> flags) throws GErrorException Loads a key file from memory into an emptyGLib.KeyFilestructure.If the object cannot be created then a [error
GLib.KeyFileErroris returned.- Parameters:
data- key file loaded in memorylength- the length ofdatain bytes (or(gsize)-1if data is nul-terminated)flags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromData
Loads a key file from memory into an emptyGLib.KeyFilestructure.If the object cannot be created then a [error
GLib.KeyFileErroris returned.- Parameters:
data- key file loaded in memorylength- the length ofdatain bytes (or(gsize)-1if data is nul-terminated)flags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromDataDirs
public boolean loadFromDataDirs(String file, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) throws GErrorException Looks for a key file namedfilein the paths returned fromGLib.getUserDataDir()andGLib.getSystemDataDirs().The search algorithm from
loadFromDirs(java.lang.String, java.lang.String[], org.javagi.base.Out<java.lang.String>, java.util.Set<org.gnome.glib.KeyFileFlags>)is used. Iffileis found, it’s loaded into this KeyFile and its full path is returned infullPath.If the file could not be loaded then either a
GLib.FileErrororGLib.KeyFileErroris returned.- Parameters:
file- a relative path to a filename to open and parsefullPath- return location for a string containing the full path of the file, orNULLto ignoreflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromDataDirs
public boolean loadFromDataDirs(String file, @Nullable Out<String> fullPath, KeyFileFlags... flags) throws GErrorException Looks for a key file namedfilein the paths returned fromGLib.getUserDataDir()andGLib.getSystemDataDirs().The search algorithm from
loadFromDirs(java.lang.String, java.lang.String[], org.javagi.base.Out<java.lang.String>, java.util.Set<org.gnome.glib.KeyFileFlags>)is used. Iffileis found, it’s loaded into this KeyFile and its full path is returned infullPath.If the file could not be loaded then either a
GLib.FileErrororGLib.KeyFileErroris returned.- Parameters:
file- a relative path to a filename to open and parsefullPath- return location for a string containing the full path of the file, orNULLto ignoreflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromDirs
public boolean loadFromDirs(String file, @Nullable String @Nullable [] searchDirs, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) throws GErrorException Looks for a key file namedfilein the paths specified insearchDirs,loads the file into this KeyFile and returns the file’s full path infullPath.searchDirsare checked in the order listed in the array, with the highest priority directory listed first. Within each directory,fileis looked for. If it’s not found,-characters infileare progressively replaced with directory separators to search subdirectories of the search directory. If the file has not been found after all-characters have been replaced, the next search directory insearchDirsis checked.If the file could not be found in any of the
searchDirs,GLib.KeyFileError.NOT_FOUNDis returned. If the file is found but the OS returns an error when opening or reading the file, aGLib.FileErroris returned. If there is a problem parsing the file, aGLib.KeyFileErroris returned.- Parameters:
file- a relative path to a filename to open and parsesearchDirs-NULL-terminated array of directories to searchfullPath- return location for a string containing the full path of the file, orNULLto ignoreflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.14
-
loadFromDirs
public boolean loadFromDirs(String file, @Nullable String @Nullable [] searchDirs, @Nullable Out<String> fullPath, KeyFileFlags... flags) throws GErrorException Looks for a key file namedfilein the paths specified insearchDirs,loads the file into this KeyFile and returns the file’s full path infullPath.searchDirsare checked in the order listed in the array, with the highest priority directory listed first. Within each directory,fileis looked for. If it’s not found,-characters infileare progressively replaced with directory separators to search subdirectories of the search directory. If the file has not been found after all-characters have been replaced, the next search directory insearchDirsis checked.If the file could not be found in any of the
searchDirs,GLib.KeyFileError.NOT_FOUNDis returned. If the file is found but the OS returns an error when opening or reading the file, aGLib.FileErroris returned. If there is a problem parsing the file, aGLib.KeyFileErroris returned.- Parameters:
file- a relative path to a filename to open and parsesearchDirs-NULL-terminated array of directories to searchfullPath- return location for a string containing the full path of the file, orNULLto ignoreflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.14
-
loadFromFile
Loads a key file into an emptyGLib.KeyFilestructure.If the OS returns an error when opening or reading the file, a
GLib.FileErroris returned. If there is a problem parsing the file, aGLib.KeyFileErroris returned.This function will never return a
GLib.KeyFileError.NOT_FOUNDerror. If thefileis not found,GLib.FileError.NOENTis returned.- Parameters:
file- the path of a filename to load, in the GLib filename encodingflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
loadFromFile
Loads a key file into an emptyGLib.KeyFilestructure.If the OS returns an error when opening or reading the file, a
GLib.FileErroris returned. If there is a problem parsing the file, aGLib.KeyFileErroris returned.This function will never return a
GLib.KeyFileError.NOT_FOUNDerror. If thefileis not found,GLib.FileError.NOENTis returned.- Parameters:
file- the path of a filename to load, in the GLib filename encodingflags- flags fromGLib.KeyFileFlags- Returns:
- true if a key file could be loaded, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
ref
Increases the reference count ofkeyFile.- Returns:
- the same
keyFile. - Since:
- 2.32
-
removeComment
public boolean removeComment(@Nullable String groupName, @Nullable String key) throws GErrorException Removes a comment abovekeyfromgroupName.If
keyisNULLthencommentwill be removed abovegroupName.If bothkeyandgroupNameareNULL, thencommentwill be removed above the first group in the file.- Parameters:
groupName- a group name, orNULLto get a top-level commentkey- a key, orNULLto get a group comment- Returns:
- true if the comment was removed, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
removeGroup
Removes the specified group,groupName,from the key file.- Parameters:
groupName- a group name- Returns:
- true if the group was removed, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
removeKey
RemoveskeyingroupNamefrom the key file.- Parameters:
groupName- a group namekey- a key name to remove- Returns:
- true if the key was removed, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
saveToFile
Writes the contents of this KeyFile tofilenameusingGLib.fileSetContents(java.lang.String, byte[]).If you need stricter guarantees about durability of the written file than are provided by
GLib.fileSetContents(java.lang.String, byte[]), useGLib.fileSetContentsFull(java.lang.String, byte[], java.util.Set<org.gnome.glib.FileSetContentsFlags>, int)with the return value oftoData(org.javagi.base.Out<java.lang.Long>).This function can fail for any of the reasons that
GLib.fileSetContents(java.lang.String, byte[])may fail.- Parameters:
filename- the name of the file to write to- Returns:
- true if successful, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.40
-
setBoolean
-
setBooleanList
public void setBooleanList(String groupName, String key, @org.jspecify.annotations.Nullable boolean @Nullable [] list) Associates a list of boolean values withkeyundergroupName.If
keycannot be found then it is created.- Parameters:
groupName- a group namekey- a keylist- an array of boolean values- Since:
- 2.6
-
setComment
public boolean setComment(@Nullable String groupName, @Nullable String key, String comment) throws GErrorException Places a comment abovekeyfromgroupName.If
keyisNULLthencommentwill be written abovegroupName.If bothkeyandgroupNameareNULL, thencommentwill be written above the first group in the file.Passing a non-existent
groupNameorkeyto this function returns false and populateserror.(In contrast, passing a non-existentgroup_nameorkeytosetString(java.lang.String, java.lang.String, java.lang.String)creates the associated group name and key.)Note that this function prepends a
#comment marker to each line ofcomment.- Parameters:
groupName- a group name, orNULLto write a top-level commentkey- a key, orNULLto write a group commentcomment- a comment- Returns:
- true if the comment was written, false otherwise
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
setDouble
-
setDoubleList
public void setDoubleList(String groupName, String key, @org.jspecify.annotations.Nullable double @Nullable [] list) Associates a list of double values withkeyundergroupName.If
keycannot be found then it is created.- Parameters:
groupName- a group namekey- a keylist- an array of double values- Since:
- 2.12
-
setInt64
-
setInteger
-
setIntegerList
public void setIntegerList(String groupName, String key, @org.jspecify.annotations.Nullable int @Nullable [] list) Associates a list of integer values withkeyundergroupName.If
keycannot be found then it is created.- Parameters:
groupName- a group namekey- a keylist- an array of integer values- Since:
- 2.6
-
setListSeparator
public void setListSeparator(byte separator) Sets the character which is used to separate values in lists.Typically
;or,are used as separators. The default list separator is;.- Parameters:
separator- the separator- Since:
- 2.6
-
setLocaleString
Associates a string value forkeyandlocaleundergroupName.If the translation for
keycannot be found then it is created.If
localeisCthen the untranslated value is set (since GLib 2.84).- Parameters:
groupName- a group namekey- a keylocale- a locale identifierstring- a string- Since:
- 2.6
-
setLocaleStringList
public void setLocaleStringList(String groupName, String key, String locale, @Nullable String @Nullable [] list) Associates a list of string values forkeyandlocaleundergroupName.If
localeisCthen the untranslated value is set (since GLib 2.84).If the translation for
keycannot be found then it is created.- Parameters:
groupName- a group namekey- a keylocale- a locale identifierlist- aNULL-terminated array of locale string values- Since:
- 2.6
-
setString
Associates a new string value withkeyundergroupName.If
keycannot be found then it is created. IfgroupNamecannot be found then it is created. UnlikesetValue(java.lang.String, java.lang.String, java.lang.String), this function handles characters that need escaping, such as newlines.- Parameters:
groupName- a group namekey- a keystring- a string- Since:
- 2.6
-
setStringList
Associates a list of string values forkeyundergroupName.If
keycannot be found then it is created. IfgroupNamecannot be found then it is created.- Parameters:
groupName- a group namekey- a keylist- an array of string values- Since:
- 2.6
-
setUint64
-
setValue
Associates a new value withkeyundergroupName.If
keycannot be found then it is created. IfgroupNamecannot be found then it is created. To set an UTF-8 string which may contain characters that need escaping (such as newlines or spaces), usesetString(java.lang.String, java.lang.String, java.lang.String).- Parameters:
groupName- a group namekey- a keyvalue- a string- Since:
- 2.6
-
toData
Outputs this KeyFile as a string.Note that this function never reports an error.
- Parameters:
length- return location for the length of the returned string, orNULLto ignore- Returns:
- a newly allocated string holding the contents of the key file
- Throws:
GErrorException- seeGError- Since:
- 2.6
-
unref
public void unref()Decreases the reference count of this KeyFile by 1.If the reference count reaches zero, frees the key file and all its allocated memory.
- Since:
- 2.32
-