Sophie

Sophie

distrib > Mandriva > 2011.0 > x86_64 > by-pkgid > 3af49f892c27a4cd1e9f30e4eb9b26e1 > files > 48

lib64confuse-devel-2.7-2mdv2011.0.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <title>confuse: simple.c</title>
 <link rel="stylesheet" href="tutorial.css" type="text/css">
</head>
<body>

<div class="main">

<!-- doxygen-header.html ends here -->
<!-- Generated by Doxygen 1.5.8 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>simple.c</h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;string.h&gt;</span>
<span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<span class="preprocessor">#include "<a class="code" href="confuse_8h.html" title="A configuration file parser library.">confuse.h</a>"</span>

<span class="keywordtype">int</span> main(<span class="keywordtype">void</span>)
{
    <a class="code" href="confuse_8h.html#4bce4b6aed9b07489d6a5c70321907e4" title="Boolean values.">cfg_bool_t</a> verbose = cfg_false;
    <span class="keywordtype">char</span> *server = NULL;
    <span class="keywordtype">double</span> delay = 1.356e-32;
    <span class="keywordtype">char</span> *username = NULL;

    <span class="comment">/* Although the macro used to specify an integer option is called</span>
<span class="comment">     * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system</span>
<span class="comment">     * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64</span>
<span class="comment">     * architectures), you will get weird effects if you use an int here.</span>
<span class="comment">     *</span>
<span class="comment">     * If you use the regular (non-"simple") options, ie CFG_INT() and use</span>
<span class="comment">     * cfg_getint(), this is not a problem as the data types are implicitly</span>
<span class="comment">     * cast.</span>
<span class="comment">     */</span>
    <span class="keywordtype">long</span> <span class="keywordtype">int</span> debug = 1;

    <a name="_a0"></a><a class="code" href="structcfg__opt__t.html" title="Data structure holding information about an option.">cfg_opt_t</a> opts[] = {
        <a name="a1"></a><a class="code" href="confuse_8h.html#228dc9c22fbcbeabed4d171774662ce8" title="Initialize a &amp;quot;simple&amp;quot; boolean option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_BOOL</a>(<span class="stringliteral">"verbose"</span>, &amp;verbose),
        <a name="a2"></a><a class="code" href="confuse_8h.html#e54fbbc31bd8c7ec8d7f04597a9f749d" title="Initialize a &amp;quot;simple&amp;quot; string option.">CFG_SIMPLE_STR</a>(<span class="stringliteral">"server"</span>, &amp;server),
        <a class="code" href="confuse_8h.html#e54fbbc31bd8c7ec8d7f04597a9f749d" title="Initialize a &amp;quot;simple&amp;quot; string option.">CFG_SIMPLE_STR</a>(<span class="stringliteral">"user"</span>, &amp;username),
        <a name="a3"></a><a class="code" href="confuse_8h.html#88fa2e73a1294c7e8a1f1519b68ce0ff" title="Initialize a &amp;quot;simple&amp;quot; integer option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_INT</a>(<span class="stringliteral">"debug"</span>, &amp;debug),
        <a name="a4"></a><a class="code" href="confuse_8h.html#073b3b12a5ba4648a1f4f1aa40ff3a2a" title="Initialize a &amp;quot;simple&amp;quot; floating point option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_FLOAT</a>(<span class="stringliteral">"delay"</span>, &amp;delay),
        <a name="a5"></a><a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>()
    };
    <a name="_a6"></a><a class="code" href="structcfg__t.html" title="Data structure holding information about a &amp;quot;section&amp;quot;.">cfg_t</a> *cfg;

    <span class="comment">/* set default value for the server option */</span>
    server = strdup(<span class="stringliteral">"gazonk"</span>);

    cfg = <a name="a7"></a>cfg_init(opts, 0);
    <a name="a8"></a>cfg_parse(cfg, <span class="stringliteral">"simple.conf"</span>);

    printf(<span class="stringliteral">"verbose: %s\n"</span>, verbose ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>);
    printf(<span class="stringliteral">"server: %s\n"</span>, server);
    printf(<span class="stringliteral">"username: %s\n"</span>, username);
    printf(<span class="stringliteral">"debug: %ld\n"</span>, debug);
    printf(<span class="stringliteral">"delay: %G\n"</span>, delay);

    printf(<span class="stringliteral">"setting username to 'foo'\n"</span>);
    <span class="comment">/* using cfg_setstr here is not necessary at all, the equivalent</span>
<span class="comment">     * code is:</span>
<span class="comment">     *   free(username);</span>
<span class="comment">     *   username = strdup("foo");</span>
<span class="comment">     */</span>
    <a name="a9"></a>cfg_setstr(cfg, <span class="stringliteral">"user"</span>, <span class="stringliteral">"foo"</span>);
    printf(<span class="stringliteral">"username: %s\n"</span>, username);

    <span class="comment">/* print the parsed values to another file */</span>
    {
        FILE *fp = fopen(<span class="stringliteral">"simple.conf.out"</span>, <span class="stringliteral">"w"</span>);
        <a name="a10"></a>cfg_print(cfg, fp);
        fclose(fp);
    }

    <a name="a11"></a>cfg_free(cfg);

    <span class="comment">/* You are responsible for freeing string values. */</span>
    free(server);
    free(username);

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<!-- doxygen-footer.html starts here -->
  </div>
 </body>
</html>