Annotation of embedaddon/confuse/doc/tutorial-html/ar01s03.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>3. Introducing lists</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="ar01s02.html" title="2. Other types of options" /><link rel="next" href="ar01s04.html" title="4. Using sections" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3. Introducing lists</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s02.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s04.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm56"></a>3. Introducing lists</h2></div></div></div><p>
1.1       misho       3:             That was easy. Now let's extend the program a bit so we can greet more than one
                      4:             "target". We'd like to be able to specify a list of targets to greet.
                      5:         </p><p>
                      6:             The list versions of the initialization macros are named
                      7:             <code class="function">CFG_STR_LIST()</code>,
                      8:             <code class="function">CFG_INT_LIST()</code>,
                      9:             <code class="function">CFG_BOOL_LIST()</code> and
                     10:             <code class="function">CFG_FLOAT_LIST()</code>. They take the same
                     11:             parameters as the non-list versions, except the default value must
                     12:             be a string surrounded by curly braces.
                     13:         </p><p>
                     14:             The modified program is shown below:
                     15:         </p><a id="listing4"></a><pre class="programlisting">
                     16: 1      #include &lt;stdio.h&gt;
                     17: 2      #include &lt;confuse.h&gt;
                     18: 3      
                     19: 4      int main(void)
                     20: 5      {
                     21: 6          cfg_opt_t opts[] =
                     22: 7          {
                     23: 8              CFG_STR_LIST("targets", "{World}", CFGF_NONE),
                     24: 9              CFG_INT("repeat", 1, CFGF_NONE),
                     25: 10             CFG_END()
                     26: 11         };
                     27: 12         cfg_t *cfg;
                     28: 13         int repeat;
                     29: 14         int i;
                     30: 15     
                     31: 16         cfg = cfg_init(opts, CFGF_NONE);
                     32: 17         if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
                     33: 18             return 1;
                     34: 19     
                     35: 20         repeat = cfg_getint(cfg, "repeat");
                     36: 21         while(repeat--)
                     37: 22         {
                     38: 23             printf("Hello");
                     39: 24             for(i = 0; i &lt; cfg_size(cfg, "targets"); i++)
                     40: 25                 printf(", %s", cfg_getnstr(cfg, "targets", i));
                     41: 26             printf("!\n");
                     42: 27         }
                     43: 28     
                     44: 29         cfg_free(cfg);
                     45: 30         return 0;
                     46: 31     }
                     47: 32     
                     48: </pre><p>
                     49:             Three things are a bit different here. First, the macro to
                     50:             initialize the "targets" option is
                     51:             <code class="function">CFG_STR_LIST()</code>. This tells libConfuse that
                     52:             "targets" is a list of strings. Second, the default value in the
                     53:             second parameter is surrounded by curly braces. This is needed to
                     54:             indicate to libConfuse where the list of values ends.
                     55:         </p><p>
                     56:             The third change is in the printing of the greeting. First we print
                     57:             the "Hello" string. Then we loop through all values found for the
                     58:             "targets" option. The number of values is retrieved with the
                     59:             <code class="function">cfg_size()</code> function. The string values are
                     60:             then retrieved with <code class="function">cfg_getnstr()</code>, which is an
                     61:             indexed version of <code class="function">cfg_getstr()</code>. In fact,
                     62:             <code class="function">cfg_getstr()</code> is equivalent to
                     63:             <code class="function">cfg_getnstr()</code> with an index of zero.
                     64:         </p><p>
                     65:             In the configuration file hello.conf, we can now specify a list of targets to
                     66:             greet:
                     67:         </p><pre class="programlisting">
                     68: # this is the configuration file for the hello program
                     69: 
                     70: targets = {"Life", "Universe", "Everything"}
                     71: repeat = 1
                     72:         </pre><p>
                     73:             The output of the hello program, run with the above configuration file, is:
                     74:             "Hello, Life, Universe, Everything!"
                     75:         </p><p>
                     76:             Again, if no targets were configured, the greeting would have been the standard
                     77:             "Hello, World!".
1.1.1.2 ! misho      78:         </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s02.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2. Other types of options </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 4. Using sections</td></tr></table></div></body></html>

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