Return to simple_8c-example.html CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / doc / html |
1.1 ! misho 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ! 2: "http://www.w3.org/TR/html4/strict.dtd"> ! 3: <html> ! 4: <head> ! 5: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> ! 6: <title>confuse: simple.c</title> ! 7: <link rel="stylesheet" href="tutorial.css" type="text/css"> ! 8: </head> ! 9: <body> ! 10: ! 11: <div class="main"> ! 12: ! 13: <!-- doxygen-header.html ends here --> ! 14: <!-- Generated by Doxygen 1.5.8 --> ! 15: <div class="navigation" id="top"> ! 16: <div class="tabs"> ! 17: <ul> ! 18: <li><a href="index.html"><span>Main Page</span></a></li> ! 19: <li><a href="annotated.html"><span>Data Structures</span></a></li> ! 20: <li><a href="files.html"><span>Files</span></a></li> ! 21: <li><a href="examples.html"><span>Examples</span></a></li> ! 22: </ul> ! 23: </div> ! 24: </div> ! 25: <div class="contents"> ! 26: <h1>simple.c</h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include <string.h></span> ! 27: <span class="preprocessor">#include <stdlib.h></span> ! 28: <span class="preprocessor">#include "<a class="code" href="confuse_8h.html" title="A configuration file parser library.">confuse.h</a>"</span> ! 29: ! 30: <span class="keywordtype">int</span> main(<span class="keywordtype">void</span>) ! 31: { ! 32: <a class="code" href="confuse_8h.html#4bce4b6aed9b07489d6a5c70321907e4" title="Boolean values.">cfg_bool_t</a> verbose = cfg_false; ! 33: <span class="keywordtype">char</span> *server = NULL; ! 34: <span class="keywordtype">double</span> delay = 1.356e-32; ! 35: <span class="keywordtype">char</span> *username = NULL; ! 36: ! 37: <span class="comment">/* Although the macro used to specify an integer option is called</span> ! 38: <span class="comment"> * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system</span> ! 39: <span class="comment"> * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64</span> ! 40: <span class="comment"> * architectures), you will get weird effects if you use an int here.</span> ! 41: <span class="comment"> *</span> ! 42: <span class="comment"> * If you use the regular (non-"simple") options, ie CFG_INT() and use</span> ! 43: <span class="comment"> * cfg_getint(), this is not a problem as the data types are implicitly</span> ! 44: <span class="comment"> * cast.</span> ! 45: <span class="comment"> */</span> ! 46: <span class="keywordtype">long</span> <span class="keywordtype">int</span> debug = 1; ! 47: ! 48: <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[] = { ! 49: <a name="a1"></a><a class="code" href="confuse_8h.html#228dc9c22fbcbeabed4d171774662ce8" title="Initialize a &quot;simple&quot; boolean option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_BOOL</a>(<span class="stringliteral">"verbose"</span>, &verbose), ! 50: <a name="a2"></a><a class="code" href="confuse_8h.html#e54fbbc31bd8c7ec8d7f04597a9f749d" title="Initialize a &quot;simple&quot; string option.">CFG_SIMPLE_STR</a>(<span class="stringliteral">"server"</span>, &server), ! 51: <a class="code" href="confuse_8h.html#e54fbbc31bd8c7ec8d7f04597a9f749d" title="Initialize a &quot;simple&quot; string option.">CFG_SIMPLE_STR</a>(<span class="stringliteral">"user"</span>, &username), ! 52: <a name="a3"></a><a class="code" href="confuse_8h.html#88fa2e73a1294c7e8a1f1519b68ce0ff" title="Initialize a &quot;simple&quot; integer option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_INT</a>(<span class="stringliteral">"debug"</span>, &debug), ! 53: <a name="a4"></a><a class="code" href="confuse_8h.html#073b3b12a5ba4648a1f4f1aa40ff3a2a" title="Initialize a &quot;simple&quot; floating point option (see documentation for CFG_SIMPLE_STR...">CFG_SIMPLE_FLOAT</a>(<span class="stringliteral">"delay"</span>, &delay), ! 54: <a name="a5"></a><a class="code" href="confuse_8h.html#6b29dd8a4c6cd3d392d4ab6b2e535597" title="Terminate list of options.">CFG_END</a>() ! 55: }; ! 56: <a name="_a6"></a><a class="code" href="structcfg__t.html" title="Data structure holding information about a &quot;section&quot;.">cfg_t</a> *cfg; ! 57: ! 58: <span class="comment">/* set default value for the server option */</span> ! 59: server = strdup(<span class="stringliteral">"gazonk"</span>); ! 60: ! 61: cfg = <a name="a7"></a>cfg_init(opts, 0); ! 62: <a name="a8"></a>cfg_parse(cfg, <span class="stringliteral">"simple.conf"</span>); ! 63: ! 64: printf(<span class="stringliteral">"verbose: %s\n"</span>, verbose ? <span class="stringliteral">"true"</span> : <span class="stringliteral">"false"</span>); ! 65: printf(<span class="stringliteral">"server: %s\n"</span>, server); ! 66: printf(<span class="stringliteral">"username: %s\n"</span>, username); ! 67: printf(<span class="stringliteral">"debug: %ld\n"</span>, debug); ! 68: printf(<span class="stringliteral">"delay: %G\n"</span>, delay); ! 69: ! 70: printf(<span class="stringliteral">"setting username to 'foo'\n"</span>); ! 71: <span class="comment">/* using cfg_setstr here is not necessary at all, the equivalent</span> ! 72: <span class="comment"> * code is:</span> ! 73: <span class="comment"> * free(username);</span> ! 74: <span class="comment"> * username = strdup("foo");</span> ! 75: <span class="comment"> */</span> ! 76: <a name="a9"></a>cfg_setstr(cfg, <span class="stringliteral">"user"</span>, <span class="stringliteral">"foo"</span>); ! 77: printf(<span class="stringliteral">"username: %s\n"</span>, username); ! 78: ! 79: <span class="comment">/* print the parsed values to another file */</span> ! 80: { ! 81: FILE *fp = fopen(<span class="stringliteral">"simple.conf.out"</span>, <span class="stringliteral">"w"</span>); ! 82: <a name="a10"></a>cfg_print(cfg, fp); ! 83: fclose(fp); ! 84: } ! 85: ! 86: <a name="a11"></a>cfg_free(cfg); ! 87: ! 88: <span class="comment">/* You are responsible for freeing string values. */</span> ! 89: free(server); ! 90: free(username); ! 91: ! 92: <span class="keywordflow">return</span> 0; ! 93: } ! 94: </pre></div> </div> ! 95: <!-- doxygen-footer.html starts here --> ! 96: </div> ! 97: </body> ! 98: </html>