?? z79.html
字號:
<tr> <td><pre class="PROGRAMLISTING"> static voidsave_column_order(const vector<GAptPkgTree::ColumnType> & columns){ g_return_if_fail(columns.size() == static_cast<guint>(GAptPkgTree::ColumnTypeEnd)); int position = 0; vector<GAptPkgTree::ColumnType>::const_iterator i = columns.begin(); while (i != columns.end()) { gchar key[256]; g_snprintf(key, 255, "/gnome-apt/ColumnOrder/%s", column_to_string(*i)); gchar val[30]; g_snprintf(val, 29, "%d", position); gnome_config_set_string(key, val); ++position; ++i; } gnome_config_sync();} </pre> </td> </tr> </table> <p> When writing this code, the decision was made to store enumeration values as strings rather than integers. The <tt class="FUNCTION">column_to_string()</tt> and <tt class="FUNCTION">string_to_column()</tt> functions use a simple array of column names indexed by the enumeration values to convert back and forth. There are two reasons to do this: it will not break when the enumeration is altered in future versions of the program, and it keeps the configuration file human-editable. </p> <p> You may also notice that the column positions are stored with <tt class="FUNCTION"> gnome_config_set_string()</tt> instead of <tt class= "FUNCTION">gnome_config_set_int()</tt>. This is because <tt class="FUNCTION">gnome_config_iterator_next()</tt> returns a string representation of the stored information, as found in the file. Most likely, <tt class="FUNCTION">gnome_config_set_int()</tt> stores integers as strings <tt class="FUNCTION">atoi()</tt> would understand (in fact it does), but it is technically not guaranteed by the API. If the code used <tt class="FUNCTION">gnome_config_set_int()</tt>, it would have to obtain only the key from <tt class= "FUNCTION">gnome_config_iterator_next()</tt> and then call <tt class="FUNCTION">gnome_config_get_int()</tt> to obtain the integer value. Using <tt class= "FUNCTION">atoi()</tt> on the string value would make unwarranted assumptions about <tt class="APPLICATION"> gnome-config</tt>'s implementation. </p> </div> <div class="SECT3"> <h3 class="SECT3"> <a name="Z84">Section Iterators</a> </h3> <p> <tt class="FUNCTION"> gnome_config_init_iterator_sections()</tt> allows you to iterate over the sections in a file, rather than over the keys in a section. When iterating over sections, <tt class="FUNCTION"> gnome_config_iterator_next()</tt> ignores its <span class="STRUCTNAME">value</span> argument and places the section name in the <span class="STRUCTNAME">key</span> argument. </p> <div class="FIGURE"> <a name="STARTUP-GNOMECONFIGITERATORS"></a> <div class="FUNCSYNOPSIS"> <a name="STARTUP-GNOMECONFIGITERATORS.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <libgnome/gnome-config.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">void* <tt class= "FUNCTION"> gnome_config_init_iterator</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void* <tt class= "FUNCTION"> gnome_config_private_init_iterator</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void* <tt class= "FUNCTION"> gnome_config_init_iterator_sections</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void* <tt class= "FUNCTION"> gnome_config_private_init_iterator_sections</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void* <tt class= "FUNCTION"> gnome_config_iterator_next</tt></code>(void* <tt class="PARAMETER"><i>iterator_handle</i></tt>, gchar** <tt class="PARAMETER"><i>key</i></tt>, gchar** <tt class="PARAMETER"><i> value</i></tt>);</code> </p> </div> <p> <b>Figure 6. Configuration file iterators</b> </p> </div> </div> <div class="SECT3"> <h3 class="SECT3"> <a name="Z85">Other Config File Operations</a> </h3> <p> <a href="z79.html#STARTUP-GNOMECONFIGMISC">Figure 7</a> lists some additional operations available for manipulating config files. The most important of these have already been mentioned in passing. <tt class= "FUNCTION">gnome_config_sync()</tt> writes the configuration file to disk, and <tt class="FUNCTION"> gnome_config_push_prefix()</tt> allows you to shorten the path passed to the other <tt class="APPLICATION"> gnome-config</tt> functions. There are also boolean tests, to ask <tt class="APPLICATION">gnome-config</tt> whether a given section exists. </p> <p> Two new operations are introduced: to <i class= "FIRSTTERM">drop</i> a file or section means to forget any information about it stored in memory, including cached values loaded from the file and values not yet saved to the file with <tt class="FUNCTION"> gnome_config_sync()</tt>. To <i class="FIRSTTERM"> clean</i> a file, section, or key means to unset its value(s), so the file, section, or key will not exist once <tt class="FUNCTION">gnome_config_sync()</tt> is called. </p> <p> <tt class="FUNCTION">gnome_config_sync()</tt> automatically calls <tt class="FUNCTION"> gnome_config_drop_all()</tt> to free all <tt class= "APPLICATION">gnome-config</tt> resources, since the information is safely stored on disk. </p> <p> Functions are also provided to get the "real" (filesystem) path of a configuration file from a <tt class="APPLICATION">gnome-config</tt> path. These are unlikely to be useful in application code. </p> <div class="FIGURE"> <a name="STARTUP-GNOMECONFIGMISC"></a> <div class="FUNCSYNOPSIS"> <a name="STARTUP-GNOMECONFIGMISC.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <libgnome/gnome-config.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gboolean <tt class= "FUNCTION"> gnome_config_has_section</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gboolean <tt class= "FUNCTION"> gnome_config_private_has_section</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_drop_all</tt></code>(void);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_sync</tt></code>(void);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">gnome_config_sync_file</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_private_sync_file</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">gnome_config_drop_file</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_private_drop_file</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_clean_file</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_private_clean_file</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_clean_section</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_private_clean_section</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">gnome_config_clean_key</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_private_clean_key</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION"> gnome_config_get_real_path</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION"> gnome_config_private_get_real_path</tt></code>(const gchar* <tt class="PARAMETER"><i> path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_push_prefix</tt></code>(const gchar* <tt class="PARAMETER"><i>path</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION"> gnome_config_pop_prefix</tt></code>(void);</code> </p> </div> <p> <b>Figure 7. Miscellaneous configuration file functions</b> </p> </div> </div> </div> </div> <div class="NAVFOOTER"> <br> <br> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z77.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="sec-sessionmanagement.html"><font color= "#0000ff" size="2"><b>Next >>></b></font></a> </td> </tr> <tr> <td colspan="2" align="left"> <font color="#000000" size="2"><b>Argument Parsing with <tt class="APPLICATION">popt</tt></b></font> </td> <td colspan="2" align="right"> <font color="#000000" size="2"><b>Session Management</b></font> </td> </tr> </table> </div> </body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -