Annotation of embedaddon/confuse/doc/tutorial-html/ar01s02.html, revision 1.1.1.2

1.1.1.2 ! misho       1: <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        !             2: <!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>
1.1       misho       3:             Of course, not only strings can be specified in the configuration file.
                      4:             libConfuse can parse strings, integers, booleans and floating point values.
                      5:             These are the fundamental values, and they are all also available as lists.
                      6:             We'll talk more about lists in the next chapter.
                      7:         </p><p>
                      8:             The macros used to initialize a string, integer, boolean and a
                      9:             float is, respectively, <code class="function">CFG_STR()</code>,
                     10:             <code class="function">CFG_INT()</code>, <code class="function">CFG_BOOL()</code>,
                     11:             <code class="function">CFG_FLOAT()</code> and
                     12:             <code class="function">CFG_PTR()</code>. All macros take three parameters:
                     13:             the name of the option, a default value and flags. To retrieve the
                     14:             values, use <code class="function">cfg_getstr()</code>,
                     15:             <code class="function">cfg_getint()</code>,
                     16:             <code class="function">cfg_getbool()</code>,
                     17:             <code class="function">cfg_getfloat()</code> or
                     18:             <code class="function">cfg_getptr()</code>, respectively.
                     19:         </p><p>
                     20:             Let's introduce an integer option that tells us how many times to print the
                     21:             greeting:
                     22:         </p><a id="listing3"></a><pre class="programlisting">
                     23: 1      #include &lt;stdio.h&gt;
                     24: 2      #include &lt;confuse.h&gt;
                     25: 3      
                     26: 4      int main(void)
                     27: 5      {
                     28: 6          cfg_opt_t opts[] =
                     29: 7          {
                     30: 8              CFG_STR("target", "World", CFGF_NONE),
                     31: 9              CFG_INT("repeat", 1, CFGF_NONE),
                     32: 10             CFG_END()
                     33: 11         };
                     34: 12         cfg_t *cfg;
                     35: 13         int repeat;
                     36: 14     
                     37: 15         cfg = cfg_init(opts, CFGF_NONE);
                     38: 16         if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
                     39: 17             return 1;
                     40: 18     
                     41: 19         repeat = cfg_getint(cfg, "repeat");
                     42: 20         while(repeat--)
                     43: 21             printf("Hello, %s!\n", cfg_getstr(cfg, "target"));
                     44: 22     
                     45: 23         cfg_free(cfg);
                     46: 24         return 0;
                     47: 25     }
                     48: 26     
                     49: </pre><p>
                     50:             Here we have used the <code class="function">CFG_INT()</code> macro to
                     51:             initialize an integer option named "repeat". The default value is 1
                     52:             as in the standard greeting. The value is retrieved with
                     53:             <code class="function">cfg_getint()</code>.
                     54:         </p><p><a id="negative-repeat-problem"></a>
                     55:             But, wait a moment, what if the user specified a negative value for
                     56:             "repeat"?  Or a too large positive value? libConfuse can handle
                     57:             that with a so-called validating callback. We'll come back to this
                     58:             problem later on, but we will first take a look at lists.
1.1.1.2 ! misho      59:         </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>