genericvalues_8c-source.html

来自「仿真人工智能是指用人工的方法和技术」· HTML 代码 · 共 378 行 · 第 1/2 页

HTML
378
字号
<!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>GenericValues.C Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body bgcolor="#ffffff"><!-- Generated by Doxygen 1.2.12 --><center><a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</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; <a class="qindex" href="globals.html">File Members</a> &nbsp; </center><hr><h1>GenericValues.C</h1><a href="GenericValues_8C.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">/*</font>00002 <font class="comment">Copyright (c) 2000,2001, Jelle Kok, University of Amsterdam</font>00003 <font class="comment">All rights reserved.</font>00004 <font class="comment"></font>00005 <font class="comment">Redistribution and use in source and binary forms, with or without </font>00006 <font class="comment">modification, are permitted provided that the following conditions are met:</font>00007 <font class="comment"></font>00008 <font class="comment">1. Redistributions of source code must retain the above copyright notice, this </font>00009 <font class="comment">list of conditions and the following disclaimer. </font>00010 <font class="comment"></font>00011 <font class="comment">2. Redistributions in binary form must reproduce the above copyright notice, </font>00012 <font class="comment">this list of conditions and the following disclaimer in the documentation </font>00013 <font class="comment">and/or other materials provided with the distribution. </font>00014 <font class="comment"></font>00015 <font class="comment">3. Neither the name of the University of Amsterdam nor the names of its </font>00016 <font class="comment">contributors may be used to endorse or promote products derived from this </font>00017 <font class="comment">software without specific prior written permission. </font>00018 <font class="comment"></font>00019 <font class="comment">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" </font>00020 <font class="comment">AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE </font>00021 <font class="comment">IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE </font>00022 <font class="comment">DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE </font>00023 <font class="comment">FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL </font>00024 <font class="comment">DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR </font>00025 <font class="comment">SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER </font>00026 <font class="comment">CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, </font>00027 <font class="comment">OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE </font>00028 <font class="comment">OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</font>00029 <font class="comment">*/</font>00047 <font class="preprocessor">#include "<a class="code" href="GenericValues_8h.html">GenericValues.h</a>"</font>00048 <font class="preprocessor">#include "<a class="code" href="Parse_8h.html">Parse.h</a>"</font>           <font class="comment">// needed for 'gotoFirstNonSpace'</font>00049 <font class="preprocessor">#include &lt;stdio.h&gt;</font>           <font class="comment">// needed for 'sprintf'</font>00050 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>          <font class="comment">// needed for 'free'</font>00051 <font class="preprocessor">#include &lt;string.h&gt;</font>          <font class="comment">// needed for 'strdup'</font>00052 <font class="preprocessor">#include &lt;ctype.h&gt;</font>           <font class="comment">// needed for 'isdigit'</font>00053 <font class="preprocessor">#include &lt;fstream.h&gt;</font>         <font class="comment">// needed for 'ifstream'</font>00054 00055 <font class="comment">/******************************************************************************/</font>00056 <font class="comment">/********************   CLASS GENERICVALUET   *********************************/</font>00057 <font class="comment">/******************************************************************************/</font>00058 <a name="l00068"></a><a class="code" href="classGenericValueT.html#a0">00068</a> <a class="code" href="classGenericValueT.html#a0">GenericValueT::GenericValueT</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *str, <font class="keywordtype">void</font> *vAddr, <a class="code" href="GenericValues_8h.html#a4">GenericValueKind</a> t )00069 {00070   <a class="code" href="classGenericValueT.html#o0">m_strName</a>  = strdup( str ); <font class="comment">// strdup needed to allocate right amount of memory</font>00071   <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> = vAddr;00072   <a class="code" href="classGenericValueT.html#o2">m_type</a>     = t;00073 }00074 <a name="l00077"></a><a class="code" href="classGenericValueT.html#a1">00077</a> <a class="code" href="classGenericValueT.html#a1">GenericValueT::~GenericValueT</a>( )00078 {00079   <font class="keywordflow">if</font>( m_strName )00080     free( ( <font class="keywordtype">char</font> * ) <a class="code" href="classGenericValueT.html#o0">m_strName</a> );00081 }00082 <a name="l00086"></a><a class="code" href="classGenericValueT.html#a2">00086</a> <font class="keyword">const</font> <font class="keywordtype">char</font>* <a class="code" href="classGenericValueT.html#a2">GenericValueT::getName</a> ( )00087 {00088   <font class="keywordflow">return</font> ( m_strName );00089 }00090 <a name="l00098"></a><a class="code" href="classGenericValueT.html#a3">00098</a> <font class="keywordtype">bool</font> <a class="code" href="classGenericValueT.html#a3">GenericValueT::setValue</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strValue )00099 {00100   <font class="keywordtype">bool</font> bReturn = <font class="keyword">true</font>, b = <font class="keyword">false</font>;00101 00102   <font class="comment">// determine the generic type associated with this GenericValueT</font>00103   <font class="comment">// object and apply the correct conversion of the string argument</font>00104   <font class="keywordflow">switch</font>( m_type )00105   {00106     <font class="keywordflow">case</font> GENERIC_VALUE_DOUBLE:00107       *( <font class="keywordtype">double</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> = atof( strValue ? strValue : 0 );00108       <font class="keywordflow">break</font>;00109     <font class="keywordflow">case</font> GENERIC_VALUE_STRING:00110       strcpy( ( <font class="keywordtype">char</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a>, strValue );00111       <font class="keywordflow">break</font>;00112     <font class="keywordflow">case</font> GENERIC_VALUE_BOOLEAN:00113       b = <font class="keyword">false</font>;00114       <font class="keywordflow">if</font>( !strValue )00115         ;00116       <font class="keywordflow">else</font> <font class="keywordflow">if</font>( isdigit( strValue[ 0 ] ) )00117         b = atoi( strValue ? strValue : 0 );00118       <font class="keywordflow">else</font> <font class="keywordflow">if</font>( strcasecmp( strValue, <font class="stringliteral">"on"</font>  ) == 0 ||00119                strcasecmp( strValue, <font class="stringliteral">"true"</font>) == 0 ||00120                strcasecmp( strValue, <font class="stringliteral">"yes"</font> ) == 0 )00121         b = <font class="keyword">true</font>;00122 00123       *( <font class="keywordtype">bool</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> = ( b == <font class="keyword">true</font> ) ? <font class="keyword">true</font> : <font class="keyword">false</font>;00124       <font class="keywordflow">break</font>;00125     <font class="keywordflow">case</font> GENERIC_VALUE_INTEGER:00126       *( <font class="keywordtype">int</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> = atoi( strValue ? strValue : 0 );00127       <font class="keywordflow">break</font>;00128     <font class="keywordflow">default</font>:00129       bReturn = <font class="keyword">false</font>;00130   }00131 00132   <font class="keywordflow">return</font> ( bReturn );00133 }00134 <a name="l00144"></a><a class="code" href="classGenericValueT.html#a4">00144</a> <font class="keywordtype">char</font>* <a class="code" href="classGenericValueT.html#a4">GenericValueT::getValue</a>( <font class="keywordtype">char</font> *strValue )00145 {00146   <font class="comment">// determine the generic type associated with this GenericValueT</font>00147   <font class="comment">// object and apply the correct conversion into a string</font>00148   <font class="keywordflow">switch</font>( m_type )00149   {00150     <font class="keywordflow">case</font> GENERIC_VALUE_DOUBLE:00151       sprintf( strValue, <font class="stringliteral">"%2f"</font>, *( <font class="keywordtype">double</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> );00152       <font class="keywordflow">break</font>;00153     <font class="keywordflow">case</font> GENERIC_VALUE_STRING:00154       sprintf( strValue, <font class="stringliteral">"%s"</font>, *( <font class="keywordtype">char</font> ** ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> );00155       <font class="keywordflow">break</font>;00156     <font class="keywordflow">case</font> GENERIC_VALUE_BOOLEAN:00157       sprintf( strValue, <font class="stringliteral">"%d"</font>, *( <font class="keywordtype">int</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> );00158       <font class="keywordflow">break</font>;00159     <font class="keywordflow">case</font> GENERIC_VALUE_INTEGER:00160       sprintf( strValue, <font class="stringliteral">"%d"</font>, *( <font class="keywordtype">int</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> );00161       <font class="keywordflow">break</font>;00162     <font class="keywordflow">default</font>:00163       *strValue = <font class="charliteral">'\0'</font>;00164   }00165 00166   <font class="keywordflow">return</font> ( strValue );00167 }00168 <a name="l00177"></a><a class="code" href="classGenericValueT.html#a5">00177</a> <font class="keywordtype">void</font> <a class="code" href="classGenericValueT.html#a5">GenericValueT::show</a>( ostream&amp; out, <font class="keyword">const</font> <font class="keywordtype">char</font> *strSeparator )00178 {00179   <font class="comment">// write the name associated with the variable in this GenericValueT</font>00180   <font class="comment">// object followed by the separator to the specified output stream</font>00181   out &lt;&lt; <a class="code" href="classGenericValueT.html#o0">m_strName</a> &lt;&lt; strSeparator;00182 00183   <font class="comment">// determine the generic type associated with this GenericValueT</font>00184   <font class="comment">// object and write the correct value to the specified output stream</font>00185   <font class="keywordflow">switch</font>( m_type )00186   {00187     <font class="keywordflow">case</font> GENERIC_VALUE_DOUBLE:00188       out &lt;&lt; *( <font class="keywordtype">double</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a>;00189       <font class="keywordflow">break</font>;00190     <font class="keywordflow">case</font> GENERIC_VALUE_STRING:00191       out &lt;&lt; ( <font class="keywordtype">char</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a>;00192       <font class="keywordflow">break</font>;00193     <font class="keywordflow">case</font> GENERIC_VALUE_BOOLEAN:00194       out &lt;&lt; ( ( *( <font class="keywordtype">bool</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a> == <font class="keyword">true</font> ) ? <font class="stringliteral">"true"</font> : <font class="stringliteral">"false"</font>);00195       <font class="keywordflow">break</font>;00196     <font class="keywordflow">case</font> GENERIC_VALUE_INTEGER:00197       out &lt;&lt; *( <font class="keywordtype">int</font> * ) <a class="code" href="classGenericValueT.html#o1">m_vAddress</a>;00198       <font class="keywordflow">break</font>;00199     <font class="keywordflow">default</font>:00200       <font class="keywordflow">break</font>;00201   }00202 00203   out &lt;&lt; endl;00204 }00205 00206 00207 <font class="comment">/******************************************************************************/</font>00208 <font class="comment">/********************   CLASS GENERICVALUES   *********************************/</font>00209 <font class="comment">/******************************************************************************/</font>00210 <a name="l00217"></a><a class="code" href="classGenericValues.html#a0">00217</a> <a class="code" href="classGenericValues.html#a0">GenericValues::GenericValues</a>( <font class="keywordtype">char</font> *strName, <font class="keywordtype">int</font> iMaxValues )00218 {00219   <a class="code" href="classGenericValues.html#o2">m_iValuesTotal</a> = 0;   <font class="comment">// total number of values in collection is set to zero</font>00220 00221   <font class="keywordflow">if</font>( strName )       <font class="comment">// set the name of the collection</font>00222     <a class="code" href="classGenericValues.html#o0">m_strClassName</a> = strdup( strName );00223 00224   <a class="code" href="classGenericValues.html#o3">m_iMaxGenericValues</a> = iMaxValues;00225 00226   <font class="comment">// a GenericValues object is a collection of GenericValueT objects</font>00227   <a class="code" href="classGenericValues.html#o1">m_values</a> = <font class="keyword">new</font> <a class="code" href="classGenericValueT.html">GenericValueT</a>*[ iMaxValues ];00228 }00229 <a name="l00232"></a><a class="code" href="classGenericValues.html#a1">00232</a> <a class="code" href="classGenericValues.html#a1">GenericValues::~GenericValues</a>( <font class="keywordtype">void</font> )00233 {00234   <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i = 0 ; i &lt; <a class="code" href="classGenericValues.html#a3">getValuesTotal</a>( ) ; i++ )00235     <font class="keyword">delete</font> <a class="code" href="classGenericValues.html#o1">m_values</a>[ i ];00236   <font class="keyword">delete</font> <a class="code" href="classGenericValues.html#o1">m_values</a>;00237 00238   <font class="keywordflow">if</font>( m_strClassName )00239     free( <a class="code" href="classGenericValues.html#o0">m_strClassName</a> );00240 }00241 <a name="l00245"></a><a class="code" href="classGenericValues.html#a2">00245</a> <font class="keywordtype">char</font>* <a class="code" href="classGenericValues.html#a2">GenericValues::getClassName</a>( )00246 {00247   <font class="keywordflow">return</font> ( m_strClassName );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?