summaryrefslogtreecommitdiff
path: root/yajl-2.1.0/example_2parse_config_8c-example.html
blob: 0034eaa1a9f85071bfb7c9502b6cb0b724994758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!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/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>YAJL: example/parse_config.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">YAJL
   &#160;<span id="projectnumber">2.1.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;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><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">example/parse_config.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Copyright (c) 2007-2014, Lloyd Hilaiel &lt;me@lloyd.io&gt;</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Permission to use, copy, modify, and/or distribute this software for any</span></div>
<div class="line"><span class="comment"> * purpose with or without fee is hereby granted, provided that the above</span></div>
<div class="line"><span class="comment"> * copyright notice and this permission notice appear in all copies.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * THE SOFTWARE IS PROVIDED &quot;AS IS&quot; AND THE AUTHOR DISCLAIMS ALL WARRANTIES</span></div>
<div class="line"><span class="comment"> * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF</span></div>
<div class="line"><span class="comment"> * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR</span></div>
<div class="line"><span class="comment"> * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES</span></div>
<div class="line"><span class="comment"> * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN</span></div>
<div class="line"><span class="comment"> * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF</span></div>
<div class="line"><span class="comment"> * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;yajl/yajl_tree.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> fileData[65536];</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span></div>
<div class="line">main(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    <span class="keywordtype">size_t</span> rd;</div>
<div class="line">    <a name="_a0"></a><a class="code" href="structyajl__val__s.html">yajl_val</a> node;</div>
<div class="line">    <span class="keywordtype">char</span> errbuf[1024];</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* null plug buffers */</span></div>
<div class="line">    fileData[0] = errbuf[0] = 0;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* read the entire config file */</span></div>
<div class="line">    rd = fread((<span class="keywordtype">void</span> *) fileData, 1, <span class="keyword">sizeof</span>(fileData) - 1, stdin);</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* file read error handling */</span></div>
<div class="line">    <span class="keywordflow">if</span> (rd == 0 &amp;&amp; !feof(stdin)) {</div>
<div class="line">        fprintf(stderr, <span class="stringliteral">&quot;error encountered on file read\n&quot;</span>);</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (rd &gt;= <span class="keyword">sizeof</span>(fileData) - 1) {</div>
<div class="line">        fprintf(stderr, <span class="stringliteral">&quot;config file too big\n&quot;</span>);</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* we have the whole config file in memory.  let&#39;s parse it ... */</span></div>
<div class="line">    node = <a name="a1"></a><a class="code" href="yajl__tree_8h.html#a6a04342e825859a3e9d52df93e9d112a">yajl_tree_parse</a>((<span class="keyword">const</span> <span class="keywordtype">char</span> *) fileData, errbuf, <span class="keyword">sizeof</span>(errbuf));</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* parse error handling */</span></div>
<div class="line">    <span class="keywordflow">if</span> (node == NULL) {</div>
<div class="line">        fprintf(stderr, <span class="stringliteral">&quot;parse_error: &quot;</span>);</div>
<div class="line">        <span class="keywordflow">if</span> (strlen(errbuf)) fprintf(stderr, <span class="stringliteral">&quot; %s&quot;</span>, errbuf);</div>
<div class="line">        <span class="keywordflow">else</span> fprintf(stderr, <span class="stringliteral">&quot;unknown error&quot;</span>);</div>
<div class="line">        fprintf(stderr, <span class="stringliteral">&quot;\n&quot;</span>);</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* ... and extract a nested value from the config file */</span></div>
<div class="line">    {</div>
<div class="line">        <span class="keyword">const</span> <span class="keywordtype">char</span> * path[] = { <span class="stringliteral">&quot;Logging&quot;</span>, <span class="stringliteral">&quot;timeFormat&quot;</span>, (<span class="keyword">const</span> <span class="keywordtype">char</span> *) 0 };</div>
<div class="line">        <a class="code" href="structyajl__val__s.html">yajl_val</a> v = <a name="a2"></a><a class="code" href="yajl__tree_8h.html#a534c427581fac81ee828c28367dbe90d">yajl_tree_get</a>(node, path, <a name="a3"></a><a class="code" href="yajl__tree_8h.html#ae95a1d5a195e8bd22a96f69cbf4e7c91a8e1c9361f16919197409a226dcad7c65">yajl_t_string</a>);</div>
<div class="line">        <span class="keywordflow">if</span> (v) printf(<span class="stringliteral">&quot;%s/%s: %s\n&quot;</span>, path[0], path[1], <a name="a4"></a><a class="code" href="yajl__tree_8h.html#a2fe1773db524ee1efdad7eb97ce12c18">YAJL_GET_STRING</a>(v));</div>
<div class="line">        <span class="keywordflow">else</span>   printf(<span class="stringliteral">&quot;no such node: %s/%s\n&quot;</span>, path[0], path[1]);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <a name="a5"></a><a class="code" href="yajl__tree_8h.html#a83b59f411e80404aad35ae7071c4ecf8">yajl_tree_free</a>(node);</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Mar 18 2014 22:12:15 for YAJL by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>