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