Annotation of embedaddon/confuse/doc/tutorial-html/ar01s04.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>4. Using sections</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="ar01s03.html" title="3. Introducing lists" /><link rel="next" href="ar01s05.html" title="5. Parsing from internal buffers" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">4. Using sections</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm78"></a>4. Using sections</h2></div></div></div><p>
1.1       misho       3:             So far, we have only use a flat configuration file. libConfuse can also handle
                      4:             sections to build a hierarchy of options. Sections can be used to group options
                      5:             in logical blocks, and those blocks can (optionally) be specified multiple
                      6:             times.
                      7:         </p><p>
                      8:             Sections are initialized with the <code class="function">CFG_SEC()</code> macro. It also takes three
                      9:             parameters: the name of the option, an array of options allowed in the section
                     10:             and flags.
                     11:         </p><p>
                     12:             We'll extend the, now rather complex, hello program so we can do other kinds of
                     13:             greetings, not just "Hello". Each greeting will have its own settings for
                     14:             targets and repeat.
                     15:         </p><a id="listing5"></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 greet_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_opt_t opts[] =
                     28: 13         {
                     29: 14             CFG_SEC("greeting", greet_opts, CFGF_TITLE | CFGF_MULTI),
                     30: 15             CFG_END()
                     31: 16         };
                     32: 17         cfg_t *cfg, *cfg_greet;
                     33: 18         int repeat;
                     34: 19         int i, j;
                     35: 20     
                     36: 21         cfg = cfg_init(opts, CFGF_NONE);
                     37: 22         if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
                     38: 23             return 1;
                     39: 24     
                     40: 25         for(j = 0; j &lt; cfg_size(cfg, "greeting"); j++)
                     41: 26         {
                     42: 27             cfg_greet = cfg_getnsec(cfg, "greeting", j);
                     43: 28     
                     44: 29             repeat = cfg_getint(cfg_greet, "repeat");
                     45: 30             while(repeat--)
                     46: 31             {
                     47: 32                 printf("%s", cfg_title(cfg_greet));
                     48: 33                 for(i = 0; i &lt; cfg_size(cfg_greet, "targets"); i++)
                     49: 34                     printf(", %s", cfg_getnstr(cfg_greet, "targets", i));
                     50: 35                 printf("!\n");
                     51: 36             }
                     52: 37         }
                     53: 38     
                     54: 39         cfg_free(cfg);
                     55: 40         return 0;
                     56: 41     }
                     57: 42     
                     58: </pre><p>
                     59:             We have renamed the option array from "opts" to "greet_opts", and introduced a
                     60:             new "opts" array that only has one option: a "greeting" section.
                     61:             The second parameter of the <code class="function">CFG_SEC()</code> macro
                     62:             points to the old greeting options "targets" and "repeat".
                     63:         </p><p>
                     64:             We have also used a couple of flags to alter the behaviour of the
                     65:             section: CFGF_TITLE means that a greeting section should have a
                     66:             title and the CFGF_MULTI flag tells libConfuse that this section
                     67:             may be specified multiple times in the configuration file. The
                     68:             title of a section is retrieved with the
                     69:             <code class="function">cfg_title()</code> function.
                     70:         </p><p>
                     71:             The outmost loop (with index j) now loops through all given
                     72:             sections in the configuration file. We retrieve a section with a
                     73:             <code class="function">cfg_getnsec()</code> call. The value returned is a
                     74:             pointer to a cfg_t struct, the same type as returned by
                     75:             <code class="function">cfg_init()</code>. Thus we can use the ordinary value
                     76:             retrieval functions <code class="function">cfg_getstr()</code>,
                     77:             <code class="function">cfg_getint()</code> and so on to retrieve values of
                     78:             options inside the section.
                     79:         </p><p>
                     80:             Ok, so how does the configuration file look like for this setup?
                     81:         </p><pre class="programlisting">
                     82: # this is the configuration file for the hello program
                     83: 
                     84: greeting Hello
                     85: {
                     86:     targets = {"Life", "Universe", "Everything"}
                     87:     repeat = 1
                     88: }
                     89: 
                     90: greeting Bye
                     91: {
                     92:     targets = {Adams}
                     93:     repeat = 1
                     94: }
                     95:         </pre><p>
                     96:             The program will loop through the sections in the order specified
                     97:             in the configuration file. First it will find the "Hello" section.
                     98:             It prints the title of the section, "Hello", retrieved with
                     99:             <code class="function">cfg_title()</code>. Then the targets are printed just
1.1.1.2 ! misho     100:             as in the previous examples, but this time the values are retrieved
1.1       misho     101:             from the cfg_greet section. Next, the section titled "Bye" is
                    102:             found, and the values are retrieved from that section.
                    103:         </p><p>
                    104:             When run, the program produces the following:
                    105:         </p><pre class="programlisting">
                    106: $ ./listing5
                    107: Hello, Life, Universe, Everything!
                    108: Bye, Adams!
                    109: $ 
1.1.1.2 ! misho     110:         </pre></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. Introducing lists </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5. Parsing from internal buffers</td></tr></table></div></body></html>

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