1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Saving Configuration Information
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="Gnome Application Basics" href=
"cha-startup.html">
<link rel="PREVIOUS" title="Argument Parsing with popt" href=
"z77.html">
<link rel="NEXT" title="Session Management" href=
"sec-sessionmanagement.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<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>
</table>
</div>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z79">Saving Configuration Information</a>
</h1>
<p>
<tt class="APPLICATION">libgnome</tt> comes with the
ability to store simple key-value pairs in plain text
configuration files. Convenience routines are provided for
numeric and boolean types which transparently convert to
and from a text representation of each type. The standard
location for Gnome configuration files is <tt class=
"FILENAME">~/.gnome</tt>, and the library will use that
location by default. However, the library can be used with
any file. There are also variants of each function which
save to <tt class="FILENAME">~/.gnome_private</tt>, a
directory with user permissions only. The basic functions
to store and retrieve data are listed in <a href=
"z79.html#STARTUP-GNOMECONFIGGET">Figure 4 in the section
called <i>Reading Stored Config Data</i></a> and <a href=
"z79.html#STARTUP-GNOMECONFIGSET">Figure 5 in the section
called <i>Storing Data In Configuration Files</i></a>. This
module of <tt class="APPLICATION">libgnome</tt> is often
referred to as <tt class="APPLICATION">gnome-config</tt>.
Don't confuse this usage of "gnome-config" with the <tt
class="APPLICATION">gnome-config</tt> script that reports
the compile and link flags for Gnome programs.
</p>
<p>
The <tt class="APPLICATION">gnome-config</tt> functions
work with a <i class="FIRSTTERM">path</i>. A path has three
components:
</p>
<ul>
<li>
<p>
The <i class="FIRSTTERM">filename</i> to use,
underneath the <tt class="FILENAME">~/.gnome</tt> or
<tt class="FILENAME">~/.gnome_private</tt> directory.
By convention this is the name of your application.
</p>
</li>
<li>
<p>
A <i class="FIRSTTERM">section</i>---a logical
subcategory of related configuration information.
</p>
</li>
<li>
<p>
A <i class="FIRSTTERM">key</i>---the key half of a
key-value pair. The key is actually associated with a
piece of configuration data.
</p>
</li>
</ul>
<p>
A path is passed to Gnome as a string, with the form <tt
class="APPLICATION">"/filename/section/key"</tt>. If you
want to use a filename which is <i class="EMPHASIS">not</i>
in the standard Gnome directories, you can bracket the
entire path with the <tt class="APPLICATION">'='</tt>
character and it will be interpreted as absolute. You can
even use this as a simple datafile format (it is used for
the <tt class="APPLICATION">.desktop</tt> files programs
install in order to appear on the Gnome panel menu).
However, XML (perhaps using the <tt class="APPLICATION">
gnome-xml</tt> package) is almost certainly a better choice
for that. XML may also be a better choice for storing some
kinds of configuration information; the primary advantage
of the <tt class="APPLICATION">libgnome</tt> configuration
library is its simplicity.
</p>
<p>
<tt class="APPLICATION">gnome-config</tt> has a long
history; it was first written for the WINE Windows emulator
project, then used in the GNU Midnight Commander file
manager, and finally migrated into the Gnome libraries. The
plan is to replace <tt class="APPLICATION">
gnome-config</tt> with something more powerful in the next
version of Gnome; we want to support per-host
configuration, backends such as LDAP, and other features.
However, the <tt class="APPLICATION">gnome-config</tt> API
will almost certainly be supported even if the underlying
engine changes dramatically.
</p>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z80">Reading Stored Config Data</a>
</h2>
<p>
Retrieving data from files is simple. You simply call a
function to retrieve the value for a given key. The
value-retrieving functions (shown in <a href=
"z79.html#STARTUP-GNOMECONFIGGET">Figure 4</a>) accept a
path as their argument. For example, you might ask
whether the user wants to see a dialog box:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gboolean show_dialog;
show_dialog =
gnome_config_get_bool("/myapp/General/dialog");
</pre>
</td>
</tr>
</table>
<p>
If the config file doesn't exist yet, or there is no key
matching the path you provide, these functions return 0,
<span class="STRUCTNAME">FALSE</span>, or <span class=
"STRUCTNAME">NULL</span>. The functions that return a
string return allocated memory; you should <tt class=
"FUNCTION">g_free()</tt> the returned string. The string
vector functions return an allocated vector full of
allocated strings (<tt class="FUNCTION">g_strfreev()</tt>
is the easiest way to free this vector).
</p>
<p>
You can specify a default value to be returned if the key
does not exist; to do so, append an <span class=
"STRUCTNAME">"=value"</span> to the path. For example:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gboolean show_dialog;
show_dialog =
gnome_config_get_bool("/myapp/General/dialog=true");
</pre>
</td>
</tr>
</table>
<p>
Each function has a <span class="STRUCTNAME">
with_default</span> variant; these tell you whether the
return value was taken from a config file or from the
default you specified. For example:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gboolean show_dialog;
gboolean used_default;
show_dialog =
gnome_config_get_bool_with_default("/myapp/General/dialog=true",
&used_default);
if (used_default)
printf("Default value used for show_dialog\n");
</pre>
</td>
</tr>
</table>
<p>
<tt class="FUNCTION">gnome_config_push_prefix()</tt> and
<tt class="FUNCTION">gnome_config_pop_prefix()</tt> (in
<a href="z79.html#STARTUP-GNOMECONFIGMISC">Figure 7 in
the section called <i>Other Config File
Operations</i></a>) can be used to avoid specifying the
entire path each time. For example:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gboolean show_dialog;
gnome_config_push_prefix("/myapp/General/");
show_dialog =
gnome_config_get_bool("dialog=true");
gnome_config_pop_prefix();
</pre>
</td>
</tr>
</table>
<p>
These functions also work when saving values.
</p>
<p>
The configuration functions with <span class=
"STRUCTNAME">private</span> in their name use a <tt
class="FILENAME">.gnome_private</tt> directory with
restricted permissions, as discussed above. The <span
class="STRUCTNAME">translated_string</span> functions
qualify the provided key with the name of the current
locale; these are used when Gnome reads <tt class=
"APPLICATION">.desktop</tt> files (see <a href=
"z72.html#SEC-.DESKTOP">the section called <i><tt class=
"APPLICATION">.desktop</tt> Entries</i> in the chapter
called <i>Creating Your Source Tree</i></a>) and are
probably not useful to applications.
</p>
<div class="FIGURE">
<a name="STARTUP-GNOMECONFIGGET"></a>
<div class="FUNCSYNOPSIS">
<a name="STARTUP-GNOMECONFIGGET.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">gchar* <tt class=
"FUNCTION">gnome_config_get_string</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_translated_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>
path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">gnome_config_get_int</tt></code>(const
gchar* <tt class="PARAMETER"><i>
path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gdouble <tt class=
"FUNCTION">gnome_config_get_float</tt></code>(const
gchar* <tt class="PARAMETER"><i>
path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gboolean <tt class=
"FUNCTION">gnome_config_get_bool</tt></code>(const
gchar* <tt class="PARAMETER"><i>
path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gnome_config_get_vector</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*
<tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt
class="PARAMETER"><i>argvp</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">
gnome_config_private_get_string</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_translated_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>
path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">
gnome_config_private_get_int</tt></code>(const gchar*
<tt class="PARAMETER"><i>path</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gdouble <tt class=
"FUNCTION">
gnome_config_private_get_float</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_get_bool</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_get_vector</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*
<tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt
class="PARAMETER"><i>argvp</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">
gnome_config_get_string_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">
gnome_config_get_translated_string_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">
gnome_config_get_int_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gdouble <tt class=
"FUNCTION">
gnome_config_get_float_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gboolean <tt class=
"FUNCTION">
gnome_config_get_bool_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_get_vector_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*
<tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt
class="PARAMETER"><i>argvp</i></tt>, gboolean* <tt
class="PARAMETER"><i>was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">
gnome_config_private_get_string_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gchar* <tt class=
"FUNCTION">
gnome_config_private_get_translated_string_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gint <tt class=
"FUNCTION">
gnome_config_private_get_int_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gdouble <tt class=
"FUNCTION">
gnome_config_private_get_float_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">gboolean <tt class=
"FUNCTION">
gnome_config_private_get_bool_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean* <tt class="PARAMETER"><i>
was_default</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_get_vector_with_default</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*
<tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt
class="PARAMETER"><i>argvp</i></tt>, gboolean* <tt
class="PARAMETER"><i>was_default</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 4. Retrieving data from configuration
files</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z81">Storing Data In Configuration Files</a>
</h2>
<p>
Saving data is simply the inverse of loading it; you
provide a <span class="STRUCTNAME">
"/file/section/key"</span> path in just the same way,
along with the value to store. Data is not written
immediately; you must call <tt class="FUNCTION">
gnome_config_sync()</tt> to ensure the file is written to
disk.
</p>
<div class="FIGURE">
<a name="STARTUP-GNOMECONFIGSET"></a>
<div class="FUNCSYNOPSIS">
<a name="STARTUP-GNOMECONFIGSET.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_set_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, const
gchar* <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_set_translated_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, const
gchar* <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gnome_config_set_int</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint
<tt class="PARAMETER"><i>value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gnome_config_set_float</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gdouble <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gnome_config_set_bool</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">gnome_config_set_vector</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint
<tt class="PARAMETER"><i>argc</i></tt>, const gchar*
const <tt class="PARAMETER"><i>
argv[]</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, const
gchar* <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_translated_string</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, const
gchar* <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_int</tt></code>(const gchar*
<tt class="PARAMETER"><i>path</i></tt>, gint <tt
class="PARAMETER"><i>value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_float</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gdouble <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_bool</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>,
gboolean <tt class="PARAMETER"><i>
value</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
gnome_config_private_set_vector</tt></code>(const
gchar* <tt class="PARAMETER"><i>path</i></tt>, gint
<tt class="PARAMETER"><i>argc</i></tt>, const gchar*
const <tt class="PARAMETER"><i>
argv[]</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 5. Saving data to configuration files</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z82">Config File Iterators</a>
</h2>
<p>
Iterators are used to scan the sections in a given file,
or the keys in a given section. Applications can use this
feature to store lists of data, by dynamically generating
key or section names to save and later iterating over
them to discover what was saved. The functions are
summarized in <a href=
"z79.html#STARTUP-GNOMECONFIGITERATORS">Figure 6 in the
section called <i>Section Iterators</i></a>.
</p>
<p>
An iterator is an opaque data type; you pass <tt class=
"FUNCTION">gnome_config_init_iterator()</tt> the name of
a section to iterate over and receive an iterator in
return. You then call <tt class="FUNCTION">
gnome_config_iterator_next()</tt> to obtain key-value
pairs from the section. The key and value returned from
<tt class="FUNCTION">gnome_config_iterator_next()</tt>
must be freed with <tt class="FUNCTION">g_free()</tt>,
and the return value of <tt class="FUNCTION">
gnome_config_iterator_next()</tt> is a pointer to the
next iterator. When <tt class="FUNCTION">
gnome_config_iterator_next()</tt> returns <span class=
"STRUCTNAME">NULL</span>, all key-value pairs have been
traversed.
</p>
<div class="SECT3">
<h3 class="SECT3">
<a name="Z83">Iteration Example from <tt class=
"APPLICATION">gnome-apt</tt></a>
</h3>
<p>
Here's an example from <tt class="APPLICATION">
gnome-apt</tt>, a C++ application used to manage
packages on Debian systems. <tt class="APPLICATION">
gnome-apt</tt> loads and saves the position of some
columns in a tree display. The columns are identified
by the <span class="STRUCTNAME">
GAptPkgTree::ColumnType</span> enumeration. <span
class="STRUCTNAME">GAptPkgTree::ColumnTypeEnd</span> is
the last element in the column type enumeration, and
equal to the number of valid column types. This example
is frighteningly "real world" and checks for a number
of error conditions.
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
load_column_order(vector<GAptPkgTree::ColumnType> & columns)
{
gpointer config_iterator;
guint loaded = 0;
config_iterator = gnome_config_init_iterator("/gnome-apt/ColumnOrder");
if (config_iterator != 0)
{
gchar * col, * pos;
columns.reserve(GAptPkgTree::ColumnTypeEnd);
loaded = 0;
while ((config_iterator =
gnome_config_iterator_next(config_iterator,
&col, &pos)))
{
// shouldn't happen, but I'm paranoid
if (pos == 0 || col == 0)
{
if (pos) g_free(pos);
if (col) g_free(col);
continue;
}
GAptPkgTree::ColumnType ct = string_to_column(col);
gint index = atoi(pos);
g_free(pos); pos = 0;
g_free(col); col = 0;
// the user could mangle the config file to make this happen
if (static_cast<guint>(index) >= columns.size())
continue;
columns[index] = ct;
++loaded;
}
}
if (loaded != static_cast<guint>(GAptPkgTree::ColumnTypeEnd))
{
// Either there was no saved order, or something is busted - use
// default order
columns.clear();
int i = 0;
while (i < GAptPkgTree::ColumnTypeEnd)
{
columns.push_back(static_cast<GAptPkgTree::ColumnType>(i));
++i;
}
// Clean the section - otherwise an old entry could
// remain forever and keep screwing us up in the future.
gnome_config_clean_section("/gnome-apt/ColumnOrder");
gnome_config_sync();
}
g_return_if_fail(columns.size() ==
static_cast<guint>(GAptPkgTree::ColumnTypeEnd));
}
</pre>
</td>
</tr>
</table>
<p>
It might be helpful to see the function that saves the
column positions:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
static void
save_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>
|