Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > a74ec78bdb789d910d054e3918f3f007 > files > 413

libsword1-devel-1.5.5-2mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>listkey.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>listkey.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font>
00002 <font class="comment"> *  listkey.cpp - code for base class 'ListKey'.  ListKey is the basis for all</font>
00003 <font class="comment"> *                              types of keys that have lists of specified indexes</font>
00004 <font class="comment"> *                              (e.g. a list of verses, place, etc.)</font>
00005 <font class="comment"> */</font>
00006 
00007 <font class="preprocessor">#include &lt;utilfuns.h&gt;</font>
00008 <font class="preprocessor">#include &lt;string.h&gt;</font>
00009 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00010 <font class="preprocessor">#include &lt;swkey.h&gt;</font>
00011 <font class="preprocessor">#include &lt;listkey.h&gt;</font>
00012 
00013 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font> *classes[] = {<font class="stringliteral">"ListKey"</font>, <font class="stringliteral">"SWKey"</font>, <font class="stringliteral">"SWObject"</font>, 0};
00014 <a class="code" href="class_s_w_class.html">SWClass</a> ListKey::classdef(classes);
00015 
00016 <font class="comment">/******************************************************************************</font>
00017 <font class="comment"> * ListKey Constructor - initializes instance of ListKey</font>
00018 <font class="comment"> *</font>
00019 <font class="comment"> * ENT: ikey - text key</font>
00020 <font class="comment"> */</font>
00021 
<a name="l00022"></a><a class="code" href="class_list_key.html#a0">00022</a> <a class="code" href="class_list_key.html#a0">ListKey::ListKey</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *ikey): <a class="code" href="class_s_w_key.html">SWKey</a>(ikey) {
00023         arraymax = 0;
00024         <a class="code" href="class_list_key.html#a4">ClearList</a>();
00025         init();
00026 }
00027 
00028 
00029 <a class="code" href="class_list_key.html#a0">ListKey::ListKey</a>(<a class="code" href="class_list_key.html">ListKey</a> <font class="keyword">const</font> &amp;k) : <a class="code" href="class_s_w_key.html">SWKey</a>(k.keytext) {
00030         arraymax = k.arraymax;
00031         arraypos = k.arraypos;
00032         arraycnt = k.arraycnt;
00033         array = (arraymax)?(<a class="code" href="class_s_w_key.html">SWKey</a> **)malloc(k.arraymax * <font class="keyword">sizeof</font>(<a class="code" href="class_s_w_key.html">SWKey</a> *)):0;
00034         <font class="keywordflow">for</font> (<font class="keywordtype">int</font> i = 0; i &lt; arraycnt; i++)
00035                 array[i] = k.array[i]-&gt;<a class="code" href="class_s_w_key.html#a3">clone</a>();
00036         init();
00037 }
00038 
00039 
00040 <font class="keywordtype">void</font> ListKey::init() {
00041         myclass = &amp;classdef;
00042 }
00043 
00044 
<a name="l00045"></a><a class="code" href="class_list_key.html#a3">00045</a> <a class="code" href="class_s_w_key.html">SWKey</a> *<a class="code" href="class_list_key.html#a3">ListKey::clone</a>()<font class="keyword"> const</font>
00046 <font class="keyword"></font>{
00047         <font class="keywordflow">return</font> <font class="keyword">new</font> <a class="code" href="class_list_key.html#a0">ListKey</a>(*<font class="keyword">this</font>);
00048 }
00049 
00050 <font class="comment">/******************************************************************************</font>
00051 <font class="comment"> * ListKey Destructor - cleans up instance of ListKey</font>
00052 <font class="comment"> */</font>
00053 
<a name="l00054"></a><a class="code" href="class_list_key.html#a2">00054</a> <a class="code" href="class_list_key.html#a2">ListKey::~ListKey</a>()
00055 {
00056         <a class="code" href="class_list_key.html#a4">ClearList</a>();
00057 }
00058 
00059 
00060 <font class="comment">/******************************************************************************</font>
00061 <font class="comment"> * ListKey::ClearList   - Clears out elements of list</font>
00062 <font class="comment"> */</font>
00063 
<a name="l00064"></a><a class="code" href="class_list_key.html#a4">00064</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a4">ListKey::ClearList</a>()
00065 {
00066         <font class="keywordtype">int</font> loop;
00067 
00068         <font class="keywordflow">if</font> (arraymax) {
00069                 <font class="keywordflow">for</font> (loop = 0; loop &lt; arraycnt; loop++)
00070                         <font class="keyword">delete</font> array[loop];
00071 
00072                 free(array);
00073                 arraymax  = 0;
00074         }
00075         arraycnt  = 0;
00076         arraypos  = 0;
00077         array     = 0;
00078 }
00079 
00080 
00081 <font class="comment">/******************************************************************************</font>
00082 <font class="comment"> * ListKey::copyFrom Equates this ListKey to another ListKey object</font>
00083 <font class="comment"> *</font>
00084 <font class="comment"> * ENT: ikey - other ListKey object</font>
00085 <font class="comment"> */</font>
00086 
<a name="l00087"></a><a class="code" href="class_list_key.html#a11">00087</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a11">ListKey::copyFrom</a>(<font class="keyword">const</font> <a class="code" href="class_list_key.html">ListKey</a> &amp;ikey) {
00088         <a class="code" href="class_list_key.html#a4">ClearList</a>();
00089 
00090         arraymax = ikey.<a class="code" href="class_list_key.html#n1">arraymax</a>;
00091         arraypos = ikey.<a class="code" href="class_list_key.html#n0">arraypos</a>;
00092         arraycnt = ikey.<a class="code" href="class_list_key.html#n2">arraycnt</a>;
00093         array = (arraymax)?(<a class="code" href="class_s_w_key.html">SWKey</a> **)malloc(ikey.<a class="code" href="class_list_key.html#n1">arraymax</a> * <font class="keyword">sizeof</font>(<a class="code" href="class_s_w_key.html">SWKey</a> *)):0;
00094         <font class="keywordflow">for</font> (<font class="keywordtype">int</font> i = 0; i &lt; arraycnt; i++)
00095                 array[i] = ikey.<a class="code" href="class_list_key.html#n3">array</a>[i]-&gt;<a class="code" href="class_s_w_key.html#a3">clone</a>();
00096 
00097         <a class="code" href="class_list_key.html#a7">SetToElement</a>(0);
00098 }
00099 
00100 
00101 <font class="comment">/******************************************************************************</font>
00102 <font class="comment"> * ListKey::add - Adds an element to the list</font>
00103 <font class="comment"> */</font>
00104 
00105 <font class="keywordtype">void</font> ListKey::add(<font class="keyword">const</font> <a class="code" href="class_s_w_key.html">SWKey</a> &amp;ikey) {
00106         <font class="keywordflow">if</font> (++arraycnt &gt; arraymax) {
00107                 array = (<a class="code" href="class_s_w_key.html">SWKey</a> **) ((array) ? realloc(array, (arraycnt + 32) * <font class="keyword">sizeof</font>(<a class="code" href="class_s_w_key.html">SWKey</a> *)) : calloc(arraycnt + 32, sizeof(<a class="code" href="class_s_w_key.html">SWKey</a> *)));
00108                 arraymax = arraycnt + 32;
00109         }
00110         array[arraycnt-1] = ikey.<a class="code" href="class_s_w_key.html#a3">clone</a>();
00111         <a class="code" href="class_list_key.html#a7">SetToElement</a>(arraycnt-1);
00112 }
00113 
00114 
00115 
00116 <font class="comment">/******************************************************************************</font>
00117 <font class="comment"> * ListKey::setPosition(SW_POSITION)    - Positions this key</font>
00118 <font class="comment"> *</font>
00119 <font class="comment"> * ENT: p       - position</font>
00120 <font class="comment"> *</font>
00121 <font class="comment"> * RET: *this</font>
00122 <font class="comment"> */</font>
00123 
<a name="l00124"></a><a class="code" href="class_list_key.html#a13">00124</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a13">ListKey::setPosition</a>(SW_POSITION p) {
00125         <font class="keywordflow">switch</font> (p) {
00126         <font class="keywordflow">case</font> 1: <font class="comment">// GCC won't compile P_TOP</font>
00127                 <a class="code" href="class_list_key.html#a7">SetToElement</a>(0);
00128                 <font class="keywordflow">break</font>;
00129         <font class="keywordflow">case</font> 2: <font class="comment">// GCC won't compile P_BOTTOM</font>
00130                 <a class="code" href="class_list_key.html#a7">SetToElement</a>(arraycnt-1);
00131                 <font class="keywordflow">break</font>;
00132         }
00133 }
00134 
00135 
00136 <font class="comment">/******************************************************************************</font>
00137 <font class="comment"> * ListKey::increment - Increments a number of elements</font>
00138 <font class="comment"> */</font>
00139 
<a name="l00140"></a><a class="code" href="class_list_key.html#a15">00140</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a15">ListKey::increment</a>(<font class="keywordtype">int</font> step) {
00141         <font class="keywordflow">if</font> (step &lt; 0) {
00142                 <a class="code" href="class_list_key.html#a14">decrement</a>(step*-1);
00143                 <font class="keywordflow">return</font>;
00144         }
00145         <a class="code" href="class_s_w_key.html#a6">Error</a>();                <font class="comment">// clear error</font>
00146         <font class="keywordflow">for</font>(; step &amp;&amp; !<a class="code" href="class_s_w_key.html#a6">Error</a>(); step--) {
00147                 <font class="keywordflow">if</font> (arraypos &lt; arraycnt) {
00148                         (*(array[arraypos]))++;
00149                         <font class="keywordflow">if</font> (array[arraypos]-&gt;Error()) {
00150                                 <a class="code" href="class_list_key.html#a7">SetToElement</a>(arraypos+1);
00151                         }
00152                         <font class="keywordflow">else</font> *<font class="keyword">this</font> = (<font class="keyword">const</font> <font class="keywordtype">char</font> *)(*array[arraypos]);
00153                 }
00154                 <font class="keywordflow">else</font> error = KEYERR_OUTOFBOUNDS;
00155         }
00156 }
00157 
00158 
00159 <font class="comment">/******************************************************************************</font>
00160 <font class="comment"> * ListKey::decrement - Decrements a number of elements</font>
00161 <font class="comment"> */</font>
00162 
<a name="l00163"></a><a class="code" href="class_list_key.html#a14">00163</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a14">ListKey::decrement</a>(<font class="keywordtype">int</font> step) {
00164         <font class="keywordflow">if</font> (step &lt; 0) {
00165                 <a class="code" href="class_list_key.html#a15">increment</a>(step*-1);
00166                 <font class="keywordflow">return</font>;
00167         }
00168         <a class="code" href="class_s_w_key.html#a6">Error</a>();                <font class="comment">// clear error</font>
00169         <font class="keywordflow">for</font>(; step &amp;&amp; !<a class="code" href="class_s_w_key.html#a6">Error</a>(); step--) {
00170                 <font class="keywordflow">if</font> (arraypos &gt; -1) {
00171                         (*(array[arraypos]))--;
00172                         <font class="keywordflow">if</font> (array[arraypos]-&gt;Error()) {
00173                                 <a class="code" href="class_list_key.html#a7">SetToElement</a>(arraypos-1, BOTTOM);
00174                         }
00175                         <font class="keywordflow">else</font> *<font class="keyword">this</font> = (<font class="keyword">const</font> <font class="keywordtype">char</font> *)(*array[arraypos]);
00176                 }
00177                 <font class="keywordflow">else</font> error = KEYERR_OUTOFBOUNDS;
00178         }
00179 }
00180 
00181 
00182 <font class="comment">/******************************************************************************</font>
00183 <font class="comment"> * ListKey::Count       - Returns number of elements in list</font>
00184 <font class="comment"> */</font>
00185 
<a name="l00186"></a><a class="code" href="class_list_key.html#a5">00186</a> <font class="keywordtype">int</font> <a class="code" href="class_list_key.html#a5">ListKey::Count</a>() {
00187         <font class="keywordflow">return</font> arraycnt;
00188 }
00189 
00190 
00191 <font class="comment">/******************************************************************************</font>
00192 <font class="comment"> * ListKey::SetToElement        - Sets key to element number</font>
00193 <font class="comment"> *</font>
00194 <font class="comment"> * ENT: ielement        - element number to set to</font>
00195 <font class="comment"> *</font>
00196 <font class="comment"> * RET: error status</font>
00197 <font class="comment"> */</font>
00198 
<a name="l00199"></a><a class="code" href="class_list_key.html#a7">00199</a> <font class="keywordtype">char</font> <a class="code" href="class_list_key.html#a7">ListKey::SetToElement</a>(<font class="keywordtype">int</font> ielement, SW_POSITION pos) {
00200         arraypos = ielement;
00201         <font class="keywordflow">if</font> (arraypos &gt;= arraycnt) {
00202                 arraypos = (arraycnt&gt;0)?arraycnt - 1:0;
00203                 error = KEYERR_OUTOFBOUNDS;
00204         }
00205         <font class="keywordflow">else</font> {
00206                 <font class="keywordflow">if</font> (arraypos &lt; 0) {
00207                         arraypos = 0;
00208                         error = KEYERR_OUTOFBOUNDS;
00209                 }
00210                 <font class="keywordflow">else</font> {
00211                         error = 0;
00212                 }
00213         }
00214         
00215         <font class="keywordflow">if</font> (arraycnt) {
00216                 (*array[arraypos]) = pos;
00217                 *<font class="keyword">this</font> = (<font class="keyword">const</font> <font class="keywordtype">char</font> *)(*array[arraypos]);
00218         }
00219         <font class="keywordflow">else</font> *<font class="keyword">this</font> = <font class="stringliteral">""</font>;
00220         
00221         <font class="keywordflow">return</font> error;
00222 }
00223 
00224 
00225 <font class="comment">/******************************************************************************</font>
00226 <font class="comment"> * ListKey::GetElement  - Gets a key element number</font>
00227 <font class="comment"> *</font>
00228 <font class="comment"> * ENT: pos     - element number to get (or default current)</font>
00229 <font class="comment"> *</font>
00230 <font class="comment"> * RET: Key or null on error</font>
00231 <font class="comment"> */</font>
00232 
<a name="l00233"></a><a class="code" href="class_list_key.html#a8">00233</a> <a class="code" href="class_s_w_key.html">SWKey</a> *<a class="code" href="class_list_key.html#a8">ListKey::GetElement</a>(<font class="keywordtype">int</font> pos) {
00234         <font class="keywordflow">if</font> (pos &gt;=0) {
00235                 <font class="keywordflow">if</font> (pos &gt;=arraycnt)
00236                         error = KEYERR_OUTOFBOUNDS;
00237         }
00238         <font class="keywordflow">else</font> pos = arraypos;
00239         <font class="keywordflow">return</font> (error) ? 0:array[pos];
00240 }
00241         
00242 
00243 <font class="comment">/******************************************************************************</font>
00244 <font class="comment"> * ListKey::Remove      - Removes current element from list</font>
00245 <font class="comment"> */</font>
00246 
<a name="l00247"></a><a class="code" href="class_list_key.html#a6">00247</a> <font class="keywordtype">void</font> <a class="code" href="class_list_key.html#a6">ListKey::Remove</a>() {
00248         <font class="keywordflow">if</font> ((arraypos &gt; -1) &amp;&amp; (arraypos &lt; arraycnt)) {
00249                 <font class="keyword">delete</font> array[arraypos];
00250                 <font class="keywordflow">if</font> (arraypos &lt; arraycnt - 1)
00251                         memmove(&amp;array[arraypos], &amp;array[arraypos+1], (arraycnt - arraypos - 1) * <font class="keyword">sizeof</font>(<a class="code" href="class_s_w_key.html">SWKey</a> *));
00252                 arraycnt--;
00253                 
00254                 <a class="code" href="class_list_key.html#a7">SetToElement</a>((arraypos)?arraypos-1:0);
00255         }
00256 }
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:12:59 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>