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.&#160;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.&#160;Other types of options" /><link rel="next" href="ar01s04.html" title="4.&#160;Using sections" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.&#160;Introducing lists</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s02.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="ar01s04.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="3.&#160;Introducing lists"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id401321"></a>3.&#160;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">
171	#include &lt;stdio.h&gt;
182	#include &lt;confuse.h&gt;
193	
204	int main(void)
215	{
226	    cfg_opt_t opts[] =
237	    {
248	        CFG_STR_LIST("targets", "{World}", CFGF_NONE),
259	        CFG_INT("repeat", 1, CFGF_NONE),
2610	        CFG_END()
2711	    };
2812	    cfg_t *cfg;
2913	    int repeat;
3014	    int i;
3115	
3216	    cfg = cfg_init(opts, CFGF_NONE);
3317	    if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR)
3418	        return 1;
3519	
3620	    repeat = cfg_getint(cfg, "repeat");
3721	    while(repeat--)
3822	    {
3923	        printf("Hello");
4024	        for(i = 0; i &lt; cfg_size(cfg, "targets"); i++)
4125	            printf(", %s", cfg_getnstr(cfg, "targets", i));
4226	        printf("!\n");
4327	    }
4428	
4529	    cfg_free(cfg);
4630	    return 0;
4731	}
4832	
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
71targets = {"Life", "Universe", "Everything"}
72repeat = 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>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="ar01s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.&#160;Other types of options&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;4.&#160;Using sections</td></tr></table></div></body></html>
80