PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libconfig-1.4.8/doc/libconfig.texi

#
Unknown | 1910 lines | 1435 code | 475 blank | 0 comment | 0 complexity | 72db844a3b5329c8ba894935cfe5491c MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. \input texinfo.tex @c -*-texinfo-*-
  2. @c
  3. @c %**start of header
  4. @c All text is ignored before the setfilename.
  5. @setfilename libconfig.info
  6. @settitle libconfig
  7. @set edition 1.4.8
  8. @set update-date 4 August 2011
  9. @set subtitle-text A Library For Processing Structured Configuration Files
  10. @set author-text Mark A.@: Lindner
  11. @comment %**end of header
  12. @dircategory Software libraries
  13. @direntry
  14. * libconfig: (libconfig). A Library For Processing Structured Configuration Files
  15. @end direntry
  16. @tex
  17. \global\emergencystretch = .3\hsize
  18. @end tex
  19. @setchapternewpage odd
  20. @titlepage
  21. @title libconfig
  22. @subtitle @value{subtitle-text}
  23. @subtitle Version @value{edition}
  24. @subtitle @value{update-date}
  25. @author @value{author-text}
  26. @page
  27. @vskip 0pt plus 1filll
  28. Copyright @copyright{} 2005-2011 Mark A Lindner
  29. Permission is granted to make and distribute verbatim copies of
  30. this manual provided the copyright notice and this permission notice
  31. are preserved on all copies.
  32. Permission is granted to copy and distribute modified versions of this
  33. manual under the conditions for verbatim copying, provided that the entire
  34. resulting derived work is distributed under the terms of a permission
  35. notice identical to this one.
  36. @end titlepage
  37. @c Give the HTML output a title page that somewhat resembles the printed one
  38. @ifhtml
  39. @html
  40. <hr noshade size=6 color="black">
  41. <div align=right>@value{subtitle-text}<br>
  42. Version @value{edition}<br>
  43. @value{update-date}</div>
  44. <br><br><br><br>
  45. <font size=+1>@value{author-text}</font>
  46. <hr size=3 noshade color="black">
  47. <br><br>
  48. @end html
  49. @end ifhtml
  50. @contents
  51. @ifnottex
  52. @node Top
  53. @comment node-name, next, previous, up
  54. @top libconfig
  55. @end ifnottex
  56. @menu
  57. * Introduction::
  58. * Configuration Files::
  59. * The C API::
  60. * The C++ API::
  61. * Example Programs::
  62. * Configuration File Grammar::
  63. * License::
  64. * Function Index::
  65. * Type Index::
  66. * Concept Index::
  67. @end menu
  68. @node Introduction, Configuration Files, Top, Top
  69. @comment node-name, next, previous, up
  70. @menu
  71. * Why Another Configuration File Library?::
  72. * Using the Library from a C Program::
  73. * Using the Library from a C++ Program::
  74. * Multithreading Issues::
  75. * Internationalization Issues::
  76. * Compiling Using pkg-config::
  77. * Version Test Macros::
  78. @end menu
  79. @chapter Introduction
  80. @i{Libconfig} is a library for reading, manipulating, and writing
  81. structured configuration files. The library features a fully
  82. reentrant parser and includes bindings for both the C and C++
  83. programming languages.
  84. The library runs on modern POSIX-compilant systems, such as Linux,
  85. Solaris, and Mac OS X (Darwin), as well as on Microsoft Windows
  86. 2000/XP and later (with either Microsoft Visual Studio 2005 or later,
  87. or the GNU toolchain via the MinGW environment).
  88. @node Why Another Configuration File Library?, Using the Library from a C Program, , Introduction
  89. @comment node-name, next, previous, up
  90. @section Why Another Configuration File Library?
  91. There are several open-source configuration file libraries available
  92. as of this writing. This library was written because each of those
  93. libraries falls short in one or more ways. The main features of
  94. @i{libconfig} that set it apart from the other libraries are:
  95. @itemize @bullet
  96. @item A fully reentrant parser. Independent configurations can be parsed in concurrent threads at the same time.
  97. @item Both C @i{and} C++ bindings, as well as hooks to allow for the creation of wrappers in other languages.
  98. @item A simple, structured configuration file format that is more
  99. readable and compact than XML and more flexible than the obsolete but
  100. prevalent Windows ``INI'' file format.
  101. @item A low-footprint implementation (just 37K for the C library and 76K for
  102. the C++ library) that is suitable for memory-constrained systems.
  103. @item Proper documentation.
  104. @end itemize
  105. @node Using the Library from a C Program, Using the Library from a C++ Program, Why Another Configuration File Library?, Introduction
  106. @comment node-name, next, previous, up
  107. @section Using the Library from a C Program
  108. To use the library from C code, include the following preprocessor
  109. directive in your source files:
  110. @sp 1
  111. @smallexample
  112. #include <libconfig.h>
  113. @end smallexample
  114. @sp 1
  115. To link with the library, specify @samp{-lconfig} as an argument to the
  116. linker.
  117. @node Using the Library from a C++ Program, Multithreading Issues, Using the Library from a C Program, Introduction
  118. @comment node-name, next, previous, up
  119. @section Using the Library from a C++ Program
  120. To use the library from C++, include the following preprocessor
  121. directive in your source files:
  122. @sp 1
  123. @smallexample
  124. #include <libconfig.h++>
  125. @end smallexample
  126. @sp 1
  127. Or, alternatively:
  128. @sp 1
  129. @smallexample
  130. #include <libconfig.hh>
  131. @end smallexample
  132. @sp 1
  133. @page
  134. The C++ API classes are defined in the namespace @samp{libconfig}, hence the
  135. following statement may optionally be used:
  136. @sp 1
  137. @smallexample
  138. using namespace libconfig;
  139. @end smallexample
  140. @sp 1
  141. To link with the library, specify @samp{-lconfig++} as an argument to
  142. the linker.
  143. @node Multithreading Issues, Internationalization Issues, Using the Library from a C++ Program, Introduction
  144. @comment node-name, next, previous, up
  145. @section Multithreading Issues
  146. @i{Libconfig} is fully @dfn{reentrant}; the functions in the library
  147. do not make use of global variables and do not maintain state between
  148. successive calls. Therefore two independent configurations may be safely
  149. manipulated concurrently by two distinct threads.
  150. @i{Libconfig} is not @dfn{thread-safe}. The library is not aware of
  151. the presence of threads and knows nothing about the host system's
  152. threading model. Therefore, if an instance of a configuration is to be
  153. accessed from multiple threads, it must be suitably protected by
  154. synchronization mechanisms like read-write locks or mutexes; the
  155. standard rules for safe multithreaded access to shared data must be
  156. observed.
  157. @i{Libconfig} is not @dfn{async-safe}. Calls should not be made into
  158. the library from signal handlers, because some of the C library
  159. routines that it uses may not be async-safe.
  160. @i{Libconfig} is not guaranteed to be @dfn{cancel-safe}. Since it is
  161. not aware of the host system's threading model, the library does not
  162. contain any thread cancellation points. In most cases this will not be
  163. an issue for multithreaded programs. However, be aware that some of
  164. the routines in the library (namely those that read/write
  165. configurations from/to files or streams) perform I/O using C library
  166. routines which may potentially block; whether or not these C library
  167. routines are cancel-safe depends on the host system.
  168. @node Internationalization Issues, Compiling Using pkg-config, Multithreading Issues, Introduction
  169. @comment node-name, next, previous, up
  170. @section Internationalization Issues
  171. @cindex Unicode
  172. @cindex UTF-8
  173. @i{Libconfig} does not natively support Unicode configuration files,
  174. but string values may contain Unicode text encoded in UTF-8; such
  175. strings will be treated as ordinary 8-bit ASCII text by the
  176. library. It is the responsibility of the calling program to perform
  177. the necessary conversions to/from wide (@t{wchar_t}) strings using the
  178. wide string conversion functions such as @t{mbsrtowcs()} and
  179. @t{wcsrtombs()} or the @t{iconv()} function of the @i{libiconv}
  180. library.
  181. @cindex locale
  182. The textual representation of a floating point value varies by
  183. locale. However, the @i{libconfig} grammar specifies that
  184. floating point values are represented using a period (`.') as the
  185. radix symbol; this is consistent with the grammar of most programming
  186. languages. When a configuration is read in or written out,
  187. @i{libconfig} temporarily changes the @t{LC_NUMERIC} category of the
  188. locale of the calling thread to the ``C'' locale to ensure consistent
  189. handling of floating point values regardless of the locale(s) in use
  190. by the calling program.
  191. Note that the MinGW environment does not (as of this writing) provide
  192. functions for changing the locale of the calling thread. Therefore,
  193. when using @i{libconfig} in that environment, the calling program is
  194. responsible for changing the @t{LC_NUMERIC} category of the locale to
  195. the "C" locale before reading or writing a configuration.
  196. @node Compiling Using pkg-config, Version Test Macros, Internationalization Issues, Introduction
  197. @comment node-name, next, previous, up
  198. @section Compiling Using pkg-config
  199. On UNIX systems you can use the @i{pkg-config} utility (version 0.20
  200. or later) to automatically select the appropriate compiler and linker
  201. switches for @i{libconfig}. Ensure that the environment variable
  202. @code{PKG_CONFIG_PATH} contains the absolute path to the
  203. @file{lib/pkgconfig} subdirectory of the @i{libconfig} installation. Then,
  204. you can compile and link C programs with @i{libconfig} as follows:
  205. @smallexample
  206. gcc `pkg-config --cflags libconfig` myprogram.c -o myprogram \
  207. `pkg-config --libs libconfig`
  208. @end smallexample
  209. @sp 1
  210. And similarly, for C++ programs:
  211. @smallexample
  212. g++ `pkg-config --cflags libconfig++` myprogram.cpp -o myprogram \
  213. `pkg-config --libs libconfig++`
  214. @end smallexample
  215. @sp 1
  216. Note the backticks in the above examples.
  217. When using @b{autoconf}, the @code{PKG_CHECK_MODULES} m4 macro may be used to check for the presence of a given version of @i{libconfig}, and set the appropriate Makefile variables automatically. For example:
  218. @smallexample
  219. PKG_CHECK_MODULES([LIBCONFIGXX], [libconfig++ >= 1.4],,
  220. AC_MSG_ERROR([libconfig++ 1.4 or newer not found.])
  221. )
  222. @end smallexample
  223. In the above example, if @i{libconfig++} version 1.4 or newer is found,
  224. the Makefile variables @code{LIBCONFIGXX_LIBS} and @code{LIBCONFIGXX_CFLAGS} will be
  225. set to the appropriate compiler and linker flags for compiling with
  226. @i{libconfig}, and if it is not found, the configure script will abort
  227. with an error to that effect.
  228. @node Version Test Macros, , Compiling Using pkg-config, Introduction
  229. @comment node-name, next, previous, up
  230. @section Version Test Macros
  231. The @file{libconfig.h} header declares the following macros:
  232. @defmac LIBCONFIG_VER_MAJOR
  233. @defmacx LIBCONFIG_VER_MINOR
  234. @defmacx LIBCONFIG_VER_REVISION
  235. These macros represent the major version, minor version, and revision
  236. of the @i{libconfig} library. For example, in @i{libconfig} 1.4 these
  237. are defined as @samp{1}, @samp{4}, and @samp{0}, respectively. These
  238. macros can be used in preprocessor directives to determine which
  239. @i{libconfig} features and/or APIs are present. For example:
  240. @smallexample
  241. #if (((LIBCONFIG_VER_MAJOR == 1) && (LIBCONFIG_VER_MINOR >= 4)) \
  242. || (LIBCONFIG_VER_MAJOR > 1))
  243. /* use features present in libconfig 1.4 and later */
  244. #endif
  245. @end smallexample
  246. These macros were introduced in @i{libconfig} 1.4.
  247. @end defmac
  248. Similarly, the @file{libconfig.h++} header declares the following macros:
  249. @defmac LIBCONFIGXX_VER_MAJOR
  250. @defmacx LIBCONFIGXX_VER_MINOR
  251. @defmacx LIBCONFIGXX_VER_REVISION
  252. These macros represent the major version, minor version, and revision
  253. of the @i{libconfig++} library.
  254. @end defmac
  255. @node Configuration Files, The C API, Introduction, Top
  256. @comment node-name, next, previous, up
  257. @menu
  258. * Settings::
  259. * Groups::
  260. * Arrays::
  261. * Lists::
  262. * Integer Values::
  263. * 64-bit Integer Values::
  264. * Floating Point Values::
  265. * Boolean Values::
  266. * String Values::
  267. * Comments::
  268. * Include Directives::
  269. @end menu
  270. @chapter Configuration Files
  271. @i{Libconfig} supports structured, hierarchical configurations. These
  272. configurations can be read from and written to files and manipulated
  273. in memory.
  274. @cindex setting
  275. @cindex value
  276. @cindex scalar value
  277. @cindex array
  278. @cindex group
  279. @cindex list
  280. @cindex configuration
  281. A @dfn{configuration} consists of a group of @dfn{settings}, which
  282. associate names with values. A @dfn{value} can be one of the
  283. following:
  284. @itemize @bullet
  285. @item A @dfn{scalar value}: integer, 64-bit integer, floating-point number, boolean,
  286. or string
  287. @item An @dfn{array}, which is a sequence of scalar values, all of which must have the same type
  288. @item A @dfn{group}, which is a collection of settings
  289. @item A @dfn{list}, which is a sequence of values of any type, including other lists
  290. @end itemize
  291. Consider the following configuration file for a hypothetical GUI
  292. application, which illustrates all of the elements of the configuration
  293. file grammar.
  294. @sp 1
  295. @cartouche
  296. @smallexample
  297. # Example application configuration file
  298. version = "1.0";
  299. application:
  300. @{
  301. window:
  302. @{
  303. title = "My Application";
  304. size = @{ w = 640; h = 480; @};
  305. pos = @{ x = 350; y = 250; @};
  306. @};
  307. list = ( ( "abc", 123, true ), 1.234, ( /* an empty list */) );
  308. books = ( @{ title = "Treasure Island";
  309. author = "Robert Louis Stevenson";
  310. price = 29.95;
  311. qty = 5; @},
  312. @{ title = "Snow Crash";
  313. author = "Neal Stephenson";
  314. price = 9.99;
  315. qty = 8; @} );
  316. misc:
  317. @{
  318. pi = 3.141592654;
  319. bigint = 9223372036854775807L;
  320. columns = [ "Last Name", "First Name", "MI" ];
  321. bitmask = 0x1FC3;
  322. @};
  323. @};
  324. @end smallexample
  325. @end cartouche
  326. @sp 1
  327. @cindex path
  328. Settings can be uniquely identified within the configuration by a
  329. @dfn{path}. The path is a dot-separated sequence of names, beginning
  330. at a top-level group and ending at the setting itself. Each name in
  331. the path is the name of a setting; if the setting has no name because
  332. it is an element in a list or array, an integer index in square
  333. brackets can be used as the name.
  334. For example, in our hypothetical configuration file, the path to the
  335. @code{x} setting is @code{application.window.pos.x}; the path to the
  336. @code{version} setting is simply @code{version}; and the path to the
  337. @code{title} setting of the second book in the @code{books} list is
  338. @code{application.books.[1].title}.
  339. The datatype of a value is determined from the format of the value
  340. itself. If the value is enclosed in double quotes, it is treated as a
  341. string. If it looks like an integer or floating point number, it is
  342. treated as such. If it is one of the values @code{TRUE}, @code{true},
  343. @code{FALSE}, or @code{false} (or any other mixed-case version of
  344. those tokens, e.g., @code{True} or @code{FaLsE}), it is treated as a
  345. boolean. If it consists of a comma-separated list of values enclosed
  346. in square brackets, it is treated as an array. And if it consists of a
  347. comma-separated list of values enclosed in parentheses, it is treated
  348. as a list. Any value which does not meet any of these criteria is
  349. considered invalid and results in a parse error.
  350. All names are case-sensitive. They may consist only of alphanumeric
  351. characters, dashes (@samp{-}), underscores (@samp{_}), and asterisks
  352. (@samp{*}), and must begin with a letter or asterisk. No other
  353. characters are allowed.
  354. In C and C++, integer, 64-bit integer, floating point, and string
  355. values are mapped to the types @code{int}, @code{long long},
  356. @code{double}, and @code{const char *}, respectively. The boolean type
  357. is mapped to @code{int} in C and @code{bool} in C++.
  358. The following sections describe the elements of the configuration file
  359. grammar in additional detail.
  360. @node Settings, Groups, , Configuration Files
  361. @comment node-name, next, previous, up
  362. @section Settings
  363. A setting has the form:
  364. @i{name} @b{=} @i{value} @b{;}
  365. or:
  366. @i{name} @b{:} @i{value} @b{;}
  367. The trailing semicolon is optional. Whitespace is not significant.
  368. The value may be a scalar value, an array, a group, or a list.
  369. @node Groups, Arrays, Settings, Configuration Files
  370. @comment node-name, next, previous, up
  371. @section Groups
  372. A group has the form:
  373. @b{@{}
  374. @i{settings ...}
  375. @b{@}}
  376. Groups can contain any number of settings, but each setting must have
  377. a unique name within the group.
  378. @node Arrays, Lists, Groups, Configuration Files
  379. @comment node-name, next, previous, up
  380. @section Arrays
  381. An array has the form:
  382. @b{[} @i{value}@b{,} @i{value ...} @b{]}
  383. An array may have zero or more elements, but the elements must all be
  384. scalar values of the same type.
  385. @node Lists, Integer Values, Arrays, Configuration Files
  386. @comment node-name, next, previous, up
  387. @section Lists
  388. A list has the form:
  389. @b{(} @i{value}@b{,} @i{value ...} @b{)}
  390. A list may have zero or more elements, each of which can be a scalar
  391. value, an array, a group, or another list.
  392. @node Integer Values, 64-bit Integer Values, Lists, Configuration Files
  393. @comment node-name, next, previous, up
  394. @section Integer Values
  395. Integers can be represented in one of two ways: as a series of one or
  396. more decimal digits (@samp{0} - @samp{9}), with an optional leading
  397. sign character (@samp{+} or @samp{-}); or as a hexadecimal value
  398. consisting of the characters @samp{0x} followed by a series of one or
  399. more hexadecimal digits (@samp{0} - @samp{9}, @samp{A} - @samp{F},
  400. @samp{a} - @samp{f}).
  401. @node 64-bit Integer Values, Floating Point Values, Integer Values, Configuration Files
  402. @comment node-name, next, previous, up
  403. @section 64-bit Integer Values
  404. Long long (64-bit) integers are represented identically to integers,
  405. except that an 'L' character is appended to indicate a 64-bit
  406. value. For example, @samp{0L} indicates a 64-bit integer value 0.
  407. @node Floating Point Values, Boolean Values, 64-bit Integer Values, Configuration Files
  408. @comment node-name, next, previous, up
  409. @section Floating Point Values
  410. Floating point values consist of a series of one or more digits, one
  411. decimal point, an optional leading sign character (@samp{+} or
  412. @samp{-}), and an optional exponent. An exponent consists of the
  413. letter @samp{E} or @samp{e}, an optional sign character, and a series
  414. of one or more digits.
  415. @node Boolean Values, String Values, Floating Point Values, Configuration Files
  416. @comment node-name, next, previous, up
  417. @section Boolean Values
  418. Boolean values may have one of the following values: @samp{true},
  419. @samp{false}, or any mixed-case variation thereof.
  420. @node String Values, Comments, Boolean Values, Configuration Files
  421. @comment node-name, next, previous, up
  422. @section String Values
  423. @cindex escape sequence
  424. String values consist of arbitrary text delimited by double
  425. quotes. Literal double quotes can be escaped by preceding them with a
  426. backslash: @samp{\"}. The escape sequences @samp{\\}, @samp{\f},
  427. @samp{\n}, @samp{\r}, and @samp{\t} are also recognized, and have the
  428. usual meaning.
  429. In addition, the @samp{\x} escape sequence is supported; this sequence
  430. must be followed by @i{exactly two} hexadecimal digits, which represent an
  431. 8-bit ASCII value. For example, @samp{\xFF} represents the character
  432. with ASCII code 0xFF.
  433. No other escape sequences are currently supported.
  434. Adjacent strings are automatically concatenated, as in C/C++ source
  435. code. This is useful for formatting very long strings as sequences of
  436. shorter strings. For example, the following constructs are equivalent:
  437. @itemize @bullet
  438. @item
  439. @code{"The quick brown fox jumped over the lazy dog."}
  440. @item
  441. @code{"The quick brown fox"} @*
  442. @code{" jumped over the lazy dog."}
  443. @item
  444. @code{"The quick" /* comment */ " brown fox " // another comment} @*
  445. @code{"jumped over the lazy dog."}
  446. @end itemize
  447. @page
  448. @node Comments, Include Directives, String Values, Configuration Files
  449. @comment node-name, next, previous, up
  450. @section Comments
  451. @cindex comment
  452. Three types of comments are allowed within a configuration:
  453. @itemize @bullet
  454. @item Script-style comments. All text beginning with a @samp{#} character
  455. to the end of the line is ignored.
  456. @item C-style comments. All text, including line breaks, between a starting
  457. @samp{/*} sequence and an ending @samp{*/} sequence is ignored.
  458. @item C++-style comments. All text beginning with a @samp{//} sequence to the
  459. end of the line is ignored.
  460. @end itemize
  461. As expected, comment delimiters appearing within quoted strings are
  462. treated as literal text.
  463. Comments are ignored when the configuration is read in, so they are
  464. not treated as part of the configuration. Therefore if the
  465. configuration is written back out to a stream, any comments that were
  466. present in the original configuration will be lost.
  467. @node Include Directives, , Comments, Configuration Files
  468. @comment node-name, next, previous, up
  469. @section Include Directives
  470. @cindex include directive
  471. A configuration file may ``include'' the contents of another file
  472. using an @i{include directive}. This directive has the effect of
  473. inlining the contents of the named file at the point of inclusion.
  474. An include directive must appear on its own line in the input. It has
  475. the form:
  476. @b{@@include "}@i{filename}@b{"}
  477. Any backslashes or double quotes in the filename must be escaped as
  478. @samp{\\} and @samp{\"}, respectively.
  479. For example, consider the following two configuration files:
  480. @cartouche
  481. @smallexample
  482. # file: quote.cfg
  483. quote = "Criticism may not be agreeable, but it is necessary."
  484. " It fulfils the same function as pain in the human"
  485. " body. It calls attention to an unhealthy state of"
  486. " things.\n"
  487. "\t--Winston Churchill";
  488. @end smallexample
  489. @end cartouche
  490. @cartouche
  491. @smallexample
  492. # file: test.cfg
  493. info: @{
  494. name = "Winston Churchill";
  495. @@include "quote.cfg"
  496. country = "UK";
  497. @};
  498. @end smallexample
  499. @end cartouche
  500. Include files may be nested to a maximum of 10 levels; exceeding this
  501. limit results in a parse error.
  502. Like comments, include directives are not part of the configuration
  503. file syntax. They are processed before the configuration itself is
  504. parsed. Therefore, they are not preserved when the configuration is
  505. written back out to a stream. There is presently no support for
  506. programmatically inserting include directives into a configuration.
  507. @node The C API, The C++ API, Configuration Files, Top
  508. @comment node-name, next, previous, up
  509. @chapter The C API
  510. @tindex config_t
  511. @tindex config_setting_t
  512. This chapter describes the C library API. The type @i{config_t}
  513. represents a configuration, and the type @i{config_setting_t} represents
  514. a configuration setting.
  515. The boolean values @code{CONFIG_TRUE} and @code{CONFIG_FALSE} are
  516. macros defined as @code{(1)} and @code{(0)}, respectively.
  517. @deftypefun void config_init (@w{config_t * @var{config}})
  518. @deftypefunx void config_destroy (@w{config_t * @var{config}})
  519. These functions initialize and destroy the configuration object @var{config}.
  520. @code{config_init()} initializes the @i{config_t} structure pointed to by
  521. @var{config} as a new, empty configuration.
  522. @code{config_destroy()} destroys the configuration @var{config},
  523. deallocating all memory associated with the configuration, but does not
  524. attempt to deallocate the @i{config_t} structure itself.
  525. @end deftypefun
  526. @deftypefun int config_read (@w{config_t * @var{config}}, @w{FILE * @var{stream}})
  527. This function reads and parses a configuration from the given
  528. @var{stream} into the configuration object @var{config}. It returns
  529. @code{CONFIG_TRUE} on success, or @code{CONFIG_FALSE} on failure; the
  530. @code{config_error_text()}, @code{config_error_file()},
  531. @code{config_error_line()}, and @code{config_error_type()} functions,
  532. described below, can be used to obtain information about the error.
  533. @end deftypefun
  534. @deftypefun int config_read_file (@w{config_t * @var{config}}, @w{const char * @var{filename}})
  535. This function reads and parses a configuration from the file named
  536. @var{filename} into the configuration object @var{config}. It returns
  537. @code{CONFIG_TRUE} on success, or @code{CONFIG_FALSE} on failure; the
  538. @code{config_error_text()} and @code{config_error_line()} functions,
  539. described below, can be used to obtain information about the error.
  540. @end deftypefun
  541. @deftypefun int config_read_string (@w{config_t * @var{config}}, @w{const char * @var{str}})
  542. This function reads and parses a configuration from the string
  543. @var{str} into the configuration object @var{config}. It returns
  544. @code{CONFIG_TRUE} on success, or @code{CONFIG_FALSE} on failure; the
  545. @code{config_error_text()} and @code{config_error_line()} functions,
  546. described below, can be used to obtain information about the error.
  547. @end deftypefun
  548. @deftypefun void config_write (@w{const config_t * @var{config}}, @w{FILE * @var{stream}})
  549. This function writes the configuration @var{config} to the given
  550. @var{stream}.
  551. @end deftypefun
  552. @deftypefun int config_write_file (@w{config_t * @var{config}}, @w{const char * @var{filename}})
  553. This function writes the configuration @var{config} to the file named
  554. @var{filename}. It returns @code{CONFIG_TRUE} on success, or
  555. @code{CONFIG_FALSE} on failure.
  556. @end deftypefun
  557. @deftypefun {const char *} config_error_text (@w{const config_t * @var{config}})
  558. @deftypefunx {const char *} config_error_file (@w{const config_t * @var{config}})
  559. @deftypefunx int config_error_line (@w{const config_t * @var{config}})
  560. These functions, which are implemented as macros, return the text,
  561. filename, and line number of the parse error, if one occurred during a
  562. call to @code{config_read()}, @code{config_read_string()}, or
  563. @code{config_read_file()}. Storage for the strings returned by
  564. @code{config_error_text()} and @code{config_error_file()} are managed
  565. by the library and released automatically when the configuration is
  566. destroyed; these strings must not be freed by the caller. If the error
  567. occurred in text that was read from a string or stream,
  568. @code{config_error_file()} will return NULL.
  569. @end deftypefun
  570. @deftypefun config_error_t config_error_type (@w{const config_t * @var{config}})
  571. @tindex config_error_t
  572. This function, which is implemented as a macro, returns the type of
  573. error that occurred during the last call to one of the read or write
  574. functions. The @var{config_error_t} type is an enumeration with the
  575. following values: @code{CONFIG_ERR_NONE}, @code{CONFIG_ERR_FILE_IO},
  576. @code{CONFIG_ERR_PARSE}. These represent success, a file I/O error,
  577. and a parsing error, respectively.
  578. @end deftypefun
  579. @deftypefun void config_set_include_dir (@w{config_t *@var{config}}, @w{const char *@var{include_dir}})
  580. @deftypefunx {const char *} config_get_include_dir (@w{const config_t *@var{config}})
  581. @code{config_set_include_dir()} specifies the include directory,
  582. @var{include_dir}, relative to which the files specified in
  583. @samp{@@include} directives will be located for the configuration
  584. @var{config}. By default, there is no include directory, and all
  585. include files are expected to be relative to the current working
  586. directory. If @var{include_dir} is @code{NULL}, the default behavior
  587. is reinstated.
  588. For example, if the include directory is set to @file{/usr/local/etc},
  589. the include directive @samp{@@include "configs/extra.cfg"} would include the
  590. file @file{/usr/local/etc/configs/extra.cfg}.
  591. @code{config_get_include_dir()} returns the current include directory for the
  592. configuration @var{config}, or @code{NULL} if none is set.
  593. @end deftypefun
  594. @deftypefun void config_set_auto_convert (@w{config_t *@var{config}}, @w{int @var{flag}})
  595. @deftypefunx int config_get_auto_convert (@w{const config_t *@var{config}})
  596. @code{config_set_auto_convert()} enables number auto-conversion for
  597. the configuration @var{config} if @var{flag} is non-zero, and disables
  598. it otherwise. When this feature is enabled, an attempt to retrieve a
  599. floating point setting's value into an integer (or vice versa), or
  600. store an integer to a floating point setting's value (or vice versa)
  601. will cause the library to silently perform the necessary conversion
  602. (possibly leading to loss of data), rather than reporting failure. By
  603. default this feature is disabled.
  604. @code{config_get_auto_convert()} returns @code{CONFIG_TRUE} if number
  605. auto-conversion is currently enabled for @var{config}; otherwise it
  606. returns @code{CONFIG_FALSE}.
  607. @end deftypefun
  608. @deftypefun void config_set_default_format (@w{config_t * @var{config}}, @w{short @var{format}})
  609. @deftypefunx short config_get_default_format (@w{config_t * @var{config}})
  610. These functions, which are implemented as macros, set and get the
  611. default external format for settings in the configuration
  612. @var{config}. If a non-default format has not been set for a setting
  613. with @code{config_setting_set_format()}, this configuration-wide
  614. default format will be used instead when that setting is written to a
  615. file or stream.
  616. @end deftypefun
  617. @deftypefun void config_set_tab_width (@w{config_t * @var{config}}, @w{unsigned short @var{width}})
  618. @deftypefunx {unsigned short} config_get_tab_width (@w{const config_t * @var{config}})
  619. These functions, which are implemented as macros, set and get the tab
  620. width for the configuration @var{config}. The tab width affects the
  621. formatting of the configuration when it is written to a file or
  622. stream: each level of nesting is indented by @var{width} spaces, or
  623. by a single tab character if @var{width} is 0. The tab width has no
  624. effect on parsing.
  625. Valid tab widths range from 0 to 15. The default tab width is 2.
  626. @end deftypefun
  627. @deftypefun int config_lookup_int (@w{const config_t * @var{config}}, @w{const char * @var{path}}, @w{int * @var{value}})
  628. @deftypefunx int config_lookup_int64 (@w{const config_t * @var{config}}, @w{const char * @var{path}}, @w{long long * @var{value}})
  629. @deftypefunx int config_lookup_float (@w{const config_t * @var{config}}, @w{const char * @var{path}}, @w{double * @var{value}})
  630. @deftypefunx int config_lookup_bool (@w{const config_t * @var{config}}, @w{const char * @var{path}}, @w{int * @var{value}})
  631. @deftypefunx int config_lookup_string (@w{const config_t * @var{config}}, @w{const char * @var{path}}, @w{const char ** @var{value}})
  632. These functions look up the value of the setting in the configuration
  633. @var{config} specified by the path @var{path}. They store the value of
  634. the setting at @var{value} and return @code{CONFIG_TRUE} on
  635. success. If the setting was not found or if the type of the value did
  636. not match the type requested, they leave the data pointed to by
  637. @var{value} unmodified and return @code{CONFIG_FALSE}.
  638. Storage for the string returned by @code{config_lookup_string()} is
  639. managed by the library and released automatically when the setting is
  640. destroyed or when the setting's value is changed; the string must not
  641. be freed by the caller.
  642. @end deftypefun
  643. @deftypefun {config_setting_t *} config_lookup (@w{const config_t * @var{config}}, @w{const char * @var{path}})
  644. This function locates the setting in the configuration @var{config}
  645. specified by the path @var{path}. It returns a pointer to the
  646. @code{config_setting_t} structure on success, or @code{NULL} if the
  647. setting was not found.
  648. @end deftypefun
  649. @deftypefun int config_setting_get_int (@w{const config_setting_t * @var{setting}})
  650. @deftypefunx {long long} config_setting_get_int64 (@w{const config_setting_t * @var{setting}})
  651. @deftypefunx double config_setting_get_float (@w{const config_setting_t * @var{setting}})
  652. @deftypefunx int config_setting_get_bool (@w{const config_setting_t * @var{setting}})
  653. @deftypefunx {const char *} config_setting_get_string (@w{const config_setting_t * @var{setting}})
  654. These functions return the value of the given @var{setting}. If the
  655. type of the setting does not match the type requested, a 0 or
  656. @code{NULL} value is returned. Storage for the string returned by
  657. @code{config_setting_get_string()} is managed by the library and
  658. released automatically when the setting is destroyed or when the
  659. setting's value is changed; the string must not be freed by the
  660. caller.
  661. @end deftypefun
  662. @deftypefun int config_setting_set_int (@w{config_setting_t * @var{setting}}, @w{int @var{value}})
  663. @deftypefunx int config_setting_set_int64 (@w{config_setting_t * @var{setting}}, @w{long long @var{value}})
  664. @deftypefunx int config_setting_set_float (@w{config_setting_t * @var{setting}}, @w{double @var{value}})
  665. @deftypefunx int config_setting_set_bool (@w{config_setting_t * @var{setting}}, @w{int @var{value}})
  666. @deftypefunx int config_setting_set_string (@w{config_setting_t * @var{setting}}, @w{const char * @var{value}})
  667. These functions set the value of the given @var{setting} to
  668. @var{value}. On success, they return @code{CONFIG_TRUE}. If
  669. the setting does not match the type of the value, they return
  670. @code{CONFIG_FALSE}. @code{config_setting_set_string()} makes a copy
  671. of the passed string @var{value}, so it may be subsequently freed or
  672. modified by the caller without affecting the value of the setting.
  673. @end deftypefun
  674. @deftypefun int config_setting_lookup_int (@w{const config_setting_t * @var{setting}}, @w{const char * @var{name}}, @w{int * @var{value}})
  675. @deftypefunx int config_setting_lookup_int64 (@w{const config_setting_t * @var{setting}}, @w{const char * @var{name}}, @w{long long * @var{value}})
  676. @deftypefunx int config_setting_lookup_float (@w{const config_setting_t * @var{setting}}, @w{const char * @var{name}}, @w{double * @var{value}})
  677. @deftypefunx int config_setting_lookup_bool (@w{const config_setting_t * @var{setting}}, @w{const char * @var{name}}, @w{int * @var{value}})
  678. @deftypefunx int config_setting_lookup_string (@w{const config_setting_t * @var{setting}}, @w{const char * @var{name}}, @w{const char ** @var{value}})
  679. These functions look up the value of the child setting named
  680. @var{name} of the setting @var{setting}. They store the value at
  681. @var{value} and return @code{CONFIG_TRUE} on success. If the setting
  682. was not found or if the type of the value did not match the type
  683. requested, they leave the data pointed to by @var{value} unmodified
  684. and return @code{CONFIG_FALSE}.
  685. Storage for the string returned by @code{config_setting_lookup_string()} is
  686. managed by the library and released automatically when the setting is
  687. destroyed or when the setting's value is changed; the string must not
  688. be freed by the caller.
  689. @end deftypefun
  690. @deftypefun short config_setting_get_format (@w{config_setting_t * @var{setting}})
  691. @deftypefunx int config_setting_set_format (@w{config_setting_t * @var{setting}}, @w{short @var{format}})
  692. These functions get and set the external format for the setting @var{setting}.
  693. @tindex SettingFormat
  694. @cindex format
  695. The @var{format} must be one of the constants
  696. @code{CONFIG_FORMAT_DEFAULT} or @code{CONFIG_FORMAT_HEX}. All settings
  697. support the @code{CONFIG_FORMAT_DEFAULT} format. The
  698. @code{CONFIG_FORMAT_HEX} format specifies hexadecimal formatting for
  699. integer values, and hence only applies to settings of type
  700. @code{CONFIG_TYPE_INT} and @code{CONFIG_TYPE_INT64}. If @var{format}
  701. is invalid for the given setting, it is ignored.
  702. If a non-default format has not been set for the setting, @code{config_setting_get_format()} returns the default format for the configuration, as set by @code{config_set_default_format()}.
  703. @code{config_setting_set_format()} returns @code{CONFIG_TRUE} on
  704. success and @code{CONFIG_FALSE} on failure.
  705. @end deftypefun
  706. @deftypefun {config_setting_t *} config_setting_get_member (@w{config_setting_t * @var{setting}}, @w{const char * @var{name}})
  707. This function fetches the child setting named @var{name} from the group
  708. @var{setting}. It returns the requested setting on success, or
  709. @code{NULL} if the setting was not found or if @var{setting} is not a
  710. group.
  711. @end deftypefun
  712. @deftypefun {config_setting_t *} config_setting_get_elem (@w{const config_setting_t * @var{setting}}, @w{unsigned int @var{index}})
  713. This function fetches the element at the given index @var{index} in the
  714. setting @var{setting}, which must be an array, list, or group. It returns the
  715. requested setting on success, or @code{NULL} if @var{index} is out of
  716. range or if @var{setting} is not an array, list, or group.
  717. @end deftypefun
  718. @deftypefun int config_setting_get_int_elem (@w{const config_setting_t * @var{setting}}, @w{int @var{index}})
  719. @deftypefunx {long long} config_setting_get_int64_elem (@w{const config_setting_t * @var{setting}}, @w{int @var{index}})
  720. @deftypefunx double config_setting_get_float_elem (@w{const config_setting_t * @var{setting}}, @w{int @var{index}})
  721. @deftypefunx int config_setting_get_bool_elem (@w{const config_setting_t * @var{setting}}, @w{int @var{index}})
  722. @deftypefunx {const char *} config_setting_get_string_elem (@w{const config_setting_t * @var{setting}}, @w{int @var{index}})
  723. These functions return the value at the specified index @var{index} in the
  724. setting @var{setting}. If the setting is not an array or list, or if
  725. the type of the element does not match the type requested, or if
  726. @var{index} is out of range, they return 0 or @code{NULL}. Storage for
  727. the string returned by @code{config_setting_get_string_elem()} is
  728. managed by the library and released automatically when the setting is
  729. destroyed or when its value is changed; the string must not be freed
  730. by the caller.
  731. @end deftypefun
  732. @deftypefun {config_setting_t *} config_setting_set_int_elem (@w{config_setting_t * @var{setting}}, @w{int @var{index}}, @w{int @var{value}})
  733. @deftypefunx {config_setting_t *} config_setting_set_int64_elem (@w{config_setting_t * @var{setting}}, @w{int @var{index}}, @w{long long @var{value}})
  734. @deftypefunx {config_setting_t *} config_setting_set_float_elem (@w{config_setting_t * @var{setting}}, @w{int @var{index}}, @w{double @var{value}})
  735. @deftypefunx {config_setting_t *} config_setting_set_bool_elem (@w{config_setting_t * @var{setting}}, @w{int @var{index}}, @w{int @var{value}})
  736. @deftypefunx {config_setting_t *} config_setting_set_string_elem (@w{config_setting_t * @var{setting}}, @w{int @var{index}}, @w{const char * @var{value}})
  737. These functions set the value at the specified index @var{index} in the
  738. setting @var{setting} to @var{value}. If @var{index} is negative, a
  739. new element is added to the end of the array or list. On success,
  740. these functions return a pointer to the setting representing the
  741. element. If the setting is not an array or list, or if the setting is
  742. an array and the type of the array does not match the type of the
  743. value, or if @var{index} is out of range, they return
  744. @code{NULL}. @code{config_setting_set_string_elem()} makes a copy of
  745. the passed string @var{value}, so it may be subsequently freed or
  746. modified by the caller without affecting the value of the setting.
  747. @end deftypefun
  748. @deftypefun {config_setting_t *} config_setting_add (@w{config_setting_t * @var{parent}}, @w{const char * @var{name}}, @w{int @var{type}})
  749. This function adds a new child setting or element to the setting
  750. @var{parent}, which must be a group, array, or list. If @var{parent}
  751. is an array or list, the @var{name} parameter is ignored and may be
  752. @code{NULL}.
  753. The function returns the new setting on success, or @code{NULL} if
  754. @var{parent} is not a group, array, or list; or if there is already a
  755. child setting of @var{parent} named @var{name}; or if @var{type} is
  756. invalid. If @var{type} is a scalar type, the new setting will have a
  757. default value of 0, 0.0, @code{false}, or @code{NULL}, as appropriate.
  758. @end deftypefun
  759. @deftypefun int config_setting_remove (@w{config_setting_t * @var{parent}}, @w{const char * @var{name}})
  760. This function removes and destroys the setting named @var{name} from
  761. the parent setting @var{parent}, which must be a group. Any child
  762. settings of the setting are recursively destroyed as well.
  763. The function returns @code{CONFIG_TRUE} on success. If @var{parent} is
  764. not a group, or if it has no setting with the given name, it returns
  765. @code{CONFIG_FALSE}.
  766. @end deftypefun
  767. @deftypefun int config_setting_remove_elem (@w{config_setting_t * @var{parent}}, @w{unsigned int @var{index}})
  768. This function removes the child setting at the given index @var{index} from
  769. the setting @var{parent}, which must be a group, list, or array. Any
  770. child settings of the removed setting are recursively destroyed as
  771. well.
  772. The function returns @code{CONFIG_TRUE} on success. If @var{parent} is
  773. not a group, list, or array, or if @var{index} is out of range, it returns
  774. @code{CONFIG_FALSE}.
  775. @end deftypefun
  776. @deftypefun {config_setting_t *} config_root_setting (@w{const config_t * @var{config}})
  777. This function returns the root setting for the configuration
  778. @var{config}. The root setting is a group.
  779. @end deftypefun
  780. @deftypefun {const char *} config_setting_name (@w{const config_setting_t * @var{setting}})
  781. This function returns the name of the given @var{setting}, or
  782. @code{NULL} if the setting has no name. Storage for the returned
  783. string is managed by the library and released automatically when the
  784. setting is destroyed; the string must not be freed by the caller.
  785. @end deftypefun
  786. @deftypefun {config_setting_t *} config_setting_parent (@w{const config_setting_t * @var{setting}})
  787. This function returns the parent setting of the given @var{setting},
  788. or @code{NULL} if @var{setting} is the root setting.
  789. @end deftypefun
  790. @deftypefun int config_setting_is_root (@w{const config_setting_t * @var{setting}})
  791. This function returns @code{CONFIG_TRUE} if the given @var{setting} is
  792. the root setting, and @code{CONFIG_FALSE} otherwise.
  793. @end deftypefun
  794. @deftypefun int config_setting_index (@w{const config_setting_t * @var{setting}})
  795. This function returns the index of the given @var{setting} within its
  796. parent setting. If @var{setting} is the root setting, this function
  797. returns -1.
  798. @end deftypefun
  799. @deftypefun int config_setting_length (@w{const config_setting_t * @var{setting}})
  800. This function returns the number of settings in a group, or the number of
  801. elements in a list or array. For other types of settings, it returns
  802. 0.
  803. @end deftypefun
  804. @deftypefun int config_setting_type (@w{const config_setting_t * @var{setting}})
  805. This function returns the type of the given @var{setting}. The return
  806. value is one of the constants
  807. @code{CONFIG_TYPE_INT}, @code{CONFIG_TYPE_INT64}, @code{CONFIG_TYPE_FLOAT},
  808. @code{CONFIG_TYPE_STRING}, @code{CONFIG_TYPE_BOOL},
  809. @code{CONFIG_TYPE_ARRAY}, @code{CONFIG_TYPE_LIST}, or @code{CONFIG_TYPE_GROUP}.
  810. @end deftypefun
  811. @deftypefun int config_setting_is_group (@w{const config_setting_t * @var{setting}})
  812. @deftypefunx int config_setting_is_array (@w{const config_setting_t * @var{setting}})
  813. @deftypefunx int config_setting_is_list (@w{const config_setting_t * @var{setting}})
  814. These convenience functions, which are implemented as macros, test if
  815. the setting @var{setting} is of a given type. They return
  816. @code{CONFIG_TRUE} or @code{CONFIG_FALSE}.
  817. @end deftypefun
  818. @deftypefun int config_setting_is_aggregate (@w{const config_setting_t * @var{setting}})
  819. @deftypefunx int config_setting_is_scalar (@w{const config_setting_t * @var{setting}})
  820. @deftypefunx int config_setting_is_number (@w{const config_setting_t * @var{setting}})
  821. @cindex aggregate value
  822. These convenience functions, which are implemented as macros, test if
  823. the setting @var{setting} is of an aggregate type (a group, array, or
  824. list), of a scalar type (integer, 64-bit integer, floating point,
  825. boolean, or string), and of a number (integer, 64-bit integer, or
  826. floating point), respectively. They return @code{CONFIG_TRUE} or
  827. @code{CONFIG_FALSE}.
  828. @end deftypefun
  829. @deftypefun {const char *} config_setting_source_file (@w{const config_setting_t * @var{setting}})
  830. This function returns the name of the file from which the setting
  831. @var{setting} was read, or NULL if the setting was not read from a
  832. file. This information is useful for reporting application-level
  833. errors. Storage for the returned string is managed by the library and
  834. released automatically when the configuration is destroyed; the
  835. string must not be freed by the caller.
  836. @end deftypefun
  837. @deftypefun {unsigned int} config_setting_source_line (@w{const config_setting_t * @var{setting}})
  838. This function returns the line number of the configuration file or
  839. stream at which the setting @var{setting} was read, or 0 if no line
  840. number is available. This information is useful for reporting
  841. application-level errors.
  842. @end deftypefun
  843. @deftypefun void config_setting_set_hook (@w{config_setting_t * @var{setting}}, @w{void * @var{hook}})
  844. @deftypefunx {void *} config_setting_get_hook (@w{const config_setting_t * @var{setting}})
  845. These functions make it possible to attach arbitrary data to each
  846. setting structure, for instance a ``wrapper'' or ``peer'' object written in
  847. another programming language. The destructor function, if one has been
  848. supplied via a call to @code{config_set_destructor()}, will be called
  849. by the library to dispose of this data when the setting itself is
  850. destroyed. There is no default destructor.
  851. @end deftypefun
  852. @deftypefun void config_set_destructor (@w{config_t * @var{config}}, @w{void (* @var{destructor})(void *)})
  853. This function assigns the destructor function @var{destructor} for the
  854. configuration @var{config}. This function accepts a single @code{void
  855. *} argument and has no return value. See
  856. @code{config_setting_set_hook()} above for more information.
  857. @end deftypefun
  858. @node The C++ API, Example Programs, The C API, Top
  859. @comment node-name, next, previous, up
  860. @chapter The C++ API
  861. @tindex Config
  862. @tindex Setting
  863. This chapter describes the C++ library API. The class @code{Config}
  864. represents a configuration, and the class @code{Setting} represents a
  865. configuration setting. Note that by design, neither of these classes
  866. provides a public copy constructor or assignment operator. Therefore,
  867. instances of these classes may only be passed between functions via
  868. references or pointers.
  869. @tindex ConfigException
  870. The library defines a group of exceptions, all of which extend the
  871. common base exception @code{ConfigException}.
  872. @tindex SettingTypeException
  873. A @code{SettingTypeException} is thrown when the type of a setting's
  874. value does not match the type requested.
  875. @tindex SettingNotFoundException
  876. A @code{SettingNotFoundException} is thrown when a setting is not found.
  877. @tindex SettingNameException
  878. A @code{SettingNameException} is thrown when an attempt is made to add
  879. a new setting with a non-unique or invalid name.
  880. @tindex ParseException
  881. A @code{ParseException} is thrown when a parse error occurs while
  882. reading a configuration from a stream.
  883. @tindex FileIOException
  884. A @code{FileIOException} is thrown when an I/O error occurs while
  885. reading/writing a configuration from/to a file.
  886. @tindex SettingException
  887. @code{SettingTypeException}, @code{SettingNotFoundException}, and
  888. @code{SettingNameException} all extend the common base
  889. exception @code{SettingException}, which provides the following method:
  890. @deftypemethod SettingException {const char *} getPath ()
  891. Returns the path to the setting associated with the exception, or
  892. @code{NULL} if there is no applicable path.
  893. @end deftypemethod
  894. The remainder of this chapter describes the methods for manipulating
  895. configurations and configuration settings.
  896. @deftypemethod Config {} Config ()
  897. @deftypemethodx Config {} ~Config ()
  898. These methods create and destroy @code{Config} objects.
  899. @end deftypemethod
  900. @deftypemethod Config void read (@w{FILE * @var{stream}})
  901. @deftypemethodx Config void write (@w{FILE * @var{stream}})
  902. The @code{read()} method reads and parses a configuration from the given
  903. @var{stream}. A @code{ParseException} is thrown if a parse error occurs.
  904. The @code{write()} method writes the configuration to the given @var{stream}.
  905. @end deftypemethod
  906. @deftypemethod Config void readFile (@w{const char * @var{filename}})
  907. @deftypemethodx Config void writeFile (@w{const char * @var{filename}})
  908. The @code{readFile()} method reads and parses a configuration from the
  909. file named @var{filename}. A @code{ParseException} is thrown if a
  910. parse error occurs. A @code{FileIOException} is thrown if the file
  911. cannot be read.
  912. The @code{writeFile()} method writes the configuration to the file
  913. named @var{filename}. A @code{FileIOException} is thrown if the file cannot
  914. be written.
  915. @end deftypemethod
  916. @deftypemethod Config void readString (@w{const char * @var{str}})
  917. @deftypemethodx Config void readString (@w{const std::string &@var{str}})
  918. These methods read and parse a configuration from the string
  919. @var{str}. A @code{ParseException} is thrown if a parse error occurs.
  920. @end deftypemethod
  921. @deftypemethod ParseException {const char *} getError ()
  922. @deftypemethodx ParseException {const char *} getFile ()
  923. @deftypemethodx ParseException int getLine ()
  924. If a call to @code{readFile()}, @code{readString()}, or @code{read()}
  925. resulted in a @code{ParseException}, these methods can be called on
  926. the exception object to obtain the text, filename, and line number of
  927. the parse error. Storage for the strings returned by @code{getError()}
  928. and @code{getFile()} are managed by the library; the strings must not
  929. be freed by the caller.
  930. @end deftypemethod
  931. @deftypemethod Config void setIncludeDir (@w{const char *@var{includeDir}})
  932. @deftypemethodx Config {const char *} getIncludeDir ()
  933. @code{setIncludeDir()} specifies the include directory,
  934. @var{includeDir}, relative to which the files specified in
  935. @samp{@@include} directives will be located for the configuration. By
  936. default, there is no include directory, and all include files are
  937. expected to be relative to the current working directory. If
  938. @var{includeDir} is @code{NULL}, the default behavior is reinstated.
  939. For example, if the include directory is set to @file{/usr/local/etc},
  940. the include directive @samp{@@include "configs/extra.cfg"} would include the
  941. file @file{/usr/local/etc/configs/extra.cfg}.
  942. @code{getIncludeDir()} returns the current include directory for the
  943. configuration, or @code{NULL} if none is set.
  944. @end deftypemethod
  945. @deftypemethod Config void setAutoConvert (bool @var{flag})
  946. @deftypemethodx Config bool getAutoConvert ()
  947. @code{setAutoConvert()} enables number auto-conversion for the
  948. configuration if @var{flag} is @code{true}, and disables it
  949. otherwise. When this feature is enabled, an attempt to assign a
  950. floating point setting to an integer (or vice versa), or
  951. assign an integer to a floating point setting (or vice versa) will
  952. cause the library to silently perform the necessary conversion
  953. (possibly leading to loss of data), rather than throwing a
  954. @code{SettingTypeException}. By default this feature is disabled.
  955. @code{getAutoConvert()} returns @code{true} if number auto-conversion
  956. is currently enabled for the configuration; otherwise it returns
  957. @code{false}.
  958. @end deftypemethod
  959. @deftypemethod Config void setDefaultFormat (@w{Setting::Format @var{format}})
  960. @deftypemethodx Config Setting::Format getDefaultFormat ()
  961. These methods set and get the default external format for settings in
  962. the configuration. If a non-default format has not been set for a
  963. setting with @code{Setting::setFormat()}, this configuration-wide
  964. default format will be used instead when that setting is written to a
  965. file or stream.
  966. @end deftypemethod
  967. @deftypemethod Config void setTabWidth (@w{unsigned short @var{width}})
  968. @deftypemethodx Config {unsigned short} getTabWidth ()
  969. These methods set and get the tab width for the configuration. The tab
  970. width affects the formatting of the configuration when it is written
  971. to a file or stream: each level of nesting is indented by @var{width}
  972. spaces, or by a single tab character if @var{width} is 0. The tab
  973. width has no effect on parsing.
  974. Valid tab widths range from 0 to 15. The default tab width is 2.
  975. @end deftypemethod
  976. @deftypemethod Config {Setting &} getRoot ()
  977. This method returns the root setting for the configuration, which is a group.
  978. @end deftypemethod
  979. @deftypemethod Config {Setting &} lookup (@w{const std::string &@var{path}})
  980. @deftypemethodx Config {Setting &} lookup (@w{const char * @var{path}})
  981. These methods locate the setting specified by the path @var{path}. If
  982. the requested setting is not found, a @code{SettingNotFoundException} is
  983. thrown.
  984. @end deftypemethod
  985. @deftypemethod Config bool exists (@w{const std::string &@var{path}})
  986. @deftypemethodx Config bool exists (@w{const char *@var{path}})
  987. These methods test if a setting with the given @var{path} exists in
  988. the configuration. They return @code{true} if the setting exists, and
  989. @code{false} otherwise. These methods do not throw exceptions.
  990. @end deftypemethod
  991. @deftypemethod Config bool lookupValue (@w{const char *@var{path}}, @w{bool &@var{value}})
  992. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{bool &@var{value}})
  993. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{int &@var{value}})
  994. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{int &@var{value}})
  995. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{unsigned int &@var{value}})
  996. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{unsigned int &@var{value}})
  997. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{long long &@var{value}})
  998. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{long long &@var{value}})
  999. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{float &@var{value}})
  1000. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{float &@var{value}})
  1001. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{double &@var{value}})
  1002. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{double &@var{value}})
  1003. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{const char *&@var{value}})
  1004. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{const char *&@var{value}})
  1005. @deftypemethodx Config bool lookupValue (@w{const char *@var{path}}, @w{std::string &@var{value}})
  1006. @deftypemethodx Config bool lookupValue (@w{const std::string &@var{path}}, @w{std::string &@var{value}})
  1007. These are convenience methods for looking up the value of a setting
  1008. with the given @var{path}. If the setting is found and is of an
  1009. appropriate type, the value is stored in @var{value} and the method
  1010. returns @code{true}. Otherwise, @var{value} is left unmodified and the
  1011. method returns @code{false}. These methods do not throw exceptions.
  1012. Storage for @w{@i{const char *}} values is managed by the library and
  1013. released automatically when the setting is destroyed or when its value
  1014. is changed; the string must not be freed by the caller. For safety and
  1015. convenience, always assigning string values to a @code{std::string} is
  1016. suggested.
  1017. Since these methods have boolean return values and do not throw
  1018. exceptions, they can be used within boolean logic expressions. The following
  1019. example presents a concise way to look up three values at once and
  1020. perform error handling if any of them are not found or are of the
  1021. wrong type:
  1022. @sp 1
  1023. @cartouche
  1024. @smallexample
  1025. int var1;
  1026. double var2;
  1027. const char *var3;
  1028. if(config.lookupValue("values.var1", var1)
  1029. && config.lookupValue("values.var2", var2)
  1030. && config.lookupValue("values.var3", var3))
  1031. @{
  1032. // use var1, var2, var3
  1033. @}
  1034. else
  1035. @{
  1036. // error handling here
  1037. @}
  1038. @end smallexample
  1039. @end cartouche
  1040. This approach also takes advantage of the short-circuit evaluation rules
  1041. of C++, e.g., if the first lookup fails (returning @code{false}), the
  1042. remaining lookups are skipped entirely.
  1043. @end deftypemethod
  1044. @deftypemethod Setting {} {operator bool ()}
  1045. @deftypemethodx Setting {} {operator int ()}
  1046. @deftypemethodx Setting {} {operator unsigned int ()}
  1047. @deftypemethodx Setting {} {operator long ()}
  1048. @deftypemethodx Setting {} {operator unsigned long ()}
  1049. @deftypemethodx Setting {} {operator long long ()}
  1050. @deftypemethodx Setting {} {operator unsigned long long ()}
  1051. @deftypemethodx Setting {} {operator float ()}
  1052. @deftypemethodx Setting {} {operator double ()}
  1053. @deftypemethodx Setting {} {operator const char * ()}
  1054. @deftypemethodx Setting {} {operator std::string ()}
  1055. @deftypemethodx Setting {const char *} c_str ()
  1056. These cast operators allow a @code{Setting} object to be assigned to a
  1057. variable of type @i{bool} if it is of type @code{TypeBoolean};
  1058. @i{int}, @i{unsigned int}; @code{long long} or @code{unsigned long long} if
  1059. it is of type @code{TypeInt64}, @i{float} or @i{double} if it is of type
  1060. @code{TypeFloat}; or @w{@i{const char *}} or @i{std::string} if it is
  1061. of type @code{TypeString}.
  1062. Values of type @code{TypeInt} or @code{TypeInt64} may be assigned to
  1063. variables of type @i{long}, or @i{unsigned long}, depending on the
  1064. sizes of those types on the host system.
  1065. Storage for @w{@i{const char *}} return values is managed by the
  1066. library and released automatically when the setting is destroyed or
  1067. when its value is changed; the string must not be freed by the
  1068. caller. For safety and convenience, always assigning string return
  1069. values to a @code{std::string} is suggested.
  1070. The following examples demonstrate this usage:
  1071. @cartouche
  1072. @smallexample
  1073. long width = config.lookup("application.window.size.w");
  1074. bool splashScreen = config.lookup("application.splash_screen");
  1075. std::string title = config.lookup("application.window.title");
  1076. @end smallexample
  1077. @end cartouche
  1078. Note that certain conversions can lead to loss of precision or
  1079. clipping of values, e.g., assigning a negative value to an @i{unsigned
  1080. int} (in which case the value will be treated as 0), or a
  1081. double-precision value to a @i{float}. The library does not treat
  1082. these lossy conversions as errors.
  1083. Perhaps surprisingly, the following code in particular will cause a
  1084. compiler error:
  1085. @cartouche
  1086. @smallexample
  1087. std::string title;
  1088. .
  1089. .
  1090. .
  1091. title = config.lookup("application.window.title");
  1092. @end smallexample
  1093. @end cartouche
  1094. This is because the assignment operator of @code{std::string} is being
  1095. invoked with a @code{Setting &} as an argument. The compiler is unable
  1096. to make an implicit conversion because both the @code{const char *}
  1097. and the @code{std::string} cast operators of @code{Setting} are
  1098. equally appropriate. This is not a bug in @i{libconfig}; providing
  1099. only the @code{const char *} cast operator would resolve this
  1100. particular ambiguity, but would cause assignments to
  1101. @code{std::string} like the one in the previous example to produce a
  1102. compiler error. (To understand why, see section 11.4.1 of @i{The C++
  1103. Programming Language}.)
  1104. The solution to this problem is to use an explicit conversion that
  1105. avoids the construction of an intermediate @code{std::string} object,
  1106. as follows:
  1107. @cartouche
  1108. @smallexample
  1109. std::string title;
  1110. .
  1111. .
  1112. .
  1113. title = (const char *)config.lookup("application.window.title");
  1114. @end smallexample
  1115. @end cartouche
  1116. Or, alternatively, use the @code{c_str()} method, which has the same effect:
  1117. @cartouche
  1118. @smallexample
  1119. std::string title;
  1120. .
  1121. .
  1122. .
  1123. title = config.lookup("application.window.title").c_str();
  1124. @end smallexample
  1125. @end cartouche
  1126. If the assignment is invalid due to a type mismatch, a
  1127. @code{SettingTypeException} is thrown.
  1128. @end deftypemethod
  1129. @deftypemethod Setting {Setting &} operator= (@w{bool @var{value}})
  1130. @deftypemethodx Setting {Setting &} operator= (@w{int @var{value}})
  1131. @deftypemethodx Setting {Setting &} operator= (@w{long @var{value}})
  1132. @deftypemethodx Setting {Setting &} operator= (@w{const long long &@var{value}})
  1133. @deftypemethodx Setting {Setting &} operator= (@w{float @var{value}})
  1134. @deftypemethodx Setting {Setting &} operator= (@w{const double &@var{value}})
  1135. @deftypemethodx Setting {Setting &} operator= (@w{const char *@var{value}})
  1136. @deftypemethodx Setting {Setting &} operator= (@w{const std::string &@var{value}})
  1137. These assignment operators allow values of type @i{bool}, @i{int},
  1138. @i{long}, @i{long long}, @i{float}, @i{double}, @i{const char *}, and
  1139. @i{std::string} to be assigned to a setting. In the case of strings,
  1140. the library makes a copy of the passed string @var{value}, so it may
  1141. be subsequently freed or modified by the caller without affecting the
  1142. value of the setting.
  1143. The following example code looks up a (presumably) integer setting
  1144. and changes its value:
  1145. @cartouche
  1146. @smallexample
  1147. Setting &setting = config.lookup("application.window.size.w");
  1148. setting = 1024;
  1149. @end smallexample
  1150. @end cartouche
  1151. If the assignment is invalid due to a type mismatch, a
  1152. @code{SettingTypeException} is thrown.
  1153. @end deftypemethod
  1154. @deftypemethod Setting {Setting &} {operator[]} (@w{int @var{index}})
  1155. @deftypemethodx Setting {Setting &} {operator[]} (@w{const std::string &@var{name}})
  1156. @deftypemethodx Setting {Setting &} {operator[]} (@w{const char *@var{name}})
  1157. A @code{Setting} object may be subscripted with an integer index
  1158. @var{index} if it is an array or list, or with either a string
  1159. @var{name} or an integer index @var{index} if it is a group. For example,
  1160. the following code would produce the string @samp{Last Name} when
  1161. applied to the example configuration in @ref{Configuration Files}.
  1162. @cartouche
  1163. @smallexample
  1164. Setting& setting = config.lookup("application.misc");
  1165. const char *s = setting["columns"][0];
  1166. @end smallexample
  1167. @end cartouche
  1168. If the setting is not an array, list, or group, a
  1169. @code{SettingTypeException} is thrown. If the subscript (@var{index}
  1170. or @var{name}) does not refer to a valid element, a
  1171. @code{SettingNotFoundException} is thrown.
  1172. Iterating over a group's child settings with an integer index will
  1173. return the settings in the same order that they appear in the
  1174. configuration.
  1175. @end deftypemethod
  1176. @deftypemethod Setting bool lookupValue (@w{const char *@var{name}}, @w{bool &@var{value}})
  1177. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{bool &@var{value}})
  1178. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{int &@var{value}})
  1179. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{int &@var{value}})
  1180. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{unsigned int &@var{value}})
  1181. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{unsigned int &@var{value}})
  1182. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{long long &@var{value}})
  1183. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{long long &@var{value}})
  1184. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{unsigned long long &@var{value}})
  1185. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{unsigned long long &@var{value}})
  1186. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{float &@var{value}})
  1187. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{float &@var{value}})
  1188. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{double &@var{value}})
  1189. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{double &@var{value}})
  1190. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{const char *&@var{value}})
  1191. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{const char *&@var{value}})
  1192. @deftypemethodx Setting bool lookupValue (@w{const char *@var{name}}, @w{std::string &@var{value}})
  1193. @deftypemethodx Setting bool lookupValue (@w{const std::string &@var{name}}, @w{std::string &@var{value}})
  1194. These are convenience methods for looking up the value of a child setting
  1195. with the given @var{name}. If the setting is found and is of an
  1196. appropriate type, the value is stored in @var{value} and the method
  1197. returns @code{true}. Otherwise, @var{value} is left unmodified and the
  1198. method returns @code{false}. These methods do not throw exceptions.
  1199. Storage for @w{@i{const char *}} values is managed by the library and
  1200. released automatically when the setting is destroyed or when its value
  1201. is changed; the string must not be freed by the caller. For safety and
  1202. convenience, always assigning string values to a @code{std::string} is
  1203. suggested.
  1204. Since these methods have boolean return values and do not throw
  1205. exceptions, they can be used within boolean logic expressions. The following
  1206. example presents a concise way to look up three values at once and
  1207. perform error handling if any of them are not found or are of the
  1208. wrong type:
  1209. @sp 1
  1210. @cartouche
  1211. @smallexample
  1212. int var1;
  1213. double var2;
  1214. const char *var3;
  1215. if(setting.lookupValue("var1", var1)
  1216. && setting.lookupValue("var2", var2)
  1217. && setting.lookupValue("var3", var3))
  1218. @{
  1219. // use var1, var2, var3
  1220. @}
  1221. else
  1222. @{
  1223. // error handling here
  1224. @}
  1225. @end smallexample
  1226. @end cartouche
  1227. This approach also takes advantage of the short-circuit evaluation
  1228. rules of C++, e.g., if the first lookup fails (returning @code{false}), the
  1229. remaining lookups are skipped entirely.
  1230. @end deftypemethod
  1231. @deftypemethod Setting {Setting &} add (@w{const std::string &@var{name}}, @w{Setting::Type @var{type}})
  1232. @deftypemethodx Setting {Setting &} add (@w{const char *@var{name}}, @w{Setting::Type @var{type}})
  1233. These methods add a new child setting with the given @var{name} and
  1234. @var{type} to the setting, which must be a group. They return a
  1235. reference to the new setting. If the setting already has a child
  1236. setting with the given name, or if the name is invalid, a
  1237. @code{SettingNameException} is thrown. If the setting is not a group,
  1238. a @code{SettingTypeException} is thrown.
  1239. Once a setting has been created, neither its name nor type can be
  1240. changed.
  1241. @end deftypemethod
  1242. @deftypemethod Setting {Setting &} add (@w{Setting::Type @var{type}})
  1243. This method adds a new element to the setting, which must be of type
  1244. @code{TypeArray} or @code{TypeList}. If the setting is an array which
  1245. currently has zero elements, the @var{type} parameter (which must be
  1246. @code{TypeInt}, @code{TypeInt64}, @code{TypeFloat}, @code{TypeBool},
  1247. or @code{TypeString}) determines the type for the array; otherwise it
  1248. must match the type of the existing elements in the array.
  1249. The method returns the new setting on success. If @var{type} is a
  1250. scalar type, the new setting will have a default value of 0, 0.0,
  1251. @code{false}, or @code{NULL}, as appropriate.
  1252. The method throws a @code{SettingTypeException} if the setting is not
  1253. an array or list, or if @var{type} is invalid.
  1254. @end deftypemethod
  1255. @deftypemethod Setting void remove (@w{const std::string &@var{name}})
  1256. @deftypemethodx Setting void remove (@w{const char *@var{name}})
  1257. These methods remove the child setting with the given @var{name} from
  1258. the setting, which must be a group. Any child settings of the removed
  1259. setting are recursively destroyed as well.
  1260. If the setting is not a group, a @code{SettingTypeException} is
  1261. thrown. If the setting does not have a child setting with the given
  1262. name, a @code{SettingNotFoundException} is thrown.
  1263. @end deftypemethod
  1264. @deftypemethod Setting void remove (@w{unsigned int @var{index}})
  1265. This method removes the child setting at the given index @var{index} from
  1266. the setting, which must be a group, list, or array. Any child settings
  1267. of the removed setting are recursively destroyed as well.
  1268. If the setting is not a group, list, or array, a
  1269. @code{SettingTypeException} is thrown. If @var{index} is out of range,
  1270. a @code{SettingNotFoundException} is thrown.
  1271. @end deftypemethod
  1272. @deftypemethod Setting {const char *} getName ()
  1273. This method returns the name of the setting, or @code{NULL} if the
  1274. setting has no name. Storage for the returned string is managed by the
  1275. library and released automatically when the setting is destroyed; the
  1276. string must not be freed by the caller. For safety and convenience,
  1277. consider assigning the return value to a @code{std::string}.
  1278. @end deftypemethod
  1279. @deftypemethod Setting {std::string} getPath ()
  1280. This method returns the complete dot-separated path to the
  1281. setting. Settings which do not have a name (list and array elements)
  1282. are represented by their index in square brackets.
  1283. @end deftypemethod
  1284. @deftypemethod Setting {Setting &} getParent ()
  1285. This method returns the parent setting of the setting. If the setting
  1286. is the root setting, a @code{SettingNotFoundException} is thrown.
  1287. @end deftypemethod
  1288. @deftypemethod Setting bool isRoot ()
  1289. This method returns @code{true} if the setting is the root setting, and
  1290. @code{false} otherwise.
  1291. @end deftypemethod
  1292. @deftypemethod Setting int getIndex ()
  1293. This method returns the index of the setting within its parent
  1294. setting. When applied to the root setting, this method returns -1.
  1295. @end deftypemethod
  1296. @deftypemethod Setting Setting::Type getType ()
  1297. @tindex Setting::Type
  1298. This method returns the type of the setting. The
  1299. @code{Setting::Type} enumeration consists of the following constants:
  1300. @code{TypeInt}, @code{TypeInt64}, @code{TypeFloat}, @code{TypeString},
  1301. @code{TypeBoolean}, @code{TypeArray}, @code{TypeList}, and
  1302. @code{TypeGroup}.
  1303. @end deftypemethod
  1304. @deftypemethod Setting Setting::Format getFormat ()
  1305. @deftypemethodx Setting void setFormat (@w{Setting::Format @var{format}})
  1306. These methods get and set the external format for the setting.
  1307. @tindex Setting::Format
  1308. The @var{Setting::Format} enumeration consists of the following
  1309. constants: @code{FormatDefault} and @code{FormatHex}. All settings
  1310. support the @code{FormatDefault} format. The @code{FormatHex} format
  1311. specifies hexadecimal formatting for integer values, and hence only
  1312. applies to settings of type @code{TypeInt} and @code{TypeInt64}. If
  1313. @var{format} is invalid for the given setting, it is ignored.
  1314. @end deftypemethod
  1315. @deftypemethod Setting bool exists (@w{const std::string &@var{name}})
  1316. @deftypemethodx Setting bool exists (@w{const char *@var{name}})
  1317. These methods test if the setting has a child setting with the given
  1318. @var{name}. They return @code{true} if the setting exists, and
  1319. @code{false} otherwise. These methods do not throw exceptions.
  1320. @end deftypemethod
  1321. @deftypemethod Setting int getLength ()
  1322. This method returns the number of settings in a group, or the number of
  1323. elements in a list or array. For other types of settings, it returns
  1324. 0.
  1325. @end deftypemethod
  1326. @deftypemethod Setting bool isGroup ()
  1327. @deftypemethodx Setting bool isArray ()
  1328. @deftypemethodx Setting bool isList ()
  1329. These convenience methods test if a setting is of a given type.
  1330. @end deftypemethod
  1331. @deftypemethod Setting bool isAggregate ()
  1332. @deftypemethodx Setting bool isScalar ()
  1333. @deftypemethodx Setting bool isNumber ()
  1334. These convenience methods test if a setting is of an aggregate type (a
  1335. group, array, or list), of a scalar type (integer, 64-bit integer,
  1336. floating point, boolean, or string), and of a number (integer, 64-bit
  1337. integer, or floating point), respectively.
  1338. @end deftypemethod
  1339. @deftypemethod Setting {const char *} getSourceFile ()
  1340. This function returns the name of the file from which the setting was
  1341. read, or NULL if the setting was not read from a file. This
  1342. information is useful for reporting application-level errors. Storage
  1343. for the returned string is managed by the library and released
  1344. automatically when the configuration is destroyed; the string must
  1345. not be freed by the caller.
  1346. @end deftypemethod
  1347. @deftypemethod Setting {unsigned int} getSourceLine ()
  1348. This function returns the line number of the configuration file or
  1349. stream at which the setting @var{setting} was read, or 0 if no line
  1350. number is available. This information is useful for reporting
  1351. application-level errors.
  1352. @end deftypemethod
  1353. @node Example Programs, Configuration File Grammar, The C++ API, Top
  1354. @comment node-name, next, previous, up
  1355. @chapter Example Programs
  1356. Practical example programs that illustrate how to use @i{libconfig}
  1357. from both C and C++ are included in the @file{examples} subdirectory
  1358. of the distribution. These examples include:
  1359. @table @file
  1360. @item examples/c/example1.c
  1361. An example C program that reads a configuration from an existing file
  1362. @file{example.cfg} (also located in @file{examples/c}) and displays
  1363. some of its contents.
  1364. @item examples/c++/example1.cpp
  1365. The C++ equivalent of @file{example1.c}.
  1366. @item examples/c/example2.c
  1367. An example C program that reads a configuration from an existing file
  1368. @file{example.cfg} (also located in @file{examples/c}), adds new
  1369. settings to the configuration, and writes the updated configuration to
  1370. another file.
  1371. @item examples/c++/example2.cpp
  1372. The C++ equivalent of @file{example2.c}
  1373. @item examples/c/example3.c
  1374. An example C program that constructs a new configuration in memory and writes it to a file.
  1375. @item examples/c++/example3.cpp
  1376. The C++ equivalent of @file{example3.c}
  1377. @end table
  1378. @node Configuration File Grammar, License, Example Programs, Top
  1379. @comment node-name, next, previous, up
  1380. @chapter Configuration File Grammar
  1381. Below is the BNF grammar for configuration files. Comments and include
  1382. directives are not part of the grammar, so they are not included here.
  1383. @sp 1
  1384. @example
  1385. configuration = setting-list | empty
  1386. setting-list = setting | setting-list setting
  1387. setting = name (":" | "=") value (";" | "," | empty)
  1388. value = scalar-value | array | list | group
  1389. value-list = value | value-list "," value
  1390. scalar-value = boolean | integer | integer64 | hex | hex64 | float
  1391. | string
  1392. scalar-value-list = scalar-value | scalar-value-list "," scalar-value
  1393. array = "[" (scalar-value-list | empty) "]"
  1394. list = "(" (value-list | empty) ")"
  1395. group = "@{" (setting-list | empty) "@}"
  1396. empty =
  1397. @end example
  1398. @sp 2
  1399. Terminals are defined below as regular expressions:
  1400. @sp 1
  1401. @multitable @columnfractions .2 .8
  1402. @item @code{boolean} @tab
  1403. @code{([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])}
  1404. @item @code{string} @tab
  1405. @code{\"([^\"\\]|\\.)*\"}
  1406. @item @code{name} @tab
  1407. @code{[A-Za-z\*][-A-Za-z0-9_\*]*}
  1408. @item @code{integer} @tab
  1409. @code{[-+]?[0-9]+}
  1410. @item @code{integer64} @tab
  1411. @code{[-+]?[0-9]+L(L)?}
  1412. @item @code{hex} @tab
  1413. @code{0[Xx][0-9A-Fa-f]+}
  1414. @item @code{hex64} @tab
  1415. @code{0[Xx][0-9A-Fa-f]+L(L)?}
  1416. @item @code{float} @tab
  1417. @code{([-+]?([0-9]*)?\.[0-9]*([eE][-+]?[0-9]+)?)|([-+]([0-9]+)(\.[0-9]*)?[eE][-+]?[0-9]+)}
  1418. @end multitable
  1419. @node License, Function Index, Configuration File Grammar, Top
  1420. @comment node-name, next, previous, up
  1421. @appendix License
  1422. @include LGPL.texi
  1423. @node Function Index, Type Index, License, Top
  1424. @comment node-name, next, previous, up
  1425. @unnumbered Function Index
  1426. @printindex fn
  1427. @node Type Index, Concept Index, Function Index, Top
  1428. @comment node-name, next, previous, up
  1429. @unnumbered Type Index
  1430. @printindex tp
  1431. @node Concept Index, , Type Index, Top
  1432. @comment node-name, next, previous, up
  1433. @unnumbered Concept Index
  1434. @printindex cp
  1435. @bye