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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>KLone: atom.c Source File</title>
<link href="kl.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">src</a> / <a class="el" href="dir_000003.html">kloned</a></div>
<h1>atom.c</h1><a href="atom_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com></span>
00003 <span class="comment"> * All rights reserved.</span>
00004 <span class="comment"> *</span>
00005 <span class="comment"> * This file is part of KLone, and as such it is subject to the license stated</span>
00006 <span class="comment"> * in the LICENSE file which you have received as part of this distribution.</span>
00007 <span class="comment"> *</span>
00008 <span class="comment"> * $Id: atom.c,v 1.12 2006/01/09 12:38:38 tat Exp $</span>
00009 <span class="comment"> */</span>
00010
00011 <span class="preprocessor">#include "klone_conf.h"</span>
00012 <span class="preprocessor">#include <u/libu.h></span>
00013 <span class="preprocessor">#include <<a class="code" href="utils_8h.html">klone/utils.h</a>></span>
00014 <span class="preprocessor">#include <<a class="code" href="atom_8h.html">klone/atom.h</a>></span>
00015
<a name="l00016"></a><a class="code" href="atom_8h.html#a4">00016</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a4">atom_create</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *<span class="keywordtype">id</span>, <span class="keyword">const</span> <span class="keywordtype">char</span> *data, size_t size, <span class="keywordtype">void</span> *arg,
00017 <a class="code" href="structatom__s.html">atom_t</a> **patom)
00018 {
00019 <a class="code" href="structatom__s.html">atom_t</a> *atom = NULL;
00020
00021 dbg_err_if (<span class="keywordtype">id</span> == NULL);
00022 dbg_err_if (data == NULL);
00023 dbg_err_if (patom == NULL);
00024
00025 atom = u_zalloc(<span class="keyword">sizeof</span>(<a class="code" href="structatom__s.html">atom_t</a>));
00026 dbg_err_if(atom == NULL);
00027
00028 atom-><a class="code" href="structatom__s.html#o0">id</a> = u_strdup(<span class="keywordtype">id</span>);
00029 dbg_err_if(atom-><a class="code" href="structatom__s.html#o0">id</a> == NULL);
00030
00031 atom-><a class="code" href="structatom__s.html#o1">data</a> = u_memdup(data, size);
00032 dbg_err_if(atom-><a class="code" href="structatom__s.html#o1">data</a> == NULL);
00033
00034 atom-><a class="code" href="structatom__s.html#o2">size</a> = size;
00035 atom-><a class="code" href="structatom__s.html#o3">arg</a> = arg;
00036
00037 *patom = atom;
00038
00039 <span class="keywordflow">return</span> 0;
00040 err:
00041 <span class="keywordflow">if</span>(atom)
00042 <a class="code" href="atom_8h.html#a5">atom_free</a>(atom);
00043 <span class="keywordflow">return</span> ~0;
00044 }
00045
<a name="l00046"></a><a class="code" href="atom_8h.html#a5">00046</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a5">atom_free</a>(<a class="code" href="structatom__s.html">atom_t</a> *atom)
00047 {
00048 <span class="keywordflow">if</span> (atom)
00049 {
00050 U_FREE(atom-><a class="code" href="structatom__s.html#o0">id</a>);
00051 U_FREE(atom-><a class="code" href="structatom__s.html#o1">data</a>);
00052 U_FREE(atom);
00053 }
00054
00055 <span class="keywordflow">return</span> 0;
00056 }
00057
00058 <span class="comment">/* sum of atoms size field */</span>
<a name="l00059"></a><a class="code" href="atom_8h.html#a8">00059</a> size_t <a class="code" href="atom_8h.html#a8">atoms_size</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as)
00060 {
00061 dbg_err_if (as == NULL);
00062
00063 <span class="keywordflow">return</span> as-><a class="code" href="structatoms__s.html#o1">size</a>;
00064 err:
00065 <span class="keywordflow">return</span> -1;
00066 }
00067
00068 <span class="comment">/* # of atoms */</span>
<a name="l00069"></a><a class="code" href="atom_8h.html#a9">00069</a> size_t <a class="code" href="atom_8h.html#a9">atoms_count</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as)
00070 {
00071 dbg_err_if (as == NULL);
00072
00073 <span class="keywordflow">return</span> as-><a class="code" href="structatoms__s.html#o2">count</a>;
00074 err:
00075 <span class="keywordflow">return</span> -1;
00076 }
00077
00078 <span class="comment">/* return the n-th atom */</span>
<a name="l00079"></a><a class="code" href="atom_8h.html#a10">00079</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a10">atoms_getn</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as, size_t n, <a class="code" href="structatom__s.html">atom_t</a> **patom)
00080 {
00081 <a class="code" href="structatom__s.html">atom_t</a> *atom;
00082 size_t i = 0;
00083
00084 dbg_err_if (as == NULL);
00085 dbg_err_if (n >= as-><a class="code" href="structatoms__s.html#o2">count</a>);
00086 dbg_err_if (patom == NULL);
00087
00088 LIST_FOREACH(atom, &as-><a class="code" href="structatoms__s.html#o0">list</a>, np)
00089 {
00090 <span class="keywordflow">if</span>(i++ == n)
00091 {
00092 *patom = atom;
00093 <span class="keywordflow">return</span> 0;
00094 }
00095 }
00096
00097 err:
00098 <span class="keywordflow">return</span> ~0; <span class="comment">/* out of bounds */</span>
00099 }
00100
00101 <span class="comment">/* return the atom whose ID is id */</span>
<a name="l00102"></a><a class="code" href="atom_8h.html#a11">00102</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a11">atoms_get</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as, <span class="keyword">const</span> <span class="keywordtype">char</span> *<span class="keywordtype">id</span>, <a class="code" href="structatom__s.html">atom_t</a> **patom)
00103 {
00104 <a class="code" href="structatom__s.html">atom_t</a> *atom;
00105
00106 dbg_err_if (as == NULL);
00107 dbg_err_if (<span class="keywordtype">id</span> == NULL);
00108 dbg_err_if (patom == NULL);
00109
00110 LIST_FOREACH(atom, &as-><a class="code" href="structatoms__s.html#o0">list</a>, np)
00111 {
00112 <span class="keywordflow">if</span>(strcmp(<span class="keywordtype">id</span>, atom-><a class="code" href="structatom__s.html#o0">id</a>) == 0)
00113 {
00114 *patom = atom;
00115 <span class="keywordflow">return</span> 0;
00116 }
00117 }
00118
00119 err:
00120 <span class="keywordflow">return</span> ~0; <span class="comment">/* not found */</span>
00121 }
00122
00123 <span class="comment">/* add an atom to the list */</span>
<a name="l00124"></a><a class="code" href="atom_8h.html#a12">00124</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a12">atoms_add</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as, <a class="code" href="structatom__s.html">atom_t</a> *atom)
00125 {
00126 dbg_err_if (as == NULL);
00127 dbg_err_if (atom == NULL);
00128
00129 LIST_INSERT_HEAD(&as-><a class="code" href="structatoms__s.html#o0">list</a>, atom, np);
00130
00131 as-><a class="code" href="structatoms__s.html#o2">count</a>++;
00132 as-><a class="code" href="structatoms__s.html#o1">size</a> += atom-><a class="code" href="structatom__s.html#o2">size</a>;
00133
00134 <span class="keywordflow">return</span> 0;
00135 err:
00136 <span class="keywordflow">return</span> ~0;
00137 }
00138
00139 <span class="comment">/* add an atom to the list */</span>
<a name="l00140"></a><a class="code" href="atom_8h.html#a13">00140</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a13">atoms_remove</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as, <a class="code" href="structatom__s.html">atom_t</a> *atom)
00141 {
00142 dbg_err_if (as == NULL);
00143 dbg_err_if (atom == NULL);
00144
00145 LIST_REMOVE(atom, np);
00146
00147 as-><a class="code" href="structatoms__s.html#o2">count</a>--;
00148 as-><a class="code" href="structatoms__s.html#o1">size</a> -= atom-><a class="code" href="structatom__s.html#o2">size</a>;
00149
00150 <span class="keywordflow">return</span> 0;
00151 err:
00152 <span class="keywordflow">return</span> ~0;
00153
00154 }
00155
00156 <span class="comment">/* create an atom list */</span>
<a name="l00157"></a><a class="code" href="atom_8h.html#a6">00157</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a6">atoms_create</a>(<a class="code" href="structatoms__s.html">atoms_t</a> **pas)
00158 {
00159 <a class="code" href="structatoms__s.html">atoms_t</a> *as = NULL;
00160
00161 dbg_err_if (pas == NULL);
00162
00163 as = u_zalloc(<span class="keyword">sizeof</span>(<a class="code" href="structatoms__s.html">atoms_t</a>));
00164 dbg_err_if(as == NULL);
00165
00166 LIST_INIT(&as-><a class="code" href="structatoms__s.html#o0">list</a>);
00167
00168 *pas = as;
00169
00170 <span class="keywordflow">return</span> 0;
00171 err:
00172 U_FREE(as);
00173 <span class="keywordflow">return</span> ~0;
00174 }
00175
00176 <span class="comment">/* free an atom list */</span>
<a name="l00177"></a><a class="code" href="atom_8h.html#a7">00177</a> <span class="keywordtype">int</span> <a class="code" href="atom_8h.html#a7">atoms_free</a>(<a class="code" href="structatoms__s.html">atoms_t</a> *as)
00178 {
00179 <a class="code" href="structatom__s.html">atom_t</a> *atom;
00180
00181 <span class="keywordflow">if</span> (as)
00182 {
00183 <span class="keywordflow">while</span>(<a class="code" href="atom_8h.html#a9">atoms_count</a>(as))
00184 {
00185 dbg_err_if(<a class="code" href="atom_8h.html#a10">atoms_getn</a>(as, 0, &atom));
00186 dbg_err_if(<a class="code" href="atom_8h.html#a13">atoms_remove</a>(as, atom));
00187 }
00188
00189 U_FREE(as);
00190 }
00191
00192 <span class="keywordflow">return</span> 0;
00193 err:
00194 <span class="keywordflow">return</span> ~0;
00195 }
</pre></div><hr>
<div>
<div style="text-align:left">
<a href="http://www.koanlogic.com/kl/cont/gb/html/products.html">←Products</a>
</div>
<div style="text-align:center;">
© 2005-2006 - <a href="http://www.koanlogic.com">KoanLogic S.r.l.</a> - All rights reserved
</div>
</div>
</body>
</html>
|