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 now, e.g 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. Space before and after the '=' character
are ignored. Newline, tab, carriage return and backslash characters in
value are escaped as \\n
, \\t
, \\r
, and \\\\\\\\
, respectively. To preserve
leading spaces in values, these can also be escaped as \\s
.
Key files can store strings (possibly with localized variants), integers, booleans and lists of these. Lists are separated by a separator character, typically ';' or ','. To use the list separator character in a value in a list, it has to be escaped by prefixing it with a backslash.
This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some important differences:
- .ini files use the ';' character to begin comments,
key files use the '
'
character.
- Key files do not allow for ungrouped keys meaning only comments can precede the first group.
- Key files are always encoded in UTF-8.
- Key and Group names are case-sensitive. For example, a group called [GROUP] is a different from [group].
- .ini files 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:
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
ConstructorDescriptionKeyFile()
Creates a new emptyGKeyFile
object.KeyFile
(MemorySegment address) Create a KeyFile proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionstatic Quark
void
free()
Clears all keys and groups from this KeyFile, and decreases the reference count by 1.boolean
getBoolean
(String groupName, String key) Returns the value associated withkey
undergroupName
as a boolean.boolean[]
getBooleanList
(String groupName, String key) Returns the values associated withkey
undergroupName
as booleans.getComment
(@Nullable String groupName, @Nullable String key) Retrieves a comment abovekey
fromgroupName
.double
Returns the value associated withkey
undergroupName
as a double.double[]
getDoubleList
(String groupName, String key) Returns the values associated withkey
undergroupName
as doubles.String[]
Returns all groups in the key file loaded with this KeyFile.long
Returns the value associated withkey
undergroupName
as a signed 64-bit integer.int
getInteger
(String groupName, String key) Returns the value associated withkey
undergroupName
as an integer.int[]
getIntegerList
(String groupName, String key) Returns the values associated withkey
undergroupName
as integers.String[]
Returns all keys for the group namegroupName
.getLocaleForKey
(String groupName, String key, @Nullable String locale) Returns the actual locale which the result of g_key_file_get_locale_string() or g_key_file_get_locale_string_list() came from.getLocaleString
(String groupName, String key, @Nullable String locale) Returns the value associated withkey
undergroupName
translated in the givenlocale
if available.String[]
getLocaleStringList
(String groupName, String key, @Nullable String locale) Returns the values associated withkey
undergroupName
translated in the givenlocale
if available.Returns the name of the start group of the file.Returns the string value associated withkey
undergroupName
.String[]
getStringList
(String groupName, String key) Returns the values associated withkey
undergroupName
.static Type
getType()
Get the GType of the KeyFile classlong
Returns the value associated withkey
undergroupName
as an unsigned 64-bit integer.Returns the raw value associated withkey
undergroupName
.boolean
Looks whether the key file has the groupgroupName
.boolean
Looks whether the key file has the keykey
in the groupgroupName
.boolean
loadFromBytes
(Bytes bytes, Set<KeyFileFlags> flags) Loads a key file from the data inbytes
into an emptyGKeyFile
structure.boolean
loadFromBytes
(Bytes bytes, KeyFileFlags... flags) Loads a key file from the data inbytes
into an emptyGKeyFile
structure.boolean
loadFromData
(String data, long length, Set<KeyFileFlags> flags) Loads a key file from memory into an emptyGKeyFile
structure.boolean
loadFromData
(String data, long length, KeyFileFlags... flags) Loads a key file from memory into an emptyGKeyFile
structure.boolean
loadFromDataDirs
(String file, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) This function looks for a key file namedfile
in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), loads the file into this KeyFile and returns the file's full path infullPath
.boolean
loadFromDataDirs
(String file, @Nullable Out<String> fullPath, KeyFileFlags... flags) This function looks for a key file namedfile
in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), loads the file into this KeyFile and returns the file's full path infullPath
.boolean
loadFromDirs
(String file, String[] searchDirs, @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) This function looks for a key file namedfile
in the paths specified insearchDirs
, loads the file into this KeyFile and returns the file's full path infullPath
.boolean
loadFromDirs
(String file, String[] searchDirs, @Nullable Out<String> fullPath, KeyFileFlags... flags) This function looks for a key file namedfile
in the paths specified insearchDirs
, loads the file into this KeyFile and returns the file's full path infullPath
.boolean
loadFromFile
(String file, Set<KeyFileFlags> flags) Loads a key file into an emptyGKeyFile
structure.boolean
loadFromFile
(String file, KeyFileFlags... flags) Loads a key file into an emptyGKeyFile
structure.ref()
Increases the reference count of this KeyFile.boolean
removeComment
(@Nullable String groupName, @Nullable String key) Removes a comment abovekey
fromgroupName
.boolean
removeGroup
(String groupName) Removes the specified group,groupName
, from the key file.boolean
Removeskey
ingroupName
from the key file.boolean
saveToFile
(String filename) Writes the contents of this KeyFile tofilename
using g_file_set_contents().void
setBoolean
(String groupName, String key, boolean value) Associates a new boolean value withkey
undergroupName
.void
setBooleanList
(String groupName, String key, boolean[] list) Associates a list of boolean values withkey
undergroupName
.boolean
setComment
(@Nullable String groupName, @Nullable String key, String comment) Places a comment abovekey
fromgroupName
.void
Associates a new double value withkey
undergroupName
.void
setDoubleList
(String groupName, String key, double[] list) Associates a list of double values withkey
undergroupName
.void
Associates a new integer value withkey
undergroupName
.void
setInteger
(String groupName, String key, int value) Associates a new integer value withkey
undergroupName
.void
setIntegerList
(String groupName, String key, int[] list) Associates a list of integer values withkey
undergroupName
.void
setListSeparator
(byte separator) Sets the character which is used to separate values in lists.void
setLocaleString
(String groupName, String key, String locale, String string) Associates a string value forkey
andlocale
undergroupName
.void
setLocaleStringList
(String groupName, String key, String locale, String[] list) Associates a list of string values forkey
andlocale
undergroupName
.void
Associates a new string value withkey
undergroupName
.void
setStringList
(String groupName, String key, String[] list) Associates a list of string values forkey
undergroupName
.void
Associates a new integer value withkey
undergroupName
.void
Associates a new value withkey
undergroupName
.This function outputs this KeyFile as a string.void
unref()
Decreases the reference count of this KeyFile by 1.Methods inherited from class io.github.jwharm.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 emptyGKeyFile
object. Use g_key_file_load_from_file(), g_key_file_load_from_data(), g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to read an existing key file.
-
-
Method Details
-
getType
-
errorQuark
-
free
public void free()Clears all keys and groups from this KeyFile, and decreases the reference count by 1. If the reference count reaches zero, frees the key file and all its allocated memory. -
getBoolean
Returns the value associated withkey
undergroupName
as a boolean.If
key
cannot be found thenfalse
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as a boolean thenfalse
is returned anderror
is set toKeyFileError.INVALID_VALUE
.- 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
-
getBooleanList
Returns the values associated withkey
undergroupName
as booleans.If
key
cannot be found thennull
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as booleans thennull
is returned anderror
is set toKeyFileError.INVALID_VALUE
.- Parameters:
groupName
- a group namekey
- 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 g_free() when no longer needed. - Throws:
GErrorException
- seeGError
-
getComment
public String getComment(@Nullable @Nullable String groupName, @Nullable @Nullable String key) throws GErrorException Retrieves a comment abovekey
fromgroupName
. Ifkey
isnull
thencomment
will be read from abovegroupName
. If bothkey
andgroupName
arenull
, thencomment
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.- Parameters:
groupName
- a group name, ornull
key
- a key- Returns:
- a comment that should be freed with g_free()
- Throws:
GErrorException
- seeGError
-
getDouble
Returns the value associated withkey
undergroupName
as a double. IfgroupName
isnull
, the start_group is used.If
key
cannot be found then 0.0 is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as a double then 0.0 is returned anderror
is set toKeyFileError.INVALID_VALUE
.- Parameters:
groupName
- a group namekey
- 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.
- Throws:
GErrorException
- seeGError
-
getDoubleList
Returns the values associated withkey
undergroupName
as doubles.If
key
cannot be found thennull
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as doubles thennull
is returned anderror
is set toKeyFileError.INVALID_VALUE
.- Parameters:
groupName
- a group namekey
- 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 g_free() when no longer needed. - Throws:
GErrorException
- seeGError
-
getGroups
Returns all groups in the key file loaded with this KeyFile. The array of returned groups will benull
-terminated, solength
may optionally benull
.- Parameters:
length
- return location for the number of returned groups, ornull
- Returns:
- a newly-allocated
null
-terminated array of strings. Use g_strfreev() to free it.
-
getInt64
Returns the value associated withkey
undergroupName
as a signed 64-bit integer. This is similar to g_key_file_get_integer() but can return 64-bit results without truncation.- Parameters:
groupName
- a non-null
group namekey
- a non-null
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.
- Throws:
GErrorException
- seeGError
-
getInteger
Returns the value associated withkey
undergroupName
as an integer.If
key
cannot be found then 0 is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the value associated withkey
cannot be interpreted as an integer, or is out of range for agint
, then 0 is returned anderror
is set toKeyFileError.INVALID_VALUE
.- Parameters:
groupName
- a group namekey
- 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.
- Throws:
GErrorException
- seeGError
-
getIntegerList
Returns the values associated withkey
undergroupName
as integers.If
key
cannot be found thennull
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. Likewise, if the values associated withkey
cannot be interpreted as integers, or are out of range forgint
, thennull
is returned anderror
is set toKeyFileError.INVALID_VALUE
.- Parameters:
groupName
- a group namekey
- 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 g_free() when no longer needed. - Throws:
GErrorException
- seeGError
-
getKeys
public String[] getKeys(String groupName, @Nullable @Nullable Out<Long> length) throws GErrorException Returns all keys for the group namegroupName
. The array of returned keys will benull
-terminated, solength
may optionally benull
. In the event that thegroupName
cannot be found,null
is returned anderror
is set toKeyFileError.GROUP_NOT_FOUND
.- Parameters:
groupName
- a group namelength
- return location for the number of keys returned, ornull
- Returns:
- a newly-allocated
null
-terminated array of strings. Use g_strfreev() to free it. - Throws:
GErrorException
- seeGError
-
getLocaleForKey
Returns the actual locale which the result of g_key_file_get_locale_string() or g_key_file_get_locale_string_list() came from.If calling g_key_file_get_locale_string() or g_key_file_get_locale_string_list() with exactly the same this KeyFile,
groupName
,key
andlocale
, 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 ornull
- Returns:
- the locale from the file, or
null
if the key was not found or the entry in the file was was untranslated
-
getLocaleString
public String getLocaleString(String groupName, String key, @Nullable @Nullable String locale) throws GErrorException Returns the value associated withkey
undergroupName
translated in the givenlocale
if available. Iflocale
isnull
then the current locale is assumed.If
locale
is to be non-null
, or if the current locale will change over the lifetime of theGKeyFile
, it must be loaded withKeyFileFlags.KEEP_TRANSLATIONS
in order to load strings for all locales.If
key
cannot be found thennull
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. If the value associated withkey
cannot 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 ornull
- Returns:
- a newly allocated string or
null
if the specified key cannot be found. - Throws:
GErrorException
- seeGError
-
getLocaleStringList
public String[] getLocaleStringList(String groupName, String key, @Nullable @Nullable String locale) throws GErrorException Returns the values associated withkey
undergroupName
translated in the givenlocale
if available. Iflocale
isnull
then the current locale is assumed.If
locale
is to be non-null
, or if the current locale will change over the lifetime of theGKeyFile
, it must be loaded withKeyFileFlags.KEEP_TRANSLATIONS
in order to load strings for all locales.If
key
cannot be found thennull
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. If the values associated withkey
cannot be interpreted or no suitable translations can be found then the untranslated values are returned. The returned array isnull
-terminated, solength
may optionally benull
.- Parameters:
groupName
- a group namekey
- a keylocale
- a locale identifier ornull
- Returns:
- a newly allocated
null
-terminated string array ornull
if the key isn't found. The string array should be freed with g_strfreev(). - Throws:
GErrorException
- seeGError
-
getStartGroup
Returns the name of the start group of the file.- Returns:
- The start group of the key file.
-
getString
Returns the string value associated withkey
undergroupName
. Unlike g_key_file_get_value(), this function handles escape sequences like \\s.In the event the key cannot be found,
null
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. In the event that thegroupName
cannot be found,null
is returned anderror
is set toKeyFileError.GROUP_NOT_FOUND
.- Parameters:
groupName
- a group namekey
- a key- Returns:
- a newly allocated string or
null
if the specified key cannot be found. - Throws:
GErrorException
- seeGError
-
getStringList
Returns the values associated withkey
undergroupName
.In the event the key cannot be found,
null
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. In the event that thegroupName
cannot be found,null
is returned anderror
is set toKeyFileError.GROUP_NOT_FOUND
.- Parameters:
groupName
- a group namekey
- a key- Returns:
- a
null
-terminated string array ornull
if the specified key cannot be found. The array should be freed with g_strfreev(). - Throws:
GErrorException
- seeGError
-
getUint64
Returns the value associated withkey
undergroupName
as an unsigned 64-bit integer. This is similar to g_key_file_get_integer() but can return large positive results without truncation.- Parameters:
groupName
- a non-null
group namekey
- a non-null
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.
- Throws:
GErrorException
- seeGError
-
getValue
Returns the raw value associated withkey
undergroupName
. Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.In the event the key cannot be found,
null
is returned anderror
is set toKeyFileError.KEY_NOT_FOUND
. In the event that thegroupName
cannot be found,null
is returned anderror
is set toKeyFileError.GROUP_NOT_FOUND
.- Parameters:
groupName
- a group namekey
- a key- Returns:
- a newly allocated string or
null
if the specified key cannot be found. - Throws:
GErrorException
- seeGError
-
hasGroup
Looks whether the key file has the groupgroupName
.- Parameters:
groupName
- a group name- Returns:
true
ifgroupName
is a part of this KeyFile,false
otherwise.
-
hasKey
Looks whether the key file has the keykey
in the groupgroupName
.Note that this function does not follow the rules for
GError
strictly; the return value both carries meaning and signals an error. To use this function, you must pass aGError
pointer inerror
, and check whether it is notnull
to see if an error occurred.Language bindings should use g_key_file_get_value() to test whether or not a key exists.
- Parameters:
groupName
- a group namekey
- a key name- Returns:
true
ifkey
is a part ofgroupName
,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromBytes
Loads a key file from the data inbytes
into an emptyGKeyFile
structure. If the object cannot be created thenerror
is set to aGKeyFileError
.- Parameters:
bytes
- aGBytes
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromBytes
Loads a key file from the data inbytes
into an emptyGKeyFile
structure. If the object cannot be created thenerror
is set to aGKeyFileError
.- Parameters:
bytes
- aGBytes
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromData
public boolean loadFromData(String data, long length, Set<KeyFileFlags> flags) throws GErrorException Loads a key file from memory into an emptyGKeyFile
structure. If the object cannot be created thenerror
is set to aGKeyFileError
.- Parameters:
data
- key file loaded in memorylength
- the length ofdata
in bytes (or (gsize)-1 if data is nul-terminated)flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromData
Loads a key file from memory into an emptyGKeyFile
structure. If the object cannot be created thenerror
is set to aGKeyFileError
.- Parameters:
data
- key file loaded in memorylength
- the length ofdata
in bytes (or (gsize)-1 if data is nul-terminated)flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromDataDirs
public boolean loadFromDataDirs(String file, @Nullable @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) throws GErrorException This function looks for a key file namedfile
in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), loads the file into this KeyFile and returns the file's full path infullPath
. If the file could not be loaded then anerror
is set to either aGFileError
orGKeyFileError
.- Parameters:
file
- a relative path to a filename to open and parsefullPath
- return location for a string containing the full path of the file, ornull
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromDataDirs
public boolean loadFromDataDirs(String file, @Nullable @Nullable Out<String> fullPath, KeyFileFlags... flags) throws GErrorException This function looks for a key file namedfile
in the paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), loads the file into this KeyFile and returns the file's full path infullPath
. If the file could not be loaded then anerror
is set to either aGFileError
orGKeyFileError
.- Parameters:
file
- a relative path to a filename to open and parsefullPath
- return location for a string containing the full path of the file, ornull
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromDirs
public boolean loadFromDirs(String file, String[] searchDirs, @Nullable @Nullable Out<String> fullPath, Set<KeyFileFlags> flags) throws GErrorException This function looks for a key file namedfile
in the paths specified insearchDirs
, loads the file into this KeyFile and returns the file's full path infullPath
.If the file could not be found in any of the
searchDirs
,KeyFileError.NOT_FOUND
is returned. If the file is found but the OS returns an error when opening or reading the file, aG_FILE_ERROR
is returned. If there is a problem parsing the file, aG_KEY_FILE_ERROR
is 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, ornull
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromDirs
public boolean loadFromDirs(String file, String[] searchDirs, @Nullable @Nullable Out<String> fullPath, KeyFileFlags... flags) throws GErrorException This function looks for a key file namedfile
in the paths specified insearchDirs
, loads the file into this KeyFile and returns the file's full path infullPath
.If the file could not be found in any of the
searchDirs
,KeyFileError.NOT_FOUND
is returned. If the file is found but the OS returns an error when opening or reading the file, aG_FILE_ERROR
is returned. If there is a problem parsing the file, aG_KEY_FILE_ERROR
is 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, ornull
flags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromFile
Loads a key file into an emptyGKeyFile
structure.If the OS returns an error when opening or reading the file, a
G_FILE_ERROR
is returned. If there is a problem parsing the file, aG_KEY_FILE_ERROR
is returned.This function will never return a
KeyFileError.NOT_FOUND
error. If thefile
is not found,FileError.NOENT
is returned.- Parameters:
file
- the path of a filename to load, in the GLib filename encodingflags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
loadFromFile
Loads a key file into an emptyGKeyFile
structure.If the OS returns an error when opening or reading the file, a
G_FILE_ERROR
is returned. If there is a problem parsing the file, aG_KEY_FILE_ERROR
is returned.This function will never return a
KeyFileError.NOT_FOUND
error. If thefile
is not found,FileError.NOENT
is returned.- Parameters:
file
- the path of a filename to load, in the GLib filename encodingflags
- flags fromGKeyFileFlags
- Returns:
true
if a key file could be loaded,false
otherwise- Throws:
GErrorException
- seeGError
-
ref
-
removeComment
public boolean removeComment(@Nullable @Nullable String groupName, @Nullable @Nullable String key) throws GErrorException Removes a comment abovekey
fromgroupName
. Ifkey
isnull
thencomment
will be removed abovegroupName
. If bothkey
andgroupName
arenull
, thencomment
will be removed above the first group in the file.- Parameters:
groupName
- a group name, ornull
key
- a key- Returns:
true
if the comment was removed,false
otherwise- Throws:
GErrorException
- seeGError
-
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
-
removeKey
Removeskey
ingroupName
from 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
-
saveToFile
Writes the contents of this KeyFile tofilename
using g_file_set_contents(). If you need stricter guarantees about durability of the written file than are provided by g_file_set_contents(), use g_file_set_contents_full() with the return value of g_key_file_to_data().This function can fail for any of the reasons that g_file_set_contents() may fail.
- Parameters:
filename
- the name of the file to write to- Returns:
true
if successful, elsefalse
witherror
set- Throws:
GErrorException
- seeGError
-
setBoolean
-
setBooleanList
Associates a list of boolean values withkey
undergroupName
. Ifkey
cannot be found then it is created. IfgroupName
isnull
, the start_group is used.- Parameters:
groupName
- a group namekey
- a keylist
- an array of boolean values
-
setComment
public boolean setComment(@Nullable @Nullable String groupName, @Nullable @Nullable String key, String comment) throws GErrorException Places a comment abovekey
fromgroupName
.If
key
isnull
thencomment
will be written abovegroupName
. If bothkey
andgroupName
arenull
, thencomment
will be written above the first group in the file.Note that this function prepends a '
'
comment marker to each line ofcomment
.- Parameters:
groupName
- a group name, ornull
key
- a keycomment
- a comment- Returns:
true
if the comment was written,false
otherwise- Throws:
GErrorException
- seeGError
-
setDouble
-
setDoubleList
-
setInt64
-
setInteger
-
setIntegerList
-
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
-
setLocaleString
Associates a string value forkey
andlocale
undergroupName
. If the translation forkey
cannot be found then it is created.- Parameters:
groupName
- a group namekey
- a keylocale
- a locale identifierstring
- a string
-
setLocaleStringList
Associates a list of string values forkey
andlocale
undergroupName
. If the translation forkey
cannot be found then it is created.- Parameters:
groupName
- a group namekey
- a keylocale
- a locale identifierlist
- anull
-terminated array of locale string values
-
setString
Associates a new string value withkey
undergroupName
. Ifkey
cannot be found then it is created. IfgroupName
cannot be found then it is created. Unlike g_key_file_set_value(), this function handles characters that need escaping, such as newlines.- Parameters:
groupName
- a group namekey
- a keystring
- a string
-
setStringList
Associates a list of string values forkey
undergroupName
. Ifkey
cannot be found then it is created. IfgroupName
cannot be found then it is created.- Parameters:
groupName
- a group namekey
- a keylist
- an array of string values
-
setUint64
-
setValue
Associates a new value withkey
undergroupName
.If
key
cannot be found then it is created. IfgroupName
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 g_key_file_set_string().- Parameters:
groupName
- a group namekey
- a keyvalue
- a string
-
toData
This function outputs this KeyFile as a string.Note that this function never reports an error, so it is safe to pass
null
aserror
.- Parameters:
length
- return location for the length of the returned string, ornull
- Returns:
- a newly allocated string holding
the contents of the
GKeyFile
- Throws:
GErrorException
- seeGError
-
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.
-