genericvalues_8c-source.html
来自「仿真人工智能是指用人工的方法和技术」· HTML 代码 · 共 378 行 · 第 1/2 页
HTML
378 行
00248 }00249 <a name="l00252"></a><a class="code" href="classGenericValues.html#a3">00252</a> <font class="keywordtype">int</font> <a class="code" href="classGenericValues.html#a3">GenericValues::getValuesTotal</a>( )00253 {00254 <font class="keywordflow">return</font> ( m_iValuesTotal );00255 }00256 <a name="l00266"></a><a class="code" href="classGenericValues.html#a4">00266</a> <font class="keywordtype">bool</font> <a class="code" href="classGenericValues.html#a4">GenericValues::addSetting</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strName, <font class="keywordtype">void</font> *vAddress,00267 <a class="code" href="GenericValues_8h.html#a4">GenericValueKind</a> type )00268 {00269 <font class="keywordflow">if</font>( <a class="code" href="classGenericValues.html#c0">getValuePtr</a>( strName ) ) <font class="comment">// setting already installed</font>00270 {00271 cerr << <font class="stringliteral">"Setting '"</font> << strName << <font class="stringliteral">"' already installed."</font> << endl;00272 <font class="keywordflow">return</font> <font class="keyword">false</font>;00273 }00274 <font class="keywordflow">if</font>( <a class="code" href="classGenericValues.html#o2">m_iValuesTotal</a> == <a class="code" href="classGenericValues.html#o3">m_iMaxGenericValues</a> ) <font class="comment">// buffer is full</font>00275 {00276 cerr << <font class="stringliteral">"GenericValues::addSetting buffer for "</font> << <a class="code" href="classGenericValues.html#o0">m_strClassName</a> <<00277 <font class="stringliteral">" is full (cannot add '"</font> << strName << <font class="stringliteral">"')"</font> << endl;00278 <font class="keywordflow">return</font> <font class="keyword">false</font>;00279 }00280 00281 <a class="code" href="classGenericValues.html#o1">m_values</a>[ <a class="code" href="classGenericValues.html#o2">m_iValuesTotal</a>++ ] = <font class="keyword">new</font> <a class="code" href="classGenericValueT.html">GenericValueT</a>( strName, vAddress, type );00282 00283 <font class="keywordflow">return</font> ( true );00284 }00285 <a name="l00294"></a><a class="code" href="classGenericValues.html#c0">00294</a> <a class="code" href="classGenericValueT.html">GenericValueT</a>* <a class="code" href="classGenericValues.html#c0">GenericValues::getValuePtr</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strName )00295 {00296 <a class="code" href="classGenericValueT.html">GenericValueT</a> *ptr = 0;00297 00298 <font class="comment">// search through the collection for a GenericValueT object of which the name</font>00299 <font class="comment">// associated with the variable matches the argument passed to the method</font>00300 <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i = 0 ; i < <a class="code" href="classGenericValues.html#a3">getValuesTotal</a>( ) ; i++ )00301 {00302 <font class="keywordflow">if</font>( strcmp( <a class="code" href="classGenericValues.html#o1">m_values</a>[ i ]->getName( ), strName ) == 0 )00303 {00304 ptr = <a class="code" href="classGenericValues.html#o1">m_values</a>[ i ];00305 <font class="keywordflow">break</font>;00306 }00307 }00308 00309 <font class="keywordflow">return</font> ( ptr );00310 }00311 <a name="l00325"></a><a class="code" href="classGenericValues.html#a5">00325</a> <font class="keywordtype">char</font>* <a class="code" href="classGenericValues.html#a5">GenericValues::getValue</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strName, <font class="keywordtype">char</font> *strValue )00326 {00327 <a class="code" href="classGenericValueT.html">GenericValueT</a> *parptr;00328 00329 parptr = <a class="code" href="classGenericValues.html#c0">getValuePtr</a>( strName );00330 00331 <font class="keywordflow">if</font>( parptr )00332 strValue = parptr-><a class="code" href="classGenericValueT.html#a4">getValue</a>( strValue ); <font class="comment">// this method returns a string</font>00333 <font class="keywordflow">else</font>00334 strValue[ 0 ] = <font class="charliteral">'\0'</font>;00335 00336 <font class="keywordflow">return</font> ( strValue );00337 }00338 <a name="l00348"></a><a class="code" href="classGenericValues.html#a6">00348</a> <font class="keywordtype">bool</font> <a class="code" href="classGenericValues.html#a6">GenericValues::setValue</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strName, <font class="keyword">const</font> <font class="keywordtype">char</font> *strValue )00349 {00350 <font class="keywordtype">bool</font> bReturn = <font class="keyword">false</font>;00351 <a class="code" href="classGenericValueT.html">GenericValueT</a> *parptr;00352 00353 parptr = <a class="code" href="classGenericValues.html#c0">getValuePtr</a>( strName );00354 00355 <font class="keywordflow">if</font>( parptr )00356 bReturn = parptr-><a class="code" href="classGenericValueT.html#a3">setValue</a>( strValue ); <font class="comment">// string is converted to right type</font>00357 00358 <font class="keywordflow">return</font> ( bReturn );00359 }00360 <a name="l00372"></a><a class="code" href="classGenericValues.html#a7">00372</a> <font class="keywordtype">bool</font> <a class="code" href="classGenericValues.html#a7">GenericValues::readValues</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strFile, <font class="keyword">const</font> <font class="keywordtype">char</font> *strSeparator )00373 {00374 ifstream in( strFile );00375 00376 <font class="keywordflow">if</font>( !in )00377 {00378 cerr << <font class="stringliteral">"(GenericValues::readValues) Could not open file '"</font> <<00379 strFile << <font class="stringliteral">"'"</font> << endl;00380 <font class="keywordflow">return</font> ( false );00381 }00382 00383 <font class="keywordtype">bool</font> bReturn = <font class="keyword">true</font>;00384 <font class="keywordtype">char</font> strLine[ 256 ], strName[ 100 ], strValue[ 100 ];00385 <font class="keywordtype">char</font>* c;00386 <font class="keywordtype">int</font> iLineNr = 0;00387 00388 <font class="comment">// read each line in the configuration file and store the values</font>00389 <font class="keywordflow">while</font>( in.getline( strLine, <font class="keyword">sizeof</font>( strLine ) ) )00390 {00391 iLineNr++;00392 00393 <font class="comment">// skip comment lines and empty lines; " #" is for server.conf version 7.xx</font>00394 <font class="keywordflow">if</font>( !( strLine[ 0 ] == <font class="charliteral">'\n'</font> ||00395 strLine[ 0 ] == <font class="charliteral">'#'</font> ||00396 strLine[ 0 ] == <font class="charliteral">'\0'</font> ||00397 ( strlen( strLine ) > 1 &&00398 strLine[ 0 ] == <font class="charliteral">' '</font> &&00399 strLine[ 1 ] == <font class="charliteral">'#'</font> ) ) )00400 {00401 <font class="comment">// convert all characters belonging to the separator to spaces</font>00402 <font class="keywordflow">if</font>( strSeparator && ( c = strstr( strLine, strSeparator ) ) != NULL )00403 <font class="keywordflow">for</font>( size_t i = 0; i < strlen( strSeparator ); i++ )00404 *( c + i ) = <font class="charliteral">' '</font>;00405 00406 <font class="comment">// read the name and value on this line and store the value</font>00407 <font class="keywordflow">if</font>( !( sscanf( strLine, <font class="stringliteral">"%s%s"</font>, strName, strValue ) == 2 &&00408 <a class="code" href="classGenericValues.html#a6">setValue</a>( strName, strValue ) ) )00409 {00410 bReturn = <font class="keyword">false</font>;00411 cerr << <font class="stringliteral">"(GenericValues::readValues) '"</font> << strFile << <font class="stringliteral">"' linenr "</font>00412 << iLineNr << <font class="stringliteral">", error in '"</font> << strLine << <font class="stringliteral">"'"</font> << endl;00413 }00414 }00415 }00416 00417 <font class="keywordflow">return</font> ( bReturn );00418 }00419 <a name="l00433"></a><a class="code" href="classGenericValues.html#a8">00433</a> <font class="keywordtype">bool</font> <a class="code" href="classGenericValues.html#a8">GenericValues::saveValues</a>( <font class="keyword">const</font> <font class="keywordtype">char</font> *strFile, <font class="keyword">const</font> <font class="keywordtype">char</font> *strSeparator,00434 <font class="keywordtype">bool</font> bAppend )00435 {00436 ofstream outf( strFile, ( bAppend == <font class="keyword">false</font> ? ( ios::out )00437 : ( ios::out | ios::app ) ) );00438 00439 <font class="keywordflow">if</font>( !outf )00440 {00441 cerr << <font class="stringliteral">"Could not open file '"</font> << strFile << <font class="stringliteral">"'"</font> << endl;00442 <font class="keywordflow">return</font> ( false );00443 }00444 00445 <font class="comment">// values are written to file using 'show' (note that</font>00446 <font class="comment">// output stream to write to is a file in this case)</font>00447 <a class="code" href="classGenericValues.html#a9">show</a>( outf, strSeparator );00448 00449 <font class="keywordflow">return</font> ( true );00450 }00451 <a name="l00460"></a><a class="code" href="classGenericValues.html#a9">00460</a> <font class="keywordtype">void</font> <a class="code" href="classGenericValues.html#a9">GenericValues::show</a>( ostream& out, <font class="keyword">const</font> <font class="keywordtype">char</font> *strSeparator )00461 {00462 <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i = 0; i < <a class="code" href="classGenericValues.html#a3">getValuesTotal</a>( ); i++ )00463 <a class="code" href="classGenericValues.html#o1">m_values</a>[ i ]-><a class="code" href="classGenericValueT.html#a5">show</a>( out, strSeparator );00464 }00465 00466 <font class="comment">/********************** TESTING PURPOSES **************************************/</font>00467 <font class="comment">/*</font>00468 <font class="comment"></font>00469 <font class="comment">int main( void )</font>00470 <font class="comment">{</font>00471 <font class="comment"> GenericValues g;</font>00472 <font class="comment"></font>00473 <font class="comment"> int i = 10;</font>00474 <font class="comment"> char* c = "hallo";</font>00475 <font class="comment"> bool b = true;</font>00476 <font class="comment"> double d = 10.14;</font>00477 <font class="comment"></font>00478 <font class="comment"> cout << g.addSetting( "var1", &i, GENERIC_VALUE_INTEGER ) << endl;</font>00479 <font class="comment"> cout << g.addSetting( "var2", &c, GENERIC_VALUE_STRING ) << endl;</font>00480 <font class="comment"> cout << g.addSetting( "var3", &d, GENERIC_VALUE_DOUBLE ) << endl;</font>00481 <font class="comment"> cout << g.addSetting( "var4", &b, GENERIC_VALUE_BOOLEAN ) << endl;</font>00482 <font class="comment"> g.show( cout );</font>00483 <font class="comment"></font>00484 <font class="comment"> g.setValue( "var1", "11" );</font>00485 <font class="comment"> g.setValue( "var2", "hoi" );</font>00486 <font class="comment"> g.setValue( "var3", "20.2342" );</font>00487 <font class="comment"> g.setValue( "var4", "false" );</font>00488 <font class="comment"> g.show( cout );</font>00489 <font class="comment"></font>00490 <font class="comment"> g.setIntegerValue( "var1", 22 );</font>00491 <font class="comment"> g.setStringValue ( "var2", "hoi2" );</font>00492 <font class="comment"> g.setDoubleValue ( "var3", 30.2342 );</font>00493 <font class="comment"> g.setBooleanValue( "var4", true );</font>00494 <font class="comment"> g.show( cout );</font>00495 <font class="comment"></font>00496 <font class="comment"> g.readValues( "server.conf", ":" );</font>00497 <font class="comment"> g.show( cout );</font>00498 <font class="comment"></font>00499 <font class="comment"> return ( 0 );</font>00500 <font class="comment">}</font>00501 <font class="comment">*/</font></pre></div><hr><address><small>Generated on Thu Mar 7 00:37:42 2002 for UvA Trilearn 2001 by<a href="http://www.doxygen.org/index.html"><img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.12 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2001</small></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?