00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00055 #include "GenericValues.h"
00056 #include "Parse.h"
00057 #include <stdio.h>
00058 #include <stdlib.h>
00059 #include <string.h>
00060 #include <ctype.h>
00061 #include <fstream>
00062
00063
00064
00065
00066
00076 GenericValueT::GenericValueT( const char *str, void *vAddr, GenericValueKind t)
00077 {
00078 m_strName = strdup( str );
00079 m_vAddress = vAddr;
00080 m_type = t;
00081 }
00082
00085 GenericValueT::~GenericValueT( )
00086 {
00087 if( m_strName )
00088 free( ( char * ) m_strName );
00089 }
00090
00094 const char* GenericValueT::getName ( )
00095 {
00096 return ( m_strName );
00097 }
00098
00108 bool GenericValueT::setValue( const char *strValue )
00109 {
00110 bool bReturn = true, b;
00111
00112
00113
00114 switch( m_type )
00115 {
00116 case GENERIC_VALUE_DOUBLE:
00117 *( double * ) m_vAddress = atof( strValue ? strValue : 0 );
00118 break;
00119 case GENERIC_VALUE_STRING:
00120 strcpy( ( char * ) m_vAddress, strValue );
00121 break;
00122 case GENERIC_VALUE_BOOLEAN:
00123 b = false;
00124 if( !strValue )
00125 ;
00126 else if( isdigit( strValue[ 0 ] ) )
00127 b = atoi( strValue ? strValue : 0 );
00128 #ifdef WIN32
00129 else if( strcmp( strValue, "on" ) == 0 ||
00130 strcmp( strValue, "true") == 0 ||
00131 strcmp( strValue, "yes" ) == 0 )
00132 #else
00133 else if( strcasecmp( strValue, "on" ) == 0 ||
00134 strcasecmp( strValue, "true") == 0 ||
00135 strcasecmp( strValue, "yes" ) == 0 )
00136 #endif
00137 b = true;
00138
00139 *( bool * ) m_vAddress = ( b == true ) ? true : false;
00140 break;
00141 case GENERIC_VALUE_INTEGER:
00142 *( int * ) m_vAddress = atoi( strValue ? strValue : 0 );
00143 break;
00144 default:
00145 bReturn = false;
00146 }
00147
00148 return ( bReturn );
00149 }
00150
00164 char* GenericValueT::getValue( char *strValue )
00165 {
00166
00167
00168 switch( m_type )
00169 {
00170 case GENERIC_VALUE_DOUBLE:
00171 sprintf( strValue, "%2f", *( double * ) m_vAddress );
00172 break;
00173 case GENERIC_VALUE_STRING:
00174 sprintf( strValue, "%s", ( char * ) m_vAddress );
00175 break;
00176 case GENERIC_VALUE_BOOLEAN:
00177 sprintf( strValue, "%d", *( int * ) m_vAddress );
00178 break;
00179 case GENERIC_VALUE_INTEGER:
00180 sprintf( strValue, "%d", *( int * ) m_vAddress );
00181 break;
00182 default:
00183 *strValue = '\0';
00184 }
00185
00186 return ( strValue );
00187 }
00188
00202 void GenericValueT::show( ostream& out, const char *strSeparator )
00203 {
00204
00205
00206 out << m_strName << strSeparator;
00207
00208
00209
00210 switch( m_type )
00211 {
00212 case GENERIC_VALUE_DOUBLE:
00213 out << *( double * ) m_vAddress;
00214 break;
00215 case GENERIC_VALUE_STRING:
00216 out << ( char * ) m_vAddress;
00217 break;
00218 case GENERIC_VALUE_BOOLEAN:
00219 out << ( ( *( bool * ) m_vAddress == true ) ? "true" : "false");
00220 break;
00221 case GENERIC_VALUE_INTEGER:
00222 out << *( int * ) m_vAddress;
00223 break;
00224 default:
00225 break;
00226 }
00227
00228 out << endl;
00229 }
00230
00231
00232
00233
00234
00235
00243 GenericValues::GenericValues( char *strName, int iMaxValues )
00244 {
00245 m_iValuesTotal = 0;
00246
00247 if( strName )
00248 m_strClassName = strdup( strName );
00249
00250 m_iMaxGenericValues = iMaxValues;
00251
00252
00253 m_values = new GenericValueT*[ iMaxValues ];
00254 }
00255
00258 GenericValues::~GenericValues( void )
00259 {
00260 for( int i = 0 ; i < getValuesTotal( ) ; i++ )
00261 delete m_values[ i ];
00262 delete m_values;
00263
00264 if( m_strClassName )
00265 free( m_strClassName );
00266 }
00267
00271 char* GenericValues::getClassName( )
00272 {
00273 return ( m_strClassName );
00274 }
00275
00278 int GenericValues::getValuesTotal( )
00279 {
00280 return ( m_iValuesTotal );
00281 }
00282
00297 bool GenericValues::addSetting( const char *strName, void *vAddress,
00298 GenericValueKind type )
00299 {
00300 if( getValuePtr( strName ) )
00301 {
00302 cerr << "Setting '" << strName << "' already installed." << endl;
00303 return false;
00304 }
00305 if( m_iValuesTotal == m_iMaxGenericValues )
00306 {
00307 cerr << "GenericValues::addSetting buffer for " << m_strClassName <<
00308 " is full (cannot add '" << strName << "')" << endl;
00309 return false;
00310 }
00311
00312 m_values[ m_iValuesTotal++ ] = new GenericValueT( strName, vAddress, type );
00313
00314 return ( true );
00315 }
00316
00329 GenericValueT* GenericValues::getValuePtr( const char *strName )
00330 {
00331 GenericValueT *ptr = 0;
00332
00333
00334
00335 for( int i = 0 ; i < getValuesTotal( ) ; i++ )
00336 {
00337 if( strcmp( m_values[ i ]->getName( ), strName ) == 0 )
00338 {
00339 ptr = m_values[ i ];
00340 break;
00341 }
00342 }
00343
00344 return ( ptr );
00345 }
00346
00360 char* GenericValues::getValue( const char *strName, char *strValue )
00361 {
00362 GenericValueT *parptr;
00363
00364 parptr = getValuePtr( strName );
00365
00366 if( parptr )
00367 strValue = parptr->getValue( strValue );
00368 else
00369 strValue[ 0 ] = '\0';
00370
00371 return ( strValue );
00372 }
00373
00387 bool GenericValues::setValue( const char *strName, const char *strValue )
00388 {
00389 bool bReturn = false;
00390 GenericValueT *parptr;
00391
00392 parptr = getValuePtr( strName );
00393
00394 if( parptr )
00395 bReturn = parptr->setValue( strValue );
00396
00397 return ( bReturn );
00398 }
00399
00412 bool GenericValues::readValues( const char *strFile, const char *strSeparator )
00413 {
00414 ifstream in( strFile );
00415
00416 if( !in )
00417 {
00418 cerr << "(GenericValues::readValues) Could not open file '" <<
00419 strFile << "'" << endl;
00420 return ( false );
00421 }
00422
00423 bool bReturn = true;
00424 char strLine[ 256 ], strName[ 100 ], strValue[ 100 ];
00425 char* c;
00426 int iLineNr = 0;
00427
00428
00429 while( in.getline( strLine, sizeof( strLine ) ) )
00430 {
00431 iLineNr++;
00432
00433
00434 if( !( strLine[ 0 ] == '\n' ||
00435 strLine[ 0 ] == '#' ||
00436 strLine[ 0 ] == '\0' ||
00437 ( strlen( strLine ) > 1 &&
00438 strLine[ 0 ] == ' ' &&
00439 strLine[ 1 ] == '#' ) ) )
00440 {
00441
00442 if( strSeparator && ( c = strstr( strLine, strSeparator ) ) != NULL )
00443 for( size_t i = 0; i < strlen( strSeparator ); i++ )
00444 *( c + i ) = ' ';
00445
00446
00447 if( !( sscanf( strLine, "%s%s", strName, strValue ) == 2 &&
00448 setValue( strName, strValue ) ) )
00449 {
00450 bReturn = false;
00451 cerr << "(GenericValues::readValues) '" << strFile << "' linenr "
00452 << iLineNr << ", error in '" << strLine << "'" << endl;
00453 }
00454 }
00455 }
00456
00457 return ( bReturn );
00458 }
00459
00478 bool GenericValues::saveValues( const char *strFile, const char *strSeparator,
00479 bool bAppend )
00480 {
00481 ofstream outf( strFile, ( bAppend == false ? ( ios::out )
00482 : ( ios::out | ios::app ) ) );
00483
00484 if( !outf )
00485 {
00486 cerr << "Could not open file '" << strFile << "'" << endl;
00487 return ( false );
00488 }
00489
00490
00491
00492 show( outf, strSeparator );
00493
00494 return ( true );
00495 }
00496
00509 void GenericValues::show( ostream& out, const char *strSeparator )
00510 {
00511 for( int i = 0; i < getValuesTotal( ); i++ )
00512 m_values[ i ]->show( out, strSeparator );
00513 }
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551