Annotation of embedaddon/confuse/doc/tutorial-html/ar01s05.html, revision 1.1.1.1
1.1 misho 1: <?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?>
2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3: <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>5. Parsing from internal buffers</title><link rel="stylesheet" href="tutorial.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="libConfuse tutorial" /><link rel="up" href="index.html" title="libConfuse tutorial" /><link rel="prev" href="ar01s04.html" title="4. Using sections" /><link rel="next" href="ar01s06.html" title="6. Validating callback functions" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5. Parsing from internal buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s06.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="5. Parsing from internal buffers"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id442117"></a>5. Parsing from internal buffers</h2></div></div></div><p>
4: So far, we have only parsed configuration data from files.
5: libConfuse can also parse buffers, or in-memory character
6: strings. We will use this to fix a problem in the previous code.
7: </p><p>
8: The problem is that without a configuration file, the hello program
9: will not print anything. We want it to at least print the standard
10: greeting "Hello, World!" if no configuration file is available.
11: </p><p>
12: We can't have a default value for a section that can be specified
13: multiple times (ie, a section with the CFGF_MULTI flag set).
14: Instead we will parse a default configuration string if no section
15: has been parsed:
16: </p><a id="listing6"></a><pre class="programlisting">
17: 1 #include <stdio.h>
18: 2 #include <confuse.h>
19: 3
20: 4 int main(void)
21: 5 {
22: 6 /* ... setup options ... */
23: 7
24: 8 cfg = cfg_init(opts, CFGF_NONE);
25: 9 cfg_parse(cfg, "hello.conf");
26: 10
27: 11 if(cfg_size(cfg, "greeting") == 0)
28: 12 {
29: 13 cfg_parse_buf(cfg, "greeting Hello {}");
30: 14 }
31: 15
32: 16 /* ... print the greetings ... */
33: 17 }
34: </pre><p>
35: Only the changes from the previous code is shown here. We check if
36: the size of the "greeting" section is zero (ie, no section has been
37: defined). In that case we call <code class="function">cfg_parse_buf()</code>
38: to parse a default in-memory string "greeting Hello {}". This
39: string defines a greeting section with title Hello, but without any
40: sub-options. This way we rely on the default values of the
41: (sub-)options "targets" and "repeat".
42: </p><p>
43: When this program is run, it issues the well-known standard greeting
44: "Hello, World!" if no configuration file is present.
45: </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. Using sections </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 6. Validating callback functions</td></tr></table></div></body></html>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>