File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / doc / tutorial-html / ar01s02.html
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 4 months ago) by misho
Branches: confuse, MAIN
CVS tags: v3_3, HEAD
confuse 3.3

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>2. Other types of options</title><link rel="stylesheet" type="text/css" href="tutorial.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="home" href="index.html" title="libConfuse tutorial" /><link rel="up" href="index.html" title="libConfuse tutorial" /><link rel="prev" href="index.html" title="libConfuse tutorial" /><link rel="next" href="ar01s03.html" title="3. Introducing lists" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2. Other types of options</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s03.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm36"></a>2. Other types of options</h2></div></div></div><p>
            Of course, not only strings can be specified in the configuration file.
            libConfuse can parse strings, integers, booleans and floating point values.
            These are the fundamental values, and they are all also available as lists.
            We'll talk more about lists in the next chapter.
        </p><p>
            The macros used to initialize a string, integer, boolean and a
            float is, respectively, <code class="function">CFG_STR()</code>,
            <code class="function">CFG_INT()</code>, <code class="function">CFG_BOOL()</code>,
            <code class="function">CFG_FLOAT()</code> and
            <code class="function">CFG_PTR()</code>. All macros take three parameters:
            the name of the option, a default value and flags. To retrieve the
            values, use <code class="function">cfg_getstr()</code>,
            <code class="function">cfg_getint()</code>,
            <code class="function">cfg_getbool()</code>,
            <code class="function">cfg_getfloat()</code> or
            <code class="function">cfg_getptr()</code>, respectively.
        </p><p>
            Let's introduce an integer option that tells us how many times to print the
            greeting:
        </p><a id="listing3"></a><pre class="programlisting">
1	#include &lt;stdio.h&gt;
2	#include &lt;confuse.h&gt;
3	
4	int main(void)
5	{
6	    cfg_opt_t opts[] =
7	    {
8	        CFG_STR("target", "World", CFGF_NONE),
9	        CFG_INT("repeat", 1, CFGF_NONE),
10	        CFG_END()
11	    };
12	    cfg_t *cfg;
13	    int repeat;
14	
15	    cfg = cfg_init(opts, CFGF_NONE);
16	    if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
17	        return 1;
18	
19	    repeat = cfg_getint(cfg, "repeat");
20	    while(repeat--)
21	        printf("Hello, %s!\n", cfg_getstr(cfg, "target"));
22	
23	    cfg_free(cfg);
24	    return 0;
25	}
26	
</pre><p>
            Here we have used the <code class="function">CFG_INT()</code> macro to
            initialize an integer option named "repeat". The default value is 1
            as in the standard greeting. The value is retrieved with
            <code class="function">cfg_getint()</code>.
        </p><p><a id="negative-repeat-problem"></a>
            But, wait a moment, what if the user specified a negative value for
            "repeat"?  Or a too large positive value? libConfuse can handle
            that with a so-called validating callback. We'll come back to this
            problem later on, but we will first take a look at lists.
        </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">libConfuse tutorial </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3. Introducing lists</td></tr></table></div></body></html>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>