Annotation of embedaddon/confuse/doc/tutorial-html/ar01s05.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>5. Parsing from internal buffers</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="ar01s04.html" title="4. Using sections" /><link rel="next" href="ar01s06.html" title="6. Validating callback functions" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5. Parsing from internal buffers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s06.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="idm100"></a>5. Parsing from internal buffers</h2></div></div></div><p>
1.1       misho       3:             So far, we have only parsed configuration data from files.
                      4:             libConfuse can also parse buffers, or in-memory character
                      5:             strings. We will use this to fix a problem in the previous code.
                      6:         </p><p>
                      7:             The problem is that without a configuration file, the hello program
                      8:             will not print anything. We want it to at least print the standard
                      9:             greeting "Hello, World!" if no configuration file is available.
                     10:         </p><p>
                     11:             We can't have a default value for a section that can be specified
                     12:             multiple times (ie, a section with the CFGF_MULTI flag set).
                     13:             Instead we will parse a default configuration string if no section
                     14:             has been parsed:
                     15:         </p><a id="listing6"></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          /* ... setup options ... */
                     22: 7      
                     23: 8          cfg = cfg_init(opts, CFGF_NONE);
                     24: 9          cfg_parse(cfg, "hello.conf");
                     25: 10     
                     26: 11         if(cfg_size(cfg, "greeting") == 0)
                     27: 12         {
                     28: 13             cfg_parse_buf(cfg, "greeting Hello {}");
                     29: 14         }
                     30: 15     
                     31: 16         /* ... print the greetings ... */
                     32: 17     }
                     33: </pre><p>
                     34:             Only the changes from the previous code is shown here. We check if
                     35:             the size of the "greeting" section is zero (ie, no section has been
                     36:             defined). In that case we call <code class="function">cfg_parse_buf()</code>
                     37:             to parse a default in-memory string "greeting Hello {}". This
                     38:             string defines a greeting section with title Hello, but without any
                     39:             sub-options. This way we rely on the default values of the
                     40:             (sub-)options "targets" and "repeat".
                     41:         </p><p>
                     42:             When this program is run, it issues the well-known standard greeting
                     43:             "Hello, World!" if no configuration file is present.
1.1.1.2 ! misho      44:         </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">4. Using sections </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 6. Validating callback functions</td></tr></table></div></body></html>

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