-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathlibconfig_manual.html
3835 lines (3569 loc) · 245 KB
/
libconfig_manual.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>libconfig</title>
<meta name="description" content="libconfig">
<meta name="keywords" content="libconfig">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="#Top" rel="start" title="Top">
<link href="#Function-Index" rel="index" title="Function Index">
<link href="#SEC_Contents" rel="contents" title="Table of Contents">
<link href="dir.html#Top" rel="up" title="(dir)">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<h1 class="settitle" align="center">libconfig</h1>
<hr noshade size=6 color="black">
<div align=right>A Library For Processing Structured Configuration Files<br>
Version 1.7.3<br>
20 Jun 2021</div>
<br><br><br><br>
<font size=+1>Mark A. Lindner</font>
<hr size=3 noshade color="black">
<br><br>
<a name="SEC_Contents"></a>
<h2 class="contents-heading">Table of Contents</h2>
<div class="contents">
<ul class="no-bullet">
<li><a name="toc-Introduction-1" href="#Introduction">1 Introduction</a>
<ul class="no-bullet">
<li><a name="toc-Why-Another-Configuration-File-Library_003f-1" href="#Why-Another-Configuration-File-Library_003f">1.1 Why Another Configuration File Library?</a></li>
<li><a name="toc-Using-the-Library-from-a-C-Program-1" href="#Using-the-Library-from-a-C-Program">1.2 Using the Library from a C Program</a></li>
<li><a name="toc-Using-the-Library-from-a-C_002b_002b-Program-1" href="#Using-the-Library-from-a-C_002b_002b-Program">1.3 Using the Library from a C++ Program</a></li>
<li><a name="toc-Multithreading-Issues-1" href="#Multithreading-Issues">1.4 Multithreading Issues</a></li>
<li><a name="toc-Internationalization-Issues-1" href="#Internationalization-Issues">1.5 Internationalization Issues</a></li>
<li><a name="toc-Compiling-Using-pkg_002dconfig-1" href="#Compiling-Using-pkg_002dconfig">1.6 Compiling Using pkg-config</a></li>
<li><a name="toc-Version-Test-Macros-1" href="#Version-Test-Macros">1.7 Version Test Macros</a></li>
</ul></li>
<li><a name="toc-Configuration-Files-1" href="#Configuration-Files">2 Configuration Files</a>
<ul class="no-bullet">
<li><a name="toc-Settings-1" href="#Settings">2.1 Settings</a></li>
<li><a name="toc-Groups-1" href="#Groups">2.2 Groups</a></li>
<li><a name="toc-Arrays-1" href="#Arrays">2.3 Arrays</a></li>
<li><a name="toc-Lists-1" href="#Lists">2.4 Lists</a></li>
<li><a name="toc-Integer-Values-1" href="#Integer-Values">2.5 Integer Values</a></li>
<li><a name="toc-64_002dbit-Integer-Values-1" href="#g_t64_002dbit-Integer-Values">2.6 64-bit Integer Values</a></li>
<li><a name="toc-Floating-Point-Values-1" href="#Floating-Point-Values">2.7 Floating Point Values</a></li>
<li><a name="toc-Boolean-Values-1" href="#Boolean-Values">2.8 Boolean Values</a></li>
<li><a name="toc-String-Values-1" href="#String-Values">2.9 String Values</a></li>
<li><a name="toc-Comments-1" href="#Comments">2.10 Comments</a></li>
<li><a name="toc-Include-Directives-1" href="#Include-Directives">2.11 Include Directives</a></li>
</ul></li>
<li><a name="toc-The-C-API-1" href="#The-C-API">3 The C API</a></li>
<li><a name="toc-The-C_002b_002b-API-1" href="#The-C_002b_002b-API">4 The C++ API</a></li>
<li><a name="toc-Example-Programs-1" href="#Example-Programs">5 Example Programs</a></li>
<li><a name="toc-Other-Bindings-and-Implementations-1" href="#Other-Bindings-and-Implementations">6 Other Bindings and Implementations</a>
<ul class="no-bullet">
<li><a name="toc-Bourne-Shell-1" href="#Bourne-Shell">6.1 Bourne Shell</a></li>
<li><a name="toc-D-1" href="#D">6.2 D</a></li>
<li><a name="toc-Haskell-1" href="#Haskell">6.3 Haskell</a></li>
<li><a name="toc-Java-1" href="#Java">6.4 Java</a></li>
<li><a name="toc-Lisp-1" href="#Lisp">6.5 Lisp</a></li>
<li><a name="toc-Perl-1" href="#Perl">6.6 Perl</a></li>
<li><a name="toc-Python-1" href="#Python">6.7 Python</a></li>
<li><a name="toc-Ruby-1" href="#Ruby">6.8 Ruby</a></li>
</ul></li>
<li><a name="toc-License-1" href="#License">Appendix A License</a></li>
<li><a name="toc-Configuration-File-Grammar-1" href="#Configuration-File-Grammar">Appendix B Configuration File Grammar</a></li>
<li><a name="toc-Function-Index-1" href="#Function-Index">Function Index</a></li>
<li><a name="toc-Type-Index-1" href="#Type-Index">Type Index</a></li>
<li><a name="toc-Concept-Index-1" href="#Concept-Index">Concept Index</a></li>
</ul>
</div>
<a name="Top"></a>
<div class="header">
<p>
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a>, Up: <a href="dir.html#Top" accesskey="u" rel="up">(dir)</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="libconfig"></a>
<h1 class="top">libconfig</h1>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Introduction" accesskey="1">Introduction</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Configuration-Files" accesskey="2">Configuration Files</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#The-C-API" accesskey="3">The C API</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#The-C_002b_002b-API" accesskey="4">The C++ API</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Example-Programs" accesskey="5">Example Programs</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Other-Bindings-and-Implementations" accesskey="6">Other Bindings and Implementations</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#License" accesskey="7">License</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Configuration-File-Grammar" accesskey="8">Configuration File Grammar</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Function-Index" accesskey="9">Function Index</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Type-Index">Type Index</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Concept-Index">Concept Index</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Introduction"></a>
<div class="header">
<p>
Next: <a href="#Configuration-Files" accesskey="n" rel="next">Configuration Files</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Why-Another-Configuration-File-Library_003f" accesskey="1">Why Another Configuration File Library?</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Using-the-Library-from-a-C-Program" accesskey="2">Using the Library from a C Program</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Using-the-Library-from-a-C_002b_002b-Program" accesskey="3">Using the Library from a C++ Program</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Multithreading-Issues" accesskey="4">Multithreading Issues</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Internationalization-Issues" accesskey="5">Internationalization Issues</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Compiling-Using-pkg_002dconfig" accesskey="6">Compiling Using pkg-config</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Version-Test-Macros" accesskey="7">Version Test Macros</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<a name="Introduction-1"></a>
<h2 class="chapter">1 Introduction</h2>
<p><i>Libconfig</i> is a library for reading, manipulating, and writing
structured configuration files. The library features a fully
reentrant parser and includes bindings for both the C and C++
programming languages.
</p>
<p>The library runs on modern POSIX-compilant systems, such as Linux,
Solaris, and Mac OS X (Darwin), as well as on Microsoft Windows
2000/XP and later (with either Microsoft Visual Studio 2005 or later,
or the GNU toolchain via the MinGW environment).
</p>
<hr>
<a name="Why-Another-Configuration-File-Library_003f"></a>
<div class="header">
<p>
Next: <a href="#Using-the-Library-from-a-C-Program" accesskey="n" rel="next">Using the Library from a C Program</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Why-Another-Configuration-File-Library_003f-1"></a>
<h3 class="section">1.1 Why Another Configuration File Library?</h3>
<p>There are several open-source configuration file libraries available
as of this writing. This library was written because each of those
libraries falls short in one or more ways. The main features of
<i>libconfig</i> that set it apart from the other libraries are:
</p>
<ul>
<li> A fully reentrant parser. Independent configurations can be parsed in concurrent threads at the same time.
</li><li> Both C <i>and</i> C++ bindings, as well as hooks to allow for the creation of wrappers in other languages.
</li><li> A simple, structured configuration file format that is more
readable and compact than XML and more flexible than the obsolete but
prevalent Windows “INI” file format.
</li><li> A low-footprint implementation (just 37K for the C library and 76K for
the C++ library) that is suitable for memory-constrained systems.
</li><li> Proper documentation.
</li></ul>
<hr>
<a name="Using-the-Library-from-a-C-Program"></a>
<div class="header">
<p>
Next: <a href="#Using-the-Library-from-a-C_002b_002b-Program" accesskey="n" rel="next">Using the Library from a C++ Program</a>, Previous: <a href="#Why-Another-Configuration-File-Library_003f" accesskey="p" rel="prev">Why Another Configuration File Library?</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Using-the-Library-from-a-C-Program-1"></a>
<h3 class="section">1.2 Using the Library from a C Program</h3>
<p>To use the library from C code, include the following preprocessor
directive in your source files:
</p>
<br>
<div class="smallexample">
<pre class="smallexample">#include <libconfig.h>
</pre></div>
<br>
<p>To link with the library, specify ‘<samp>-lconfig</samp>’ as an argument to the
linker.
</p>
<hr>
<a name="Using-the-Library-from-a-C_002b_002b-Program"></a>
<div class="header">
<p>
Next: <a href="#Multithreading-Issues" accesskey="n" rel="next">Multithreading Issues</a>, Previous: <a href="#Using-the-Library-from-a-C-Program" accesskey="p" rel="prev">Using the Library from a C Program</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Using-the-Library-from-a-C_002b_002b-Program-1"></a>
<h3 class="section">1.3 Using the Library from a C++ Program</h3>
<p>To use the library from C++, include the following preprocessor
directive in your source files:
</p>
<br>
<div class="smallexample">
<pre class="smallexample">#include <libconfig.h++>
</pre></div>
<br>
<p>Or, alternatively:
</p>
<br>
<div class="smallexample">
<pre class="smallexample">#include <libconfig.hh>
</pre></div>
<br>
<p>The C++ API classes are defined in the namespace ‘<samp>libconfig</samp>’, hence the
following statement may optionally be used:
</p>
<br>
<div class="smallexample">
<pre class="smallexample">using namespace libconfig;
</pre></div>
<br>
<p>To link with the library, specify ‘<samp>-lconfig++</samp>’ as an argument to
the linker.
</p>
<hr>
<a name="Multithreading-Issues"></a>
<div class="header">
<p>
Next: <a href="#Internationalization-Issues" accesskey="n" rel="next">Internationalization Issues</a>, Previous: <a href="#Using-the-Library-from-a-C_002b_002b-Program" accesskey="p" rel="prev">Using the Library from a C++ Program</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Multithreading-Issues-1"></a>
<h3 class="section">1.4 Multithreading Issues</h3>
<p><i>Libconfig</i> is fully <em>reentrant</em>; the functions in the library
do not make use of global variables and do not maintain state between
successive calls. Therefore two independent configurations may be safely
manipulated concurrently by two distinct threads.
</p>
<p><i>Libconfig</i> is not <em>thread-safe</em>. The library is not aware of
the presence of threads and knows nothing about the host system’s
threading model. Therefore, if an instance of a configuration is to be
accessed from multiple threads, it must be suitably protected by
synchronization mechanisms like read-write locks or mutexes; the
standard rules for safe multithreaded access to shared data must be
observed.
</p>
<p><i>Libconfig</i> is not <em>async-safe</em>. Calls should not be made into
the library from signal handlers, because some of the C library
routines that it uses may not be async-safe.
</p>
<p><i>Libconfig</i> is not guaranteed to be <em>cancel-safe</em>. Since it is
not aware of the host system’s threading model, the library does not
contain any thread cancellation points. In most cases this will not be
an issue for multithreaded programs. However, be aware that some of
the routines in the library (namely those that read/write
configurations from/to files or streams) perform I/O using C library
routines which may potentially block; whether or not these C library
routines are cancel-safe depends on the host system.
</p>
<hr>
<a name="Internationalization-Issues"></a>
<div class="header">
<p>
Next: <a href="#Compiling-Using-pkg_002dconfig" accesskey="n" rel="next">Compiling Using pkg-config</a>, Previous: <a href="#Multithreading-Issues" accesskey="p" rel="prev">Multithreading Issues</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Internationalization-Issues-1"></a>
<h3 class="section">1.5 Internationalization Issues</h3>
<a name="index-Unicode"></a>
<a name="index-UTF_002d8"></a>
<p><i>Libconfig</i> does not natively support Unicode configuration files,
but string values may contain Unicode text encoded in UTF-8; such
strings will be treated as ordinary 8-bit ASCII text by the
library. It is the responsibility of the calling program to perform
the necessary conversions to/from wide (<tt>wchar_t</tt>) strings using the
wide string conversion functions such as <tt>mbsrtowcs()</tt> and
<tt>wcsrtombs()</tt> or the <tt>iconv()</tt> function of the <i>libiconv</i>
library.
</p>
<a name="index-locale"></a>
<p>The textual representation of a floating point value varies by
locale. However, the <i>libconfig</i> grammar specifies that
floating point values are represented using a period (‘.’) as the
radix symbol; this is consistent with the grammar of most programming
languages. When a configuration is read in or written out,
<i>libconfig</i> temporarily changes the <tt>LC_NUMERIC</tt> category of the
locale of the calling thread to the “C” locale to ensure consistent
handling of floating point values regardless of the locale(s) in use
by the calling program.
</p>
<p>Note that the MinGW environment does not (as of this writing) provide
functions for changing the locale of the calling thread. Therefore,
when using <i>libconfig</i> in that environment, the calling program is
responsible for changing the <tt>LC_NUMERIC</tt> category of the locale to
the "C" locale before reading or writing a configuration.
</p>
<hr>
<a name="Compiling-Using-pkg_002dconfig"></a>
<div class="header">
<p>
Next: <a href="#Version-Test-Macros" accesskey="n" rel="next">Version Test Macros</a>, Previous: <a href="#Internationalization-Issues" accesskey="p" rel="prev">Internationalization Issues</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Compiling-Using-pkg_002dconfig-1"></a>
<h3 class="section">1.6 Compiling Using pkg-config</h3>
<a name="index-pkg_002dconfig"></a>
<p>On UNIX systems you can use the <i>pkg-config</i> utility (version 0.20
or later) to automatically select the appropriate compiler and linker
switches for <i>libconfig</i>. Ensure that the environment variable
<code>PKG_CONFIG_PATH</code> contains the absolute path to the
<samp>lib/pkgconfig</samp> subdirectory of the <i>libconfig</i> installation. Then,
you can compile and link C programs with <i>libconfig</i> as follows:
</p>
<div class="smallexample">
<pre class="smallexample">gcc `pkg-config --cflags libconfig` myprogram.c -o myprogram \
`pkg-config --libs libconfig`
</pre></div>
<br>
<p>And similarly, for C++ programs:
</p>
<div class="smallexample">
<pre class="smallexample">g++ `pkg-config --cflags libconfig++` myprogram.cpp -o myprogram \
`pkg-config --libs libconfig++`
</pre></div>
<br>
<p>Note the backticks in the above examples.
</p>
<p>When using <b>autoconf</b>, the <code>PKG_CHECK_MODULES</code> m4 macro may be used to check for the presence of a given version of <i>libconfig</i>, and set the appropriate Makefile variables automatically. For example:
</p>
<div class="smallexample">
<pre class="smallexample">PKG_CHECK_MODULES([LIBCONFIGXX], [libconfig++ >= 1.4],,
AC_MSG_ERROR([libconfig++ 1.4 or newer not found.])
)
</pre></div>
<p>In the above example, if <i>libconfig++</i> version 1.4 or newer is found,
the Makefile variables <code>LIBCONFIGXX_LIBS</code> and <code>LIBCONFIGXX_CFLAGS</code> will be
set to the appropriate compiler and linker flags for compiling with
<i>libconfig</i>, and if it is not found, the configure script will abort
with an error to that effect.
</p>
<hr>
<a name="Version-Test-Macros"></a>
<div class="header">
<p>
Previous: <a href="#Compiling-Using-pkg_002dconfig" accesskey="p" rel="prev">Compiling Using pkg-config</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Version-Test-Macros-1"></a>
<h3 class="section">1.7 Version Test Macros</h3>
<p>The <samp>libconfig.h</samp> header declares the following macros:
</p>
<dl>
<dt><a name="index-LIBCONFIG_005fVER_005fMAJOR"></a>Macro: <strong>LIBCONFIG_VER_MAJOR</strong></dt>
<dt><a name="index-LIBCONFIG_005fVER_005fMINOR"></a>Macro: <strong>LIBCONFIG_VER_MINOR</strong></dt>
<dt><a name="index-LIBCONFIG_005fVER_005fREVISION"></a>Macro: <strong>LIBCONFIG_VER_REVISION</strong></dt>
<dd>
<p>These macros represent the major version, minor version, and revision
of the <i>libconfig</i> library. For example, in <i>libconfig</i> 1.4 these
are defined as ‘<samp>1</samp>’, ‘<samp>4</samp>’, and ‘<samp>0</samp>’, respectively. These
macros can be used in preprocessor directives to determine which
<i>libconfig</i> features and/or APIs are present. For example:
</p>
<div class="smallexample">
<pre class="smallexample">#if (((LIBCONFIG_VER_MAJOR == 1) && (LIBCONFIG_VER_MINOR >= 4)) \
|| (LIBCONFIG_VER_MAJOR > 1))
/* use features present in libconfig 1.4 and later */
#endif
</pre></div>
<p>These macros were introduced in <i>libconfig</i> 1.4.
</p>
</dd></dl>
<p>Similarly, the <samp>libconfig.h++</samp> header declares the following macros:
</p>
<dl>
<dt><a name="index-LIBCONFIGXX_005fVER_005fMAJOR"></a>Macro: <strong>LIBCONFIGXX_VER_MAJOR</strong></dt>
<dt><a name="index-LIBCONFIGXX_005fVER_005fMINOR"></a>Macro: <strong>LIBCONFIGXX_VER_MINOR</strong></dt>
<dt><a name="index-LIBCONFIGXX_005fVER_005fREVISION"></a>Macro: <strong>LIBCONFIGXX_VER_REVISION</strong></dt>
<dd>
<p>These macros represent the major version, minor version, and revision
of the <i>libconfig++</i> library.
</p>
</dd></dl>
<hr>
<a name="Configuration-Files"></a>
<div class="header">
<p>
Next: <a href="#The-C-API" accesskey="n" rel="next">The C API</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Settings" accesskey="1">Settings</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Groups" accesskey="2">Groups</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Arrays" accesskey="3">Arrays</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Lists" accesskey="4">Lists</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Integer-Values" accesskey="5">Integer Values</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#g_t64_002dbit-Integer-Values" accesskey="6">64-bit Integer Values</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Floating-Point-Values" accesskey="7">Floating Point Values</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Boolean-Values" accesskey="8">Boolean Values</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#String-Values" accesskey="9">String Values</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Comments">Comments</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Include-Directives">Include Directives</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<a name="Configuration-Files-1"></a>
<h2 class="chapter">2 Configuration Files</h2>
<p><i>Libconfig</i> supports structured, hierarchical configurations. These
configurations can be read from and written to files and manipulated
in memory.
</p>
<a name="index-setting"></a>
<a name="index-value"></a>
<a name="index-scalar-value"></a>
<a name="index-array"></a>
<a name="index-group"></a>
<a name="index-list"></a>
<a name="index-configuration"></a>
<p>A <em>configuration</em> consists of a group of <em>settings</em>, which
associate names with values. A <em>value</em> can be one of the
following:
</p>
<ul>
<li> A <em>scalar value</em>: integer, 64-bit integer, floating-point number, boolean,
or string
</li><li> An <em>array</em>, which is a sequence of scalar values, all of which must have the same type
</li><li> A <em>group</em>, which is a collection of settings
</li><li> A <em>list</em>, which is a sequence of values of any type, including other lists
</li></ul>
<p>Consider the following configuration file for a hypothetical GUI
application, which illustrates all of the elements of the configuration
file grammar.
</p>
<br>
<table class="cartouche" border="1"><tr><td>
<div class="smallexample">
<pre class="smallexample"># Example application configuration file
version = "1.0";
application:
{
window:
{
title = "My Application";
size = { w = 640; h = 480; };
pos = { x = 350; y = 250; };
};
list = ( ( "abc", 123, true ), 1.234, ( /* an empty list */ ) );
books = ( { title = "Treasure Island";
author = "Robert Louis Stevenson";
price = 29.95;
qty = 5; },
{ title = "Snow Crash";
author = "Neal Stephenson";
price = 9.99;
qty = 8; } );
misc:
{
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3; // hex
umask = 0027; // octal. Range limited to that of "int"
};
};
</pre></div>
</td></tr></table>
<br>
<a name="index-path"></a>
<p>Settings can be uniquely identified within the configuration by a
<em>path</em>. The path is a dot-separated sequence of names, beginning
at a top-level group and ending at the setting itself. Each name in
the path is the name of a setting; if the setting has no name because
it is an element in a list or array, an integer index in square
brackets can be used as the name.
</p>
<p>For example, in our hypothetical configuration file, the path to the
<code>x</code> setting is <code>application.window.pos.x</code>; the path to the
<code>version</code> setting is simply <code>version</code>; and the path to the
<code>title</code> setting of the second book in the <code>books</code> list is
<code>application.books.[1].title</code>.
</p>
<p>The datatype of a value is determined from the format of the value
itself. If the value is enclosed in double quotes, it is treated as a
string. If it looks like an integer or floating point number, it is
treated as such. If it is one of the values <code>TRUE</code>, <code>true</code>,
<code>FALSE</code>, or <code>false</code> (or any other mixed-case version of
those tokens, e.g., <code>True</code> or <code>FaLsE</code>), it is treated as a
boolean. If it consists of a comma-separated list of values enclosed
in square brackets, it is treated as an array. And if it consists of a
comma-separated list of values enclosed in parentheses, it is treated
as a list. Any value which does not meet any of these criteria is
considered invalid and results in a parse error.
</p>
<p>All names are case-sensitive. They may consist only of alphanumeric
characters, dashes (‘<samp>-</samp>’), underscores (‘<samp>_</samp>’), and asterisks
(‘<samp>*</samp>’), and must begin with a letter or asterisk. No other
characters are allowed.
</p>
<p>In C and C++, integer, 64-bit integer, floating point, and string
values are mapped to the native types <code>int</code>, <code>long long</code>,
<code>double</code>, and <code>const char *</code>, respectively. The boolean type
is mapped to <code>int</code> in C and <code>bool</code> in C++.
</p>
<p>The following sections describe the elements of the configuration file
grammar in additional detail.
</p>
<hr>
<a name="Settings"></a>
<div class="header">
<p>
Next: <a href="#Groups" accesskey="n" rel="next">Groups</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Settings-1"></a>
<h3 class="section">2.1 Settings</h3>
<p>A setting has the form:
</p>
<p><i>name</i> <b>=</b> <i>value</i> <b>;</b>
</p>
<p>or:
</p>
<p><i>name</i> <b>:</b> <i>value</i> <b>;</b>
</p>
<p>The trailing semicolon is optional. Whitespace is not significant.
</p>
<p>The value may be a scalar value, an array, a group, or a list.
</p>
<hr>
<a name="Groups"></a>
<div class="header">
<p>
Next: <a href="#Arrays" accesskey="n" rel="next">Arrays</a>, Previous: <a href="#Settings" accesskey="p" rel="prev">Settings</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Groups-1"></a>
<h3 class="section">2.2 Groups</h3>
<p>A group has the form:
</p>
<p><b>{</b>
<i>settings ...</i>
<b>}</b>
</p>
<p>Groups can contain any number of settings, but each setting must have
a unique name within the group.
</p>
<hr>
<a name="Arrays"></a>
<div class="header">
<p>
Next: <a href="#Lists" accesskey="n" rel="next">Lists</a>, Previous: <a href="#Groups" accesskey="p" rel="prev">Groups</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Arrays-1"></a>
<h3 class="section">2.3 Arrays</h3>
<p>An array has the form:
</p>
<p><b>[</b> <i>value</i><b>,</b> <i>value ...</i> <b>]</b>
</p>
<p>An array may have zero or more elements, but the elements must all be
scalar values of the same type.
</p>
<p>The last element in an array may be followed by a comma, which will be ignored.
</p>
<hr>
<a name="Lists"></a>
<div class="header">
<p>
Next: <a href="#Integer-Values" accesskey="n" rel="next">Integer Values</a>, Previous: <a href="#Arrays" accesskey="p" rel="prev">Arrays</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Lists-1"></a>
<h3 class="section">2.4 Lists</h3>
<p>A list has the form:
</p>
<p><b>(</b> <i>value</i><b>,</b> <i>value ...</i> <b>)</b>
</p>
<p>A list may have zero or more elements, each of which can be a scalar
value, an array, a group, or another list.
</p>
<p>The last element in a list may be followed by a comma, which will be ignored.
</p>
<hr>
<a name="Integer-Values"></a>
<div class="header">
<p>
Next: <a href="#g_t64_002dbit-Integer-Values" accesskey="n" rel="next">64-bit Integer Values</a>, Previous: <a href="#Lists" accesskey="p" rel="prev">Lists</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Integer-Values-1"></a>
<h3 class="section">2.5 Integer Values</h3>
<p>Integers can be represented in one of two ways: as a series of one or
more decimal digits (‘<samp>0</samp>’ - ‘<samp>9</samp>’), with an optional leading
sign character (‘<samp>+</samp>’ or ‘<samp>-</samp>’); or as a hexadecimal value
consisting of the characters ‘<samp>0x</samp>’ followed by a series of one or
more hexadecimal digits (‘<samp>0</samp>’ - ‘<samp>9</samp>’, ‘<samp>A</samp>’ - ‘<samp>F</samp>’,
‘<samp>a</samp>’ - ‘<samp>f</samp>’). Additionally, octal notation integers (that is,
those having a leading zero with non-zero value) are also allowed.
</p>
<hr>
<a name="g_t64_002dbit-Integer-Values"></a>
<div class="header">
<p>
Next: <a href="#Floating-Point-Values" accesskey="n" rel="next">Floating Point Values</a>, Previous: <a href="#Integer-Values" accesskey="p" rel="prev">Integer Values</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="g_t64_002dbit-Integer-Values-1"></a>
<h3 class="section">2.6 64-bit Integer Values</h3>
<p>Long long (64-bit) integers are represented identically to integers,
except that an ‘L’ character is appended to indicate a 64-bit
value. For example, ‘<samp>0L</samp>’ indicates a 64-bit integer value 0. As
of version 1.5 of the library, the trailing ‘L’ is optional; if the
integer value exceeds the range of a 32-bit integer, it will
automatically be interpreted as a 64-bit integer.
</p>
<p>The <i>integer</i> and <i>64-bit integer</i> setting types are interchangeable to the
extent that a conversion between the corresponding native types would not
result in an overflow or underflow. For example, a <i>long long</i> value can be
written to a setting that has an <i>integer</i> type, if that value is within the
range of an <i>int</i>. This rule applies to every API function or method that
reads a value from or writes a value to a setting: if the type conversion would
not result in an overflow or underflow, then the call will succeed, and
otherwise it will fail. This behavior was not well-defined prior to version 1.7
of the library.
</p>
<hr>
<a name="Floating-Point-Values"></a>
<div class="header">
<p>
Next: <a href="#Boolean-Values" accesskey="n" rel="next">Boolean Values</a>, Previous: <a href="#g_t64_002dbit-Integer-Values" accesskey="p" rel="prev">64-bit Integer Values</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Floating-Point-Values-1"></a>
<h3 class="section">2.7 Floating Point Values</h3>
<p>Floating point values consist of a series of one or more digits, one
decimal point, an optional leading sign character (‘<samp>+</samp>’ or
‘<samp>-</samp>’), and an optional exponent. An exponent consists of the
letter ‘<samp>E</samp>’ or ‘<samp>e</samp>’, an optional sign character, and a series
of one or more digits.
</p>
<hr>
<a name="Boolean-Values"></a>
<div class="header">
<p>
Next: <a href="#String-Values" accesskey="n" rel="next">String Values</a>, Previous: <a href="#Floating-Point-Values" accesskey="p" rel="prev">Floating Point Values</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Boolean-Values-1"></a>
<h3 class="section">2.8 Boolean Values</h3>
<p>Boolean values may have one of the following values: ‘<samp>true</samp>’,
‘<samp>false</samp>’, or any mixed-case variation thereof.
</p>
<hr>
<a name="String-Values"></a>
<div class="header">
<p>
Next: <a href="#Comments" accesskey="n" rel="next">Comments</a>, Previous: <a href="#Boolean-Values" accesskey="p" rel="prev">Boolean Values</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="String-Values-1"></a>
<h3 class="section">2.9 String Values</h3>
<a name="index-escape-sequence"></a>
<p>String values consist of arbitrary text delimited by double
quotes. Literal double quotes can be escaped by preceding them with a
backslash: ‘<samp>\"</samp>’. The escape sequences ‘<samp>\\</samp>’, ‘<samp>\f</samp>’,
‘<samp>\n</samp>’, ‘<samp>\r</samp>’, and ‘<samp>\t</samp>’ are also recognized, and have the
usual meaning.
</p>
<p>In addition, the ‘<samp>\x</samp>’ escape sequence is supported; this sequence
must be followed by <i>exactly two</i> hexadecimal digits, which represent an
8-bit ASCII value. For example, ‘<samp>\xFF</samp>’ represents the character
with ASCII code 0xFF.
</p>
<p>No other escape sequences are currently supported.
</p>
<p>Adjacent strings are automatically concatenated, as in C/C++ source
code. This is useful for formatting very long strings as sequences of
shorter strings. For example, the following constructs are equivalent:
</p>
<ul>
<li> <code>"The quick brown fox jumped over the lazy dog."</code>
</li><li> <code>"The quick brown fox"</code> <br>
<code>" jumped over the lazy dog."</code>
</li><li> <code>"The quick" /* comment */ " brown fox " // another comment</code> <br>
<code>"jumped over the lazy dog."</code>
</li></ul>
<hr>
<a name="Comments"></a>
<div class="header">
<p>
Next: <a href="#Include-Directives" accesskey="n" rel="next">Include Directives</a>, Previous: <a href="#String-Values" accesskey="p" rel="prev">String Values</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Comments-1"></a>
<h3 class="section">2.10 Comments</h3>
<a name="index-comment"></a>
<p>Three types of comments are allowed within a configuration:
</p>
<ul>
<li> Script-style comments. All text beginning with a ‘<samp>#</samp>’ character
to the end of the line is ignored.
</li><li> C-style comments. All text, including line breaks, between a starting
‘<samp>/*</samp>’ sequence and an ending ‘<samp>*/</samp>’ sequence is ignored.
</li><li> C++-style comments. All text beginning with a ‘<samp>//</samp>’ sequence to the
end of the line is ignored.
</li></ul>
<p>As expected, comment delimiters appearing within quoted strings are
treated as literal text.
</p>
<p>Comments are ignored when the configuration is read in, so they are
not treated as part of the configuration. Therefore if the
configuration is written back out to a stream, any comments that were
present in the original configuration will be lost.
</p>
<hr>
<a name="Include-Directives"></a>
<div class="header">
<p>
Previous: <a href="#Comments" accesskey="p" rel="prev">Comments</a>, Up: <a href="#Configuration-Files" accesskey="u" rel="up">Configuration Files</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Include-Directives-1"></a>
<h3 class="section">2.11 Include Directives</h3>
<a name="index-include-directive"></a>
<p>A configuration file may “include” the contents of other files
using an <i>include directive</i>. This directive has the effect of
inlining the contents of the named file(s) at the point of inclusion.
</p>
<p>An include directive must appear on its own line in the input. It has
the form:
</p>
<p><b>@include "</b><i>path</i><b>"</b>
</p>
<a name="index-include-function"></a>
<p>The interpretation of <i>path</i> depends on the currently registered
<i>include function</i>. The default include function prepends the include
directory, if any, to <i>path</i>, and then interprets the result as a single,
literal file path. The application may supply its own include function which
does variable substitution, wildcard expansion, or other transformations,
returning a list of zero or more paths to files whose contents should be inlined
at the point of inclusion.
</p>
<p>Any backslashes or double quotes in the path must be escaped as
‘<samp>\\</samp>’ and ‘<samp>\"</samp>’, respectively.
</p>
<p>For example, consider the following two configuration files:
</p>
<table class="cartouche" border="1"><tr><td>
<div class="smallexample">
<pre class="smallexample"># file: quote.cfg
quote = "Criticism may not be agreeable, but it is necessary."
" It fulfils the same function as pain in the human"
" body. It calls attention to an unhealthy state of"
" things.\n"
"\t--Winston Churchill";
</pre></div>
</td></tr></table>
<table class="cartouche" border="1"><tr><td>
<div class="smallexample">
<pre class="smallexample"># file: test.cfg
info: {
name = "Winston Churchill";
@include "quote.cfg"
country = "UK";
};
</pre></div>
</td></tr></table>
<p>The resulting configuration will be equivalent to one in which the
contents of the file ‘<samp>quote.cfg</samp>’ appeared at the point where the
include directive is placed.
</p>
<p>Include files may be nested to a maximum of 10 levels; exceeding this
limit results in a parse error.
</p>
<p>When the path argument to an <b>@include</b> directive is a relative
path, then it will be interpreted as being relative to the include
directory that has been been set by means of
<code>config_set_include_dir()</code>. If no include directory has been set,
then it will be taken as being relative to the program’s current
working directory.
</p>
<p>Like comments, include directives are not part of the configuration
file syntax. They are processed before the configuration itself is
parsed. Therefore, they are not preserved when the configuration is
written back out to a stream. There is presently no support for
programmatically inserting include directives into a configuration.
</p>
<hr>
<a name="The-C-API"></a>
<div class="header">
<p>
Next: <a href="#The-C_002b_002b-API" accesskey="n" rel="next">The C++ API</a>, Previous: <a href="#Configuration-Files" accesskey="p" rel="prev">Configuration Files</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-C-API-1"></a>
<h2 class="chapter">3 The C API</h2>
<a name="index-config_005ft"></a>
<a name="index-config_005fsetting_005ft"></a>
<p>This chapter describes the C library API. The type <i>config_t</i>
represents a configuration, and the type <i>config_setting_t</i> represents
a configuration setting.
</p>
<p>The boolean values <code>CONFIG_TRUE</code> and <code>CONFIG_FALSE</code> are
macros defined as <code>(1)</code> and <code>(0)</code>, respectively.
</p>
<dl>
<dt><a name="index-config_005finit"></a>Function: <em>void</em> <strong>config_init</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dt><a name="index-config_005fdestroy"></a>Function: <em>void</em> <strong>config_destroy</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dd>
<p>These functions initialize and destroy the configuration object <var>config</var>.
</p>
<p><code>config_init()</code> initializes the <i>config_t</i> structure pointed to by
<var>config</var> as a new, empty configuration.
</p>
<p><code>config_destroy()</code> destroys the configuration <var>config</var>,
deallocating all memory associated with the configuration, but does not
attempt to deallocate the <i>config_t</i> structure itself.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fclear"></a>Function: <em>void</em> <strong>config_clear</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dd>
<p><b>Since <i>v1.7</i></b>
</p>
<p>This function clears the configuration <var>config</var>. All child settings of the
root setting are recursively destroyed. All other attributes of the configuration
are left unchanged.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fread"></a>Function: <em>int</em> <strong>config_read</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->, FILE * <var>stream</var><!-- /@w -->)</em></dt>
<dd>
<p>This function reads and parses a configuration from the given
<var>stream</var> into the configuration object <var>config</var>. It returns
<code>CONFIG_TRUE</code> on success, or <code>CONFIG_FALSE</code> on failure; the
<code>config_error_text()</code>, <code>config_error_file()</code>,
<code>config_error_line()</code>, and <code>config_error_type()</code> functions,
described below, can be used to obtain information about the error.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fread_005ffile"></a>Function: <em>int</em> <strong>config_read_file</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->, const char * <var>filename</var><!-- /@w -->)</em></dt>
<dd>
<p>This function reads and parses a configuration from the file named
<var>filename</var> into the configuration object <var>config</var>. It returns
<code>CONFIG_TRUE</code> on success, or <code>CONFIG_FALSE</code> on failure; the
<code>config_error_text()</code> and <code>config_error_line()</code> functions,
described below, can be used to obtain information about the error.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fread_005fstring"></a>Function: <em>int</em> <strong>config_read_string</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->, const char * <var>str</var><!-- /@w -->)</em></dt>
<dd>
<p>This function reads and parses a configuration from the string
<var>str</var> into the configuration object <var>config</var>. It returns
<code>CONFIG_TRUE</code> on success, or <code>CONFIG_FALSE</code> on failure; the
<code>config_error_text()</code> and <code>config_error_line()</code> functions,
described below, can be used to obtain information about the error.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fwrite"></a>Function: <em>void</em> <strong>config_write</strong> <em>(const <span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->, FILE * <var>stream</var><!-- /@w -->)</em></dt>
<dd>
<p>This function writes the configuration <var>config</var> to the given
<var>stream</var>.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fwrite_005ffile"></a>Function: <em>int</em> <strong>config_write_file</strong> <em>(<span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->, const char * <var>filename</var><!-- /@w -->)</em></dt>
<dd>
<p>This function writes the configuration <var>config</var> to the file named
<var>filename</var>. It returns <code>CONFIG_TRUE</code> on success, or
<code>CONFIG_FALSE</code> on failure.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005ferror_005ftext"></a>Function: <em>const char *</em> <strong>config_error_text</strong> <em>(const <span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dt><a name="index-config_005ferror_005ffile"></a>Function: <em>const char *</em> <strong>config_error_file</strong> <em>(const <span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dt><a name="index-config_005ferror_005fline"></a>Function: <em>int</em> <strong>config_error_line</strong> <em>(const <span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dd>
<p>These functions, which are implemented as macros, return the text,
filename, and line number of the parse error, if one occurred during a
call to <code>config_read()</code>, <code>config_read_string()</code>, or
<code>config_read_file()</code>. Storage for the strings returned by
<code>config_error_text()</code> and <code>config_error_file()</code> are managed
by the library and released automatically when the configuration is
destroyed; these strings must not be freed by the caller. If the error
occurred in text that was read from a string or stream,
<code>config_error_file()</code> will return NULL.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005ferror_005ftype"></a>Function: <em>config_error_t</em> <strong>config_error_type</strong> <em>(const <span class="nolinebreak">config_t</span> * <var>config</var><!-- /@w -->)</em></dt>
<dd><a name="index-config_005ferror_005ft"></a>
<p>This function, which is implemented as a macro, returns the type of
error that occurred during the last call to one of the read or write
functions. The <var>config_error_t</var> type is an enumeration with the
following values: <code>CONFIG_ERR_NONE</code>, <code>CONFIG_ERR_FILE_IO</code>,
<code>CONFIG_ERR_PARSE</code>. These represent success, a file I/O error,
and a parsing error, respectively.
</p>
</dd></dl>
<dl>
<dt><a name="index-config_005fset_005finclude_005fdir"></a>Function: <em>void</em> <strong>config_set_include_dir</strong> <em>(<span class="nolinebreak">config_t</span> *<var>config</var><!-- /@w -->, const char *<var><span class="nolinebreak">include_dir</span></var><!-- /@w -->)</em></dt>
<dt><a name="index-config_005fget_005finclude_005fdir"></a>Function: <em>const char *</em> <strong>config_get_include_dir</strong> <em>(const <span class="nolinebreak">config_t</span> *<var>config</var><!-- /@w -->)</em></dt>
<dd>
<p><code>config_set_include_dir()</code> specifies the include directory,
<var>include_dir</var>, relative to which the files specified in
‘<samp>@include</samp>’ directives will be located for the configuration