键值形式的文件解析api-解析类ini形式的配置文件

glib-键值形式的文件解析api

解析类ini形式的配置文件

前言

本文转自https://developer.gnome.org/glib/unstable/glib-Key-value-file-parser.html

业余时间进行翻译,欢迎批评指正。

函数

GKeyFile *g_key_file_new ()
voidg_key_file_free ()
GKeyFile *g_key_file_ref ()
voidg_key_file_unref ()
voidg_key_file_set_list_separator ()
gbooleang_key_file_load_from_file ()
gbooleang_key_file_load_from_data ()
gbooleang_key_file_load_from_data_dirs ()
gbooleang_key_file_load_from_dirs ()
gchar *g_key_file_to_data ()
gbooleang_key_file_save_to_file ()
gchar *g_key_file_get_start_group ()
gchar **g_key_file_get_groups ()
gchar **g_key_file_get_keys ()
gbooleang_key_file_has_group ()
gbooleang_key_file_has_key ()
gchar *g_key_file_get_value ()
gchar *g_key_file_get_string ()
gchar *g_key_file_get_locale_string ()
gbooleang_key_file_get_boolean ()
gintg_key_file_get_integer ()
gint64g_key_file_get_int64 ()
guint64g_key_file_get_uint64 ()
gdoubleg_key_file_get_double ()
gchar **g_key_file_get_string_list ()
gchar **g_key_file_get_locale_string_list ()
gboolean *g_key_file_get_boolean_list ()
gint *g_key_file_get_integer_list ()
gdouble *g_key_file_get_double_list ()
gchar *g_key_file_get_comment ()
voidg_key_file_set_value ()
voidg_key_file_set_string ()
voidg_key_file_set_locale_string ()
voidg_key_file_set_boolean ()
voidg_key_file_set_integer ()
voidg_key_file_set_int64 ()
voidg_key_file_set_uint64 ()
voidg_key_file_set_double ()
voidg_key_file_set_string_list ()
voidg_key_file_set_locale_string_list ()
voidg_key_file_set_boolean_list ()
voidg_key_file_set_integer_list ()
voidg_key_file_set_double_list ()
gbooleang_key_file_set_comment ()
gbooleang_key_file_remove_group ()
gbooleang_key_file_remove_key ()
gbooleang_key_file_remove_comment ()

使用时需添加引用

#include <glib.h>

描述

使用GKeyFile可以解析、编辑或者创建包含使用分组形式的键值对格式的文件。姑且称这种文件为“键值”文件。freedesktop.org中的有些配置文件使用了这种格式,如Desktop Entry SpecificationIcon Theme Specification等。

“键值”文件的详细语法可以参考Desktop Entry Specification。此处仅做简要介绍。

“键值”文件是由多个分组形式的键值对组成,其中可以包含注释。示例如下:

# 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
Welcome[be@latin]=Hello

[Another Group]

Numbers=2;20;-200;0

Booleans=true;false;true;true

以'#'开头的行和空行为注释行。

分组的起始行由‘[’和‘]’包含的分组名。下一个分组的开始或文件尾表示分组的结束。每一个键值对必须包含在一个分组中。

键值对通常的形式为键=值。 但对于本地化字符串,其表示形式为键[locale]=值,其中lang_COUNTRY@MODIFIER中,COUNTRYMODIFIER是可选的。忽略'='前后的空格。换行符,制表符,回车符和反斜杠字符分别使用\n,\t, \r和 \表示。 To preserve leading spaces in values,these can also be escaped as \s.

键值文件可以存储字符串、整数、布尔值和列表。列表通常使用';'或者 ','分隔符进行分割。要在列表值中使用列表分隔符,必须加上一个反斜杠转义前缀。

键值文件的格式与Windows平台的ini文件类似,但是二者有明显的差异:

  • .ini文件使用 ';注释行,键值文件使用 '#'注释行;

  • 键值文件不允许又未分组键值对,即第一个分组前的所有内容都必须是注释;

  • 键值文件的编码格式为UTF-8;

  • 键值文件中键和值是大小写敏感的,比如[GROUP]和[group]表示两个不同的分组。

  • .ini文件中没有boolean类型,只能用GetProfileInt()获取整形数表示boolean类型,而键值文件只能使用true和false(必须小写)表示boolean类型。

注意与Desktop Entry Specification相比,一个键可以在同一个分组中出现多次,但只有最后一个有效。键值文件中也可以出现多个名称相同的分组,每个分组下的键值对均有效。另一个区别是分组和键的名称并不仅限于ASCII字符。

接口函数

g_key_file_new ()

GKeyFile * g_key_file_new (void);

创建空的GKeyFile 对象。可以使用g_key_file_load_from_file(),g_key_file_load_from_data(),g_key_file_load_from_dirs() 或者 g_key_file_load_from_data_dirs() 从已有GKeyFile 对象读取书记。

返回值:空的GKeyFile.

适用版本:2.6及以上


g_key_file_free ()

void g_key_file_free (GKeyFile *key_file);

释放key_file中的所有键和组,引用计数减1。如果引用计数到0是,释放GkeyFile及所有已分配的内存。

参数:

key_file

GKeyFile

 

适用版本:2.6及以上


g_key_file_ref ()

GKeyFile * g_key_file_ref (GKeyFile *key_file);

增加键值文件key_file的引用计数

参数

key_file

键值文件GKeyFile

 
返回值

与输入参数的键值文件key_file相同

适用版本:2.32及以上


g_key_file_unref ()

void g_key_file_unref (GKeyFile *key_file);

对键值文件key_file的引用计数减1。如果减1后引用计数为0,则释放键值文件及其分配的内存。

参数

key_file

键值文件GKeyFile

 

适用版本:2.32及以上


g_key_file_set_list_separator ()

void g_key_file_set_list_separator (GKeyFile *key_file,gchar separator);

设置列表值的分隔符,通常使用';'或者','作为分隔符,默认使用';'。

参数

key_file

键值文件GKeyFile

 

separator

分隔符

 

适用版本:2.6及以上


g_key_file_load_from_file ()

gboolean g_key_file_load_from_file (GKeyFile *key_file,const gchar *file,GKeyFileFlags flags,GError **error);

加载键值文件到一个空的GKeyFile结构。如果加载失败,返回的错误为GFileError或者GKeyFileError

参数

key_file

一个空的GKeyFile结构

 

file

键值文件的路径,使用GLib文件编码

[type filename]

flags

参见 GKeyFileFlags

 

error

加载失败时返回GError或者NULL

 
Returns

加载成功返回TRUE,否则返回FALSE

适用版本:2.6及以上


g_key_file_load_from_data ()

gboolean
g_key_file_load_from_data (GKeyFile *key_file,const gchar *data,gsize length,GKeyFileFlags flags,GError **error);

从内存中加载到一个空的GKeyFile结构, 如果加载失败则设置error为GKeyFileError

参数

key_file

一个空的GKeyFile结构

 

data

内存中的键值文件

 

length

data变量的字节大小(如果以空字符结束则问长度-1)the length of datain bytes (or (gsize)-1 if data is nul-terminated)

 

flags

参见GKeyFileFlags

 

error

加载失败时返回GError或者NULL

 
返回

加载成功返回TRUE,否则返回FALSE

适用版本:2.6及以上


g_key_file_load_from_data_dirs ()

gboolean g_key_file_load_from_data_dirs (GKeyFile *key_file,const gchar *file,gchar **full_path,GKeyFileFlags flags,GError **error);

这个函数从g_get_user_data_dir()g_get_system_data_dirs()返回的文件夹中查找名称为file的键值文件,并将其加载到key_file,同时返回file的完整路径。如果文件加载失败则可以通过error变量获取返回的GFileError错误或者GKeyFileError错误。

参数

key_file

一个空的GKeyFile 结构

 

file

使用相对路径表示的,要打开并解析的文件名称

[type filename]

full_path

返回文件的完整路径,类型为字符串,或者返回NULL

[out][type filename][allow-none]

flags

参见GKeyFileFlags

 

error

返回GError或者NULL

 
返回

如果文件可加载返回TRUE,否则返回FALSE。

适用版本:2.6及以上


g_key_file_load_from_dirs ()

gboolean g_key_file_load_from_dirs (GKeyFile *key_file,const gchar *file,const gchar **search_dirs,gchar **full_path,GKeyFileFlags flags,GError **error);

这个函数从search_dirs指示的文件夹中查找名称为file的键值文件,并将其加载到key_file,同时返回file的完整路径。如果文件加载失败则可以通过error变量获取返回的GFileError错误或者GKeyFileError错误。

参数

key_file

空的GKeyFile结构

 

file

使用相对路径表示的,要打开并解析的文件名称

[type filename]

search_dirs

要搜索的文件夹数组

[array zero-terminated=1][element-type filename]

full_path

返回文件的完整路径,类型为字符串,或者返回NULL

[out][type filename][allow-none]

flags

参见GKeyFileFlags

 

error

返回GError或者NULL

 
返回

如果文件可加载返回TRUE,否则返回FALSE。

适用版本: 2.32及以上


g_key_file_to_data ()

gchar * g_key_file_to_data (GKeyFile *key_file,gsize *length,GError **error);

此函数根据输入的键值文件key_file返回相应长度的字符串。

请注意,此函数从不报告错误,因此向error传递NULL是安全的。

参数

key_file

GKeyFile 结构

 

length

返回字符串的长度或者NULL

[out][allow-none]

error

返回GError或者NULL

 
Returns

用于保存GKeyFile的新分配的字符串。 

适用版本:2.6及以上


g_key_file_save_to_file ()

gboolean g_key_file_save_to_file (GKeyFile *key_file,const gchar *filename,GError **error);

通过g_file_set_contents().将键值文件key_file的内容写入到filename。g_file_set_contents()执行失败将导致本函数执行失败。

Parameters

key_file

键值文件

 

filename

要写入的文件

 

error

返回  GError或者NULL

 
返回

操作成功返回TRUE,否则返回FALSE,可通过error获取错误信息

适用版本:2.6及以上


g_key_file_get_start_group ()

gchar * g_key_file_get_start_group (GKeyFile *key_file);

返回键值文件中第一个分组的名称

参数

key_file

键值文件

 
返回

键值文件的第一个分组的名称

适用版本: 2.6及以上

g_key_file_get_groups ()

gchar **
g_key_file_get_groups (GKeyFile *key_file,
                       gsize *length);

Returns all groups in the key file loaded with key_file. The array of returned groups will beNULL-terminated, so length may optionally beNULL.

Parameters

key_file

a GKeyFile

 

length

return location for the number of returned groups, or NULL.

[out][allow-none]
Returns

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

[array zero-terminated=1][transfer full]

Since: 2.6


g_key_file_get_keys ()

gchar **
g_key_file_get_keys (GKeyFile *key_file,
                     const gchar *group_name,
                     gsize *length,
                     GError **error);

Returns all keys for the group name group_name. The array ofreturned keys will beNULL-terminated, so length mayoptionally beNULL. In the event that the group_name cannotbe found,NULL is returned and error is set toG_KEY_FILE_ERROR_GROUP_NOT_FOUND.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

length

return location for the number of keys returned, or NULL.

[out][allow-none]

error

return location for a GError, or NULL

 
Returns

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

[array zero-terminated=1][transfer full]

Since: 2.6


g_key_file_has_group ()

gboolean
g_key_file_has_group (GKeyFile *key_file,
                      const gchar *group_name);

Looks whether the key file has the group group_name.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 
Returns

TRUE ifgroup_nameis a part ofkey_file,FALSEotherwise.

Since: 2.6


g_key_file_has_key ()

gboolean
g_key_file_has_key (GKeyFile *key_file,
                    const gchar *group_name,
                    const gchar *key,
                    GError **error);

Looks whether the key file has the key key in the groupgroup_name.

Note that this function does not follow the rules for GError strictly;the return value both carries meaning and signals an error. To usethis function, you must pass aGError pointer in error, and checkwhether it is notNULL to see if an error occurred.

Language bindings should use g_key_file_get_value() to test whetheror not a key exists.

[skip]

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key name

 

error

return location for a GError

 
Returns

TRUE ifkeyis a part ofgroup_name,FALSE otherwise

Since: 2.6


g_key_file_get_value ()

gchar *
g_key_file_get_value (GKeyFile *key_file,
                      const gchar *group_name,
                      const gchar *key,
                      GError **error);

Returns the raw value associated with key undergroup_name. Useg_key_file_get_string() to retrieve an unescaped UTF-8 string.

In the event the key cannot be found, NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that thegroup_name cannot be found,NULL is returned and error is set toG_KEY_FILE_ERROR_GROUP_NOT_FOUND.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

error

return location for a GError, or NULL

 
Returns

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

Since: 2.6


g_key_file_get_string ()

gchar *
g_key_file_get_string (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       GError **error);

Returns the string value associated with key undergroup_name.Unlikeg_key_file_get_value(), this function handles escape sequenceslike \s.

In the event the key cannot be found, NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that thegroup_name cannot be found,NULL is returned and error is set toG_KEY_FILE_ERROR_GROUP_NOT_FOUND.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

error

return location for a GError, or NULL

 
Returns

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

Since: 2.6


g_key_file_get_locale_string ()

gchar *
g_key_file_get_locale_string (GKeyFile *key_file,
                              const gchar *group_name,
                              const gchar *key,
                              const gchar *locale,
                              GError **error);

Returns the value associated with key undergroup_nametranslated in the givenlocale if available. Iflocale isNULL then the current locale is assumed.

If key cannot be found then NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associatedwithkey cannot be interpreted or no suitable translation canbe found then the untranslated value is returned.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

locale

a locale identifier or NULL.

[allow-none]

error

return location for a GError, or NULL

 
Returns

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

Since: 2.6


g_key_file_get_boolean ()

gboolean
g_key_file_get_boolean (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        GError **error);

Returns the value associated with key undergroup_name as aboolean.

If key cannot be found then FALSE is returned and error is settoG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the valueassociated withkey cannot be interpreted as a boolean thenFALSEis returned and error is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

error

return location for a GError

 
Returns

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

Since: 2.6


g_key_file_get_integer ()

gint
g_key_file_get_integer (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        GError **error);

Returns the value associated with key undergroup_name as aninteger.

If key cannot be found then 0 is returned anderror is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associatedwith key cannot be interpreted as an integer then 0 is returnedanderror is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

error

return location for a GError

 
Returns

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

Since: 2.6


g_key_file_get_int64 ()

gint64
g_key_file_get_int64 (GKeyFile *key_file,
                      const gchar *group_name,
                      const gchar *key,
                      GError **error);

Returns the value associated with key undergroup_name as a signed64-bit integer. This is similar tog_key_file_get_integer() but can return64-bit results without truncation.

Parameters

key_file

a non-NULLGKeyFile

 

group_name

a non-NULL group name

 

key

a non-NULL key

 

error

return location for a GError

 
Returns

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

Since: 2.26


g_key_file_get_uint64 ()

guint64
g_key_file_get_uint64 (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       GError **error);

Returns the value associated with key undergroup_name as an unsigned64-bit integer. This is similar tog_key_file_get_integer() but can returnlarge positive results without truncation.

Parameters

key_file

a non-NULLGKeyFile

 

group_name

a non-NULL group name

 

key

a non-NULL key

 

error

return location for a GError

 
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.

Since: 2.26


g_key_file_get_double ()

gdouble
g_key_file_get_double (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       GError **error);

Returns the value associated with key undergroup_name as adouble. Ifgroup_name isNULL, the start_group is used.

If key cannot be found then 0.0 is returned anderror is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associatedwith key cannot be interpreted as a double then 0.0 is returnedanderror is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

error

return location for a GError

 
Returns

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

Since: 2.12


g_key_file_get_string_list ()

gchar **
g_key_file_get_string_list (GKeyFile *key_file,
                            const gchar *group_name,
                            const gchar *key,
                            gsize *length,
                            GError **error);

Returns the values associated with key undergroup_name.

In the event the key cannot be found, NULL is returned anderror is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. In theevent that thegroup_name cannot be found,NULL is returnedand error is set toG_KEY_FILE_ERROR_GROUP_NOT_FOUND.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

length

return location for the number of returned strings, or NULL.

[out][allow-none]

error

return location for a GError, or NULL

 
Returns

a NULL-terminated string array or NULL if the specifiedkey cannot be found. The array should be freed withg_strfreev().

[array zero-terminated=1 length=length][element-type utf8][transfer full]

Since: 2.6


g_key_file_get_locale_string_list ()

gchar **
g_key_file_get_locale_string_list (GKeyFile *key_file,
                                   const gchar *group_name,
                                   const gchar *key,
                                   const gchar *locale,
                                   gsize *length,
                                   GError **error);

Returns the values associated with key undergroup_nametranslated in the givenlocale if available. Iflocale isNULL then the current locale is assumed.

If key cannot be found then NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associatedwithkey cannot be interpreted or no suitable translationscan be found then the untranslated values are returned. The returned array isNULL-terminated, so length may optionally beNULL.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

locale

a locale identifier or NULL.

[allow-none]

length

return location for the number of returned strings or NULL.

[out][allow-none]

error

return location for a GError or NULL

 
Returns

a newly allocated NULL-terminated string arrayor NULL if the key isn't found. The string array should be freedwithg_strfreev().

[array zero-terminated=1 length=length][element-type utf8][transfer full]

Since: 2.6


g_key_file_get_boolean_list ()

gboolean *
g_key_file_get_boolean_list (GKeyFile *key_file,
                             const gchar *group_name,
                             const gchar *key,
                             gsize *length,
                             GError **error);

Returns the values associated with key undergroup_name asbooleans.

If key cannot be found then NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associatedwith key cannot be interpreted as booleans thenNULL is returnedand error is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

length

the number of booleans returned.

[out]

error

return location for a GError

 
Returns

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

[array length=length][element-type gboolean][transfer container]

Since: 2.6


g_key_file_get_integer_list ()

gint *
g_key_file_get_integer_list (GKeyFile *key_file,
                             const gchar *group_name,
                             const gchar *key,
                             gsize *length,
                             GError **error);

Returns the values associated with key undergroup_name asintegers.

If key cannot be found then NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associatedwith key cannot be interpreted as integers thenNULL is returnedand error is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

length

the number of integers returned.

[out]

error

return location for a GError

 
Returns

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

[array length=length][element-type gint][transfer container]

Since: 2.6


g_key_file_get_double_list ()

gdouble *
g_key_file_get_double_list (GKeyFile *key_file,
                            const gchar *group_name,
                            const gchar *key,
                            gsize *length,
                            GError **error);

Returns the values associated with key undergroup_name asdoubles.

If key cannot be found then NULL is returned and error is set toG_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associatedwith key cannot be interpreted as doubles thenNULL is returnedand error is set toG_KEY_FILE_ERROR_INVALID_VALUE.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

length

the number of doubles returned.

[out]

error

return location for a GError

 
Returns

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

[array length=length][element-type gdouble][transfer container]

Since: 2.12


g_key_file_get_comment ()

gchar *
g_key_file_get_comment (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        GError **error);

Retrieves a comment above key from group_name.If key is NULL then comment will be read from abovegroup_name. If bothkey andgroup_name areNULL, thencomment will be read from above the first group in the file.

Note that the returned string includes the '#' comment markers.

Parameters

key_file

a GKeyFile

 

group_name

a group name, or NULL.

[allow-none]

key

a key

 

error

return location for a GError

 
Returns

a comment that should be freed with g_free()

Since: 2.6


g_key_file_set_value ()

void
g_key_file_set_value (GKeyFile *key_file,
                      const gchar *group_name,
                      const gchar *key,
                      const gchar *value);

Associates a new value with key under group_name.

If key cannot be found then it is created. Ifgroup_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), useg_key_file_set_string().

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

a string

 

Since: 2.6


g_key_file_set_string ()

void
g_key_file_set_string (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       const gchar *string);

Associates a new string value with key undergroup_name. Ifkey cannot be found then it is created. Ifgroup_name cannot be found then it is created.Unlikeg_key_file_set_value(), this function handles charactersthat need escaping, such as newlines.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

string

a string

 

Since: 2.6


g_key_file_set_locale_string ()

void
g_key_file_set_locale_string (GKeyFile *key_file,
                              const gchar *group_name,
                              const gchar *key,
                              const gchar *locale,
                              const gchar *string);

Associates a string value for key and locale under group_name.If the translation forkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

locale

a locale identifier

 

string

a string

 

Since: 2.6


g_key_file_set_boolean ()

void
g_key_file_set_boolean (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        gboolean value);

Associates a new boolean value with key undergroup_name.Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

TRUE orFALSE

 

Since: 2.6


g_key_file_set_integer ()

void
g_key_file_set_integer (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        gint value);

Associates a new integer value with key undergroup_name.Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

an integer value

 

Since: 2.6


g_key_file_set_int64 ()

void
g_key_file_set_int64 (GKeyFile *key_file,
                      const gchar *group_name,
                      const gchar *key,
                      gint64 value);

Associates a new integer value with key undergroup_name.Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

an integer value

 

Since: 2.26


g_key_file_set_uint64 ()

void
g_key_file_set_uint64 (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       guint64 value);

Associates a new integer value with key undergroup_name.Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

an integer value

 

Since: 2.26


g_key_file_set_double ()

void
g_key_file_set_double (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       gdouble value);

Associates a new double value with key undergroup_name.Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

value

an double value

 

Since: 2.12


g_key_file_set_string_list ()

void
g_key_file_set_string_list (GKeyFile *key_file,
                            const gchar *group_name,
                            const gchar *key,
                            const gchar * const list[],
                            gsize length);

Associates a list of string values for key undergroup_name.Ifkey cannot be found then it is created.Ifgroup_name cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

list

an array of string values.

[array zero-terminated=1 length=length][element-type utf8]

length

number of string values in list

 

Since: 2.6


g_key_file_set_locale_string_list ()

void
g_key_file_set_locale_string_list (GKeyFile *key_file,
                                   const gchar *group_name,
                                   const gchar *key,
                                   const gchar *locale,
                                   const gchar * const list[],
                                   gsize length);

Associates a list of string values for key andlocale undergroup_name. If the translation forkey cannot be found thenit is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

locale

a locale identifier

 

list

a NULL-terminated array of locale string values.

[array zero-terminated=1 length=length]

length

the length of list

 

Since: 2.6


g_key_file_set_boolean_list ()

void
g_key_file_set_boolean_list (GKeyFile *key_file,
                             const gchar *group_name,
                             const gchar *key,
                             gboolean list[],
                             gsize length);

Associates a list of boolean values with key undergroup_name. Ifkey cannot be found then it is created.Ifgroup_name isNULL, the start_group is used.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

list

an array of boolean values.

[array length=length]

length

length of list

 

Since: 2.6


g_key_file_set_integer_list ()

void
g_key_file_set_integer_list (GKeyFile *key_file,
                             const gchar *group_name,
                             const gchar *key,
                             gint list[],
                             gsize length);

Associates a list of integer values with key undergroup_name. Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

list

an array of integer values.

[array length=length]

length

number of integer values in list

 

Since: 2.6


g_key_file_set_double_list ()

void
g_key_file_set_double_list (GKeyFile *key_file,
                            const gchar *group_name,
                            const gchar *key,
                            gdouble list[],
                            gsize length);

Associates a list of double values with key undergroup_name. Ifkey cannot be found then it is created.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key

 

list

an array of double values.

[array length=length]

length

number of double values in list

 

Since: 2.12


g_key_file_set_comment ()

gboolean
g_key_file_set_comment (GKeyFile *key_file,
                        const gchar *group_name,
                        const gchar *key,
                        const gchar *comment,
                        GError **error);

Places a comment above key from group_name.

If key is NULL then comment will be written abovegroup_name.If bothkey andgroup_name areNULL, thencomment will bewritten above the first group in the file.

Note that this function prepends a '#' comment marker toeach line of comment.

Parameters

key_file

a GKeyFile

 

group_name

a group name, or NULL.

[allow-none]

key

a key.

[allow-none]

comment

a comment

 

error

return location for a GError

 
Returns

TRUE if the comment was written,FALSE otherwise

Since: 2.6


g_key_file_remove_group ()

gboolean
g_key_file_remove_group (GKeyFile *key_file,
                         const gchar *group_name,
                         GError **error);

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

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

error

return location for a GError or NULL

 
Returns

TRUE if the group was removed,FALSE otherwise

Since: 2.6


g_key_file_remove_key ()

gboolean
g_key_file_remove_key (GKeyFile *key_file,
                       const gchar *group_name,
                       const gchar *key,
                       GError **error);

Removes key in group_name from the key file.

Parameters

key_file

a GKeyFile

 

group_name

a group name

 

key

a key name to remove

 

error

return location for a GError or NULL

 
Returns

TRUE if the key was removed,FALSE otherwise

Since: 2.6


g_key_file_remove_comment ()

gboolean
g_key_file_remove_comment (GKeyFile *key_file,
                           const gchar *group_name,
                           const gchar *key,
                           GError **error);

Removes a comment above key from group_name.If key is NULL then comment will be removed abovegroup_name. If bothkey andgroup_name areNULL, thencomment willbe removed above the first group in the file.

Parameters

key_file

a GKeyFile

 

group_name

a group name, or NULL.

[allow-none]

key

a key.

[allow-none]

error

return location for a GError

 
Returns

TRUE if the comment was removed,FALSE otherwise

Since: 2.6

Types and Values

GKeyFile

typedef struct _GKeyFile GKeyFile;

The GKeyFile struct contains only private dataand should not be accessed directly.


G_KEY_FILE_ERROR

#define G_KEY_FILE_ERROR g_key_file_error_quark()

Error domain for key file parsing. Errors in this domain willbe from the GKeyFileError enumeration.

See GError for information on error domains.


enum GKeyFileError

Error codes returned by key file parsing.

Members

G_KEY_FILE_ERROR_UNKNOWN_ENCODING

the text being parsed was in an unknown encoding

 

G_KEY_FILE_ERROR_PARSE

document was ill-formed

 

G_KEY_FILE_ERROR_NOT_FOUND

the file was not found

 

G_KEY_FILE_ERROR_KEY_NOT_FOUND

a requested key was not found

 

G_KEY_FILE_ERROR_GROUP_NOT_FOUND

a requested group was not found

 

G_KEY_FILE_ERROR_INVALID_VALUE

a value could not be parsed

 

enum GKeyFileFlags

Flags which influence the parsing.

Members

G_KEY_FILE_NONE

No flags, default behaviour

 

G_KEY_FILE_KEEP_COMMENTS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise all comments will be lost when the key file is written back.

 

G_KEY_FILE_KEEP_TRANSLATIONS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise only the translations for the current language will be written back.

 

G_KEY_FILE_DESKTOP_GROUP

#define G_KEY_FILE_DESKTOP_GROUP                "Desktop Entry"

The name of the main group of a desktop entry file, as defined in theDesktop Entry Specification.Consult the specification for moredetails about the meanings of the keys below.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_TYPE

#define G_KEY_FILE_DESKTOP_KEY_TYPE             "Type"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringgiving the type of the desktop entry. UsuallyG_KEY_FILE_DESKTOP_TYPE_APPLICATION,G_KEY_FILE_DESKTOP_TYPE_LINK, orG_KEY_FILE_DESKTOP_TYPE_DIRECTORY.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_VERSION

#define G_KEY_FILE_DESKTOP_KEY_VERSION          "Version"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringgiving the version of the Desktop Entry Specification used forthe desktop entry file.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_NAME

#define G_KEY_FILE_DESKTOP_KEY_NAME             "Name"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a localizedstring giving the specific name of the desktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME

#define G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME     "GenericName"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a localizedstring giving the generic name of the desktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY

#define G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY       "NoDisplay"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a booleanstating whether the desktop entry should be shown in menus.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_COMMENT

#define G_KEY_FILE_DESKTOP_KEY_COMMENT          "Comment"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a localizedstring giving the tooltip for the desktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_ICON

#define G_KEY_FILE_DESKTOP_KEY_ICON             "Icon"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a localizedstring giving the name of the icon to be displayed for the desktopentry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_HIDDEN

#define G_KEY_FILE_DESKTOP_KEY_HIDDEN           "Hidden"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a booleanstating whether the desktop entry has been deleted by the user.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN

#define G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN     "OnlyShowIn"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a list ofstrings identifying the environments that should display thedesktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN

#define G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN      "NotShowIn"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a list ofstrings identifying the environments that should not display thedesktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_TRY_EXEC

#define G_KEY_FILE_DESKTOP_KEY_TRY_EXEC         "TryExec"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringgiving the file name of a binary on disk used to determine if theprogram is actually installed. It is only valid for desktop entrieswith theApplication type.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_EXEC

#define G_KEY_FILE_DESKTOP_KEY_EXEC             "Exec"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringgiving the command line to execute. It is only valid for desktopentries with theApplication type.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_PATH

#define G_KEY_FILE_DESKTOP_KEY_PATH             "Path"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringcontaining the working directory to run the program in. It is onlyvalid for desktop entries with theApplication type.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_TERMINAL

#define G_KEY_FILE_DESKTOP_KEY_TERMINAL         "Terminal"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a booleanstating whether the program should be run in a terminal window.It is only valid for desktop entries with theApplication type.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_MIME_TYPE

#define G_KEY_FILE_DESKTOP_KEY_MIME_TYPE        "MimeType"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a listof strings giving the MIME types supported by this desktop entry.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_CATEGORIES

#define G_KEY_FILE_DESKTOP_KEY_CATEGORIES       "Categories"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a listof strings giving the categories in which the desktop entryshould be shown in a menu.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY

#define G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY   "StartupNotify"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a booleanstating whether the application supports theStartup Notification Protocol Specification.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS

#define G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS "StartupWMClass"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is stringidentifying the WM class or name hint of a window that the applicationwill create, which can be used to emulate Startup Notification witholder applications.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_URL

#define G_KEY_FILE_DESKTOP_KEY_URL              "URL"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a stringgiving the URL to access. It is only valid for desktop entrieswith theLink type.

Since: 2.14


G_KEY_FILE_DESKTOP_KEY_ACTIONS

#define G_KEY_FILE_DESKTOP_KEY_ACTIONS          "Actions"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a string listgiving the available application actions.

Since: 2.38


G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE

#define G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE "DBusActivatable"

A key under G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to trueif the application is D-Bus activatable.

Since: 2.38


G_KEY_FILE_DESKTOP_TYPE_APPLICATION

#define G_KEY_FILE_DESKTOP_TYPE_APPLICATION     "Application"

The value of the G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktopentries representing applications.

Since: 2.14


G_KEY_FILE_DESKTOP_TYPE_LINK

#define G_KEY_FILE_DESKTOP_TYPE_LINK            "Link"

The value of the G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktopentries representing links to documents.

Since: 2.14


G_KEY_FILE_DESKTOP_TYPE_DIRECTORY

#define G_KEY_FILE_DESKTOP_TYPE_DIRECTORY       "Directory"

The value of the G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktopentries representing directories.

Since: 2.14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值