📄 glib-key-value-file-parser.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Key-value file parser</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-Simple-XML-Subset-Parser.html" title="Simple XML Subset Parser"><link rel="next" href="glib-Windows-Compatability-Functions.html" title="Windows Compatibility Functions"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Simple-XML-Subset-Parser.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Windows-Compatability-Functions.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Key-value-file-parser"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Key-value file parser</span></h2><p>Key-value file parser — parses <code class="filename">.ini</code>-like config files</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a>;#define <a href="glib-Key-value-file-parser.html#G-KEY-FILE-ERROR:CAPS">G_KEY_FILE_ERROR</a>enum <a href="glib-Key-value-file-parser.html#GKeyFileError">GKeyFileError</a>;enum <a href="glib-Key-value-file-parser.html#GKeyFileFlags">GKeyFileFlags</a>;<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a>* <a href="glib-Key-value-file-parser.html#g-key-file-new">g_key_file_new</a> (void);void <a href="glib-Key-value-file-parser.html#g-key-file-free">g_key_file_free</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file);void <a href="glib-Key-value-file-parser.html#g-key-file-set-list-separator">g_key_file_set_list_separator</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, <a href="glib-Basic-Types.html#gchar">gchar</a> separator);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-load-from-file">g_key_file_load_from_file</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *file, <a href="glib-Key-value-file-parser.html#GKeyFileFlags">GKeyFileFlags</a> flags, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-load-from-data">g_key_file_load_from_data</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *data, <a href="glib-Basic-Types.html#gsize">gsize</a> length, <a href="glib-Key-value-file-parser.html#GKeyFileFlags">GKeyFileFlags</a> flags, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-load-from-data-dirs">g_key_file_load_from_data_dirs</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *file, <a href="glib-Basic-Types.html#gchar">gchar</a> **full_path, <a href="glib-Key-value-file-parser.html#GKeyFileFlags">GKeyFileFlags</a> flags, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-to-data">g_key_file_to_data</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-start-group">g_key_file_get_start_group</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file);<a href="glib-Basic-Types.html#gchar">gchar</a>** <a href="glib-Key-value-file-parser.html#g-key-file-get-groups">g_key_file_get_groups</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, <a href="glib-Basic-Types.html#gsize">gsize</a> *length);<a href="glib-Basic-Types.html#gchar">gchar</a>** <a href="glib-Key-value-file-parser.html#g-key-file-get-keys">g_key_file_get_keys</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-has-group">g_key_file_has_group</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-has-key">g_key_file_has_key</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-value">g_key_file_get_value</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-string">g_key_file_get_string</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-locale-string">g_key_file_get_locale_string</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *locale, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Key-value-file-parser.html#g-key-file-get-boolean">g_key_file_get_boolean</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-Key-value-file-parser.html#g-key-file-get-integer">g_key_file_get_integer</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>** <a href="glib-Key-value-file-parser.html#g-key-file-get-string-list">g_key_file_get_string_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>** <a href="glib-Key-value-file-parser.html#g-key-file-get-locale-string-list">g_key_file_get_locale_string_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *locale, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-boolean-list">g_key_file_get_boolean_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gint">gint</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-integer-list">g_key_file_get_integer_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Key-value-file-parser.html#g-key-file-get-comment">g_key_file_get_comment</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);void <a href="glib-Key-value-file-parser.html#g-key-file-set-value">g_key_file_set_value</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *value);void <a href="glib-Key-value-file-parser.html#g-key-file-set-string">g_key_file_set_string</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);void <a href="glib-Key-value-file-parser.html#g-key-file-set-locale-string">g_key_file_set_locale_string</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *locale, const <a href="glib-Basic-Types.html#gchar">gchar</a> *string);void <a href="glib-Key-value-file-parser.html#g-key-file-set-boolean">g_key_file_set_boolean</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gboolean">gboolean</a> value);void <a href="glib-Key-value-file-parser.html#g-key-file-set-integer">g_key_file_set_integer</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gint">gint</a> value);void <a href="glib-Key-value-file-parser.html#g-key-file-set-string-list">g_key_file_set_string_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *const list[], <a href="glib-Basic-Types.html#gsize">gsize</a> length);void <a href="glib-Key-value-file-parser.html#g-key-file-set-locale-string-list">g_key_file_set_locale_string_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *locale, const <a href="glib-Basic-Types.html#gchar">gchar</a> *const list[], <a href="glib-Basic-Types.html#gsize">gsize</a> length);void <a href="glib-Key-value-file-parser.html#g-key-file-set-boolean-list">g_key_file_set_boolean_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gboolean">gboolean</a> list[], <a href="glib-Basic-Types.html#gsize">gsize</a> length);void <a href="glib-Key-value-file-parser.html#g-key-file-set-integer-list">g_key_file_set_integer_list</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Basic-Types.html#gint">gint</a> list[], <a href="glib-Basic-Types.html#gsize">gsize</a> length);void <a href="glib-Key-value-file-parser.html#g-key-file-set-comment">g_key_file_set_comment</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, const <a href="glib-Basic-Types.html#gchar">gchar</a> *comment, <a href="glib-Error-Reporting.html#GError">GError</a> **error);void <a href="glib-Key-value-file-parser.html#g-key-file-remove-group">g_key_file_remove_group</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, <a href="glib-Error-Reporting.html#GError">GError</a> **error);void <a href="glib-Key-value-file-parser.html#g-key-file-remove-key">g_key_file_remove_key</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);void <a href="glib-Key-value-file-parser.html#g-key-file-remove-comment">g_key_file_remove_comment</a> (<a href="glib-Key-value-file-parser.html#GKeyFile">GKeyFile</a> *key_file, const <a href="glib-Basic-Types.html#gchar">gchar</a> *group_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *key, <a href="glib-Error-Reporting.html#GError">GError</a> **error);</pre></div><div class="refsect1" lang="en"><a name="id3081860"></a><h2>Description</h2><p><a href="glib-Key-value-file-parser.html#GKeyFile"><span class="type">GKeyFile</span></a> lets you parse, edit or create files containing groups ofkey-value pairs, which we call <em class="firstterm">key files</em> for lack of a better name. Several freedesktop.org specifications usekey files now, e.g the <a href="http://freedesktop.org/Standards/desktop-entry-spec" target="_top">DesktopEntry Specification</a> and the<a href="http://freedesktop.org/Standards/icon-theme-spec" target="_top">IconTheme Specification</a>. </p><p>The syntax of key files is described in detail in the <a href="http://freedesktop.org/Standards/desktop-entry-spec" target="_top">DesktopEntry Specification</a>, here is a quick summary: Key filesconsists of groups of key-value pairs, interspersed with comments.</p><div class="informalexample"><pre class="programlisting"># 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 pairsWelcome=HelloWelcome[de]=HalloWelcome[fr]=BonjourWelcome[it]=Ciao[Another Group]Numbers=2;20;-200;0Booleans=true;false;true;true</pre></div><p>Lines beginning with a '#' and blank lines are considered comments.</p><p>Groups are started by a header line containing the group name enclosedin '[' and ']', and ended implicitly by the start of the next group orthe end of the file. Each key-value pair must be contained in a group.</p><p>Key-value pairs generally have the form <code class="literal">key=value</code>, with the exception of localized strings, which have the form <code class="literal">key[locale]=value</code>. Space before and after the '=' character are ignored. Newline, tab, carriage return and backslash characters are escaped as \n, \t, \r, and \\, respectively. To preserveinitial and final spaces, these can also be escaped as \s.</p><p>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 ina list, it has to be escaped by prefixing it with a backslash.</p><p>This syntax is obviously inspired by the <code class="filename">.ini</code> files commonly met on Windows, but there are some important differences:</p><div class="itemizedlist"><ul type="disc">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -