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>2. Other types of options</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="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" title="2. Other types of options"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id401204"></a>2. Other types of options</h2></div></div></div><p> 4 Of course, not only strings can be specified in the configuration file. 5 libConfuse can parse strings, integers, booleans and floating point values. 6 These are the fundamental values, and they are all also available as lists. 7 We'll talk more about lists in the next chapter. 8 </p><p> 9 The macros used to initialize a string, integer, boolean and a 10 float is, respectively, <code class="function">CFG_STR()</code>, 11 <code class="function">CFG_INT()</code>, <code class="function">CFG_BOOL()</code>, 12 <code class="function">CFG_FLOAT()</code> and 13 <code class="function">CFG_PTR()</code>. All macros take three parameters: 14 the name of the option, a default value and flags. To retrieve the 15 values, use <code class="function">cfg_getstr()</code>, 16 <code class="function">cfg_getint()</code>, 17 <code class="function">cfg_getbool()</code>, 18 <code class="function">cfg_getfloat()</code> or 19 <code class="function">cfg_getptr()</code>, respectively. 20 </p><p> 21 Let's introduce an integer option that tells us how many times to print the 22 greeting: 23 </p><a id="listing3"></a><pre class="programlisting"> 241 #include <stdio.h> 252 #include <confuse.h> 263 274 int main(void) 285 { 296 cfg_opt_t opts[] = 307 { 318 CFG_STR("target", "World", CFGF_NONE), 329 CFG_INT("repeat", 1, CFGF_NONE), 3310 CFG_END() 3411 }; 3512 cfg_t *cfg; 3613 int repeat; 3714 3815 cfg = cfg_init(opts, CFGF_NONE); 3916 if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) 4017 return 1; 4118 4219 repeat = cfg_getint(cfg, "repeat"); 4320 while(repeat--) 4421 printf("Hello, %s!\n", cfg_getstr(cfg, "target")); 4522 4623 cfg_free(cfg); 4724 return 0; 4825 } 4926 50</pre><p> 51 Here we have used the <code class="function">CFG_INT()</code> macro to 52 initialize an integer option named "repeat". The default value is 1 53 as in the standard greeting. The value is retrieved with 54 <code class="function">cfg_getint()</code>. 55 </p><p><a id="negative-repeat-problem"></a> 56 But, wait a moment, what if the user specified a negative value for 57 "repeat"? Or a too large positive value? libConfuse can handle 58 that with a so-called validating callback. We'll come back to this 59 problem later on, but we will first take a look at lists. 60 </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> 61