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

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

解析类ini形式的配置文件

 

前言

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

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

函数

GKeyFile * g_key_file_new ()
void g_key_file_free ()
GKeyFile * g_key_file_ref ()
void g_key_file_unref ()
void g_key_file_set_list_separator ()
gboolean g_key_file_load_from_file ()
gboolean g_key_file_load_from_data ()
gboolean g_key_file_load_from_data_dirs ()
gboolean g_key_file_load_from_dirs ()
gchar * g_key_file_to_data ()
gboolean g_key_file_save_to_file ()
gchar * g_key_file_get_start_group ()
gchar ** g_key_file_get_groups ()
gchar ** g_key_file_get_keys ()
gboolean g_key_file_has_group ()
gboolean g_key_file_has_key ()
gchar * g_key_file_get_value ()
gchar * g_key_file_get_string ()
gchar * g_key_file_get_locale_string ()
gboolean g_key_file_get_boolean ()
gint g_key_file_get_integer ()
gint64 g_key_file_get_int64 ()
guint64 g_key_file_get_uint64 ()
gdouble g_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 ()
void g_key_file_set_value ()
void g_key_file_set_string ()
void g_key_file_set_locale_string ()
void g_key_file_set_boolean ()
void g_key_file_set_integer ()
void g_key_file_set_int64 ()
void g_key_file_set_uint64 ()
void g_key_file_set_double ()
void g_key_file_set_string_list ()
void g_key_file_set_locale_string_list ()
void g_key_file_set_boolean_list ()
void g_key_file_set_integer_list ()
void g_key_file_set_double_list ()
gboolean g_key_file_set_comment ()
gboolean g_key_file_remove_group ()
gboolean g_key_file_remove_key ()
gboolean g_key_file_remove_comment ()

类型和常值

  GKeyFile
#define G_KEY_FILE_ERROR
enum GKeyFileError
enum GKeyFileFlags
#define G_KEY_FILE_DESKTOP_GROUP
#define G_KEY_FILE_DESKTOP_KEY_TYPE
#define G_KEY_FILE_DESKTOP_KEY_VERSION
#define G_KEY_FILE_DESKTOP_KEY_NAME
#define G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME
#define G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY
#define G_KEY_FILE_DESKTOP_KEY_COMMENT
#define G_KEY_FILE_DESKTOP_KEY_ICON
#define G_KEY_FILE_DESKTOP_KEY_HIDDEN
#define G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN
#define G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN
#define G_KEY_FILE_DESKTOP_KEY_TRY_EXEC
#define G_KEY_FILE_DESKTOP_KEY_EXEC
#define G_KEY_FILE_DESKTOP_KEY_PATH
#define G_KEY_FILE_DESKTOP_KEY_TERMINAL
#define G_KEY_FILE_DESKTOP_KEY_MIME_TYPE
#define G_KEY_FILE_DESKTOP_KEY_CATEGORIES
#define G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY
#define G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS
#define G_KEY_FILE_DESKTOP_KEY_URL
#define G_KEY_FILE_DESKTOP_KEY_ACTIONS
#define G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE
#define G_KEY_FILE_DESKTOP_TYPE_APPLICATION
#define G_KEY_FILE_DESKTOP_TYPE_LINK
#define G_KEY_FILE_DESKTOP_TYPE_DIRECTORY

使用时需添加引用

#include <glib.h>

描述

使用GKeyFile可以解析、编辑或者创建包含使用分组形式的键值对格式的文件。姑且称这种文件为“键值”文件。freedesktop.org中的有些配置文件使用了这种格式,如Desktop
Entry Specification
Icon 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[[email protected]]=Hello

[Another Group]

Numbers=2;20;-200;0

Booleans=true;false;true;true

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

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

键值对通常的形式为键=值。 但对于本地化字符串,其表示形式为键[locale]=值,其中[email protected]中,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

 

Returns

如果文件可加载返回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);

Writes the contents of key_file to
filename
usingg_file_set_contents().

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

Parameters


key_file


a GKeyFile

 

filename


the name of the file to write to

 

error


a pointer to a NULL GError, or NULL

 

Returns

TRUE if successful, elseFALSE
with errorset

Since: 2.40


g_key_file_get_start_group ()

gchar *
g_key_file_get_start_group (GKeyFile *key_file);

Returns the name of the start group of the file.

Parameters


key_file


a GKeyFile

 

Returns

The start group of the key file.

Since: 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

时间: 2024-10-11 10:06:35

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

zabbix-2.0.8利用组和键值快速添加筛选(api操作)

利用zabbix api快速添加筛选 功能描述: 该脚本可以利用组名和组中host中含有的key快速生成筛选 使用前提: 需要对key设置图形 适用版本: zabbix 2.0.8(实际测试),理论支持2.0~2.4系列(未验证) 操作注意: 需要将url,user,passwd,组名称,已经创建图形的key,筛选名称填入脚本,python运行即可 #!/usr/bin/env python # coding:utf-8 ''' ###################### # Function

Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作

有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn.xm.exam.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java

实现键值对存储(三):Kyoto Cabinet 和LevelDB的架构比较分析

译自  Emmanuel Goossaert (CodeCapsule.com) 在本文中,我将会逐组件地把Kyoto Cabinet 和 LevelDB的架构过一遍.目标和本系列第二部分讲的差不多,通过分析现有键值对存储的架构来思考我应该如何建立我自己键值对存储的架构.本文将包括: 1. 本架构分析的意图和方法 2. 键值对存储组件概览 3. Kyoto Cabinet 和LevelDB在结构和概念上的分析 3.1 用Doxygen建立代码地图 3.2 整体架构 3.3 接口 3.4 参数化

5.1 保存键值集

如果您有想要保存的相对较小键值集合,您应使用 SharedPreferences API.SharedPreferences 对象指向包含键值对的文件并提供读写这些文件的简单方法. 每个 SharedPreferences 文件由框架进行管理并且可以专用或共享. 本课向您展示如何使用 SharedPreferences API 存储和检索简单的值. 注:SharedPreferences API 仅用于读写键值对,您不得将其与 Preference API 混淆,后者帮助您为您的应用设置构建用户

KVC - 键值编码

[基本概念] 1.键值编码是一个用于间接访问对象属性的机制,使用该机制不需要调用存取方法和变量实例就可访问对象属性. 2.键值编码方法在OC非正式协议(类目)NSKeyValueCoding中被声明,默认的实现方法由NSObject提供. 3.键值编码支持带有对象值的属性,同时也支持纯数值类型和结构.非对象参数和返回类型会被识别并自动封装/解封. [键值访问] 键值编码中的基本调用包括-valueForKey: 和 -setValue:forkey: 这两个方法,它们以字符串的形式向对象发送消息

安卓训练-开始-保存数据-保存键值对集合

保存键值对集合 上一课 下一课 这节课教你 取得一个 SharedPreferences 的句柄(Handle) 写入共享首选项 读取共享首选项 你还需要阅读 使用共享首选项 如果你有一个比较小的键值对集合需要保存,你应该使用 SharedPreferences API.一个SharedPreferences 对象指向一个包含键值对的文件并提供简单的方法读写键值对.每个SharedPreferences 文件都由框架管理,它可以是私有的或共享的. 这节课教你怎样使用 SharedPreferen

Android学习路线(二十七)键值对(SharedPreferences)存储

假设你又一个相对较小的键值对数据想要保存,你应该使用SharedPreferences APIs.一个SharedPreferences 对象指向一个包括键值对的文件,它提供简单的方法来读写他们.每一个SharedPreferences 文件系统框架管理,它们能够是私有的也能够被共享. 本课将介绍怎样使用SharedPreferences APIs来存储和获取简单的数据. 提示: SharedPreferences APIs 仅仅能被用来操作键值对类型数据,不要把它和 Preference AP

[深入浅出Cocoa]详解键值观察(KVO)及其实现机理

一,前言 Objective-C 中的键(key)-值(value)观察(KVO)并不是什么新鲜事物,它来源于设计模式中的观察者模式,其基本思想就是: 一个目标对象管理所有依赖于它的观察者对象,并在它自身的状态改变时主动通知观察者对象.这个主动通知通常是通过调用各观察者对象所提供的接口方法来实现的.观察者模式较完美地将目标对象与观察者对象解耦. 在 Objective-C 中有两种使用键值观察的方式:手动或自动,此外还支持注册依赖键(即一个键依赖于其他键,其他键的变化也会作用到该键).下面将一一

redis插入单个较大的键值

1.前言: 在linux的命令行界面或者是进入到redis数据库中,在插入较大的键值时,由于命令行界面对于字符个数的限制,都不能完全将redis的键值粘贴上去,这个时通过shell脚本比较容易实现 2.涉及的文件 redis.sh  #执行插入键值的脚本 redis.txt  #存放键值数据的文件 3.注意 在复制redis键值数据到redis.txt文件中的时候注意空格 4.执行插入脚本redis.sh #!/bin/bash #name: redis.sh #Author: lipc #Da