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
00047 #include "GenericValues.h"
00048 #include "Parse.h"
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052 #include <ctype.h>
00053 #include <fstream.h>
00054
00055
00056
00057
00058
00068 GenericValueT::GenericValueT( const char *str, void *vAddr, GenericValueKind t )
00069 {
00070 m_strName = strdup( str );
00071 m_vAddress = vAddr;
00072 m_type = t;
00073 }
00074
00077 GenericValueT::~GenericValueT( )
00078 {
00079 if( m_strName )
00080 free( ( char * ) m_strName );
00081 }
00082
00086 const char* GenericValueT::getName ( )
00087 {
00088 return ( m_strName );
00089 }
00090
00098 bool GenericValueT::setValue( const char *strValue )
00099 {
00100 bool bReturn = true, b = false;
00101
00102
00103
00104 switch( m_type )
00105 {
00106 case GENERIC_VALUE_DOUBLE:
00107 *( double * ) m_vAddress = atof( strValue ? strValue : 0 );
00108 break;
00109 case GENERIC_VALUE_STRING:
00110 strcpy( ( char * ) m_vAddress, strValue );
00111 break;
00112 case GENERIC_VALUE_BOOLEAN:
00113 b = false;
00114 if( !strValue )
00115 ;
00116 else if( isdigit( strValue[ 0 ] ) )
00117 b = atoi( strValue ? strValue : 0 );
00118 else if( strcasecmp( strValue, "on" ) == 0 ||
00119 strcasecmp( strValue, "true") == 0 ||
00120 strcasecmp( strValue, "yes" ) == 0 )
00121 b = true;
00122
00123 *( bool * ) m_vAddress = ( b == true ) ? true : false;
00124 break;
00125 case GENERIC_VALUE_INTEGER:
00126 *( int * ) m_vAddress = atoi( strValue ? strValue : 0 );
00127 break;
00128 default:
00129 bReturn = false;
00130 }
00131
00132 return ( bReturn );
00133 }
00134
00144 char* GenericValueT::getValue( char *strValue )
00145 {
00146
00147
00148 switch( m_type )
00149 {
00150 case GENERIC_VALUE_DOUBLE:
00151 sprintf( strValue, "%2f", *( double * ) m_vAddress );
00152 break;
00153 case GENERIC_VALUE_STRING:
00154 sprintf( strValue, "%s", *( char ** ) m_vAddress );
00155 break;
00156 case GENERIC_VALUE_BOOLEAN:
00157 sprintf( strValue, "%d", *( int * ) m_vAddress );
00158 break;
00159 case GENERIC_VALUE_INTEGER:
00160 sprintf( strValue, "%d", *( int * ) m_vAddress );
00161 break;
00162 default:
00163 *strValue = '\0';
00164 }
00165
00166 return ( strValue );
00167 }
00168
00177 void GenericValueT::show( ostream& out, const char *strSeparator )
00178 {
00179
00180
00181 out << m_strName << strSeparator;
00182
00183
00184
00185 switch( m_type )
00186 {
00187 case GENERIC_VALUE_DOUBLE:
00188 out << *( double * ) m_vAddress;
00189 break;
00190 case GENERIC_VALUE_STRING:
00191 out << ( char * ) m_vAddress;
00192 break;
00193 case GENERIC_VALUE_BOOLEAN:
00194 out << ( ( *( bool * ) m_vAddress == true ) ? "true" : "false");
00195 break;
00196 case GENERIC_VALUE_INTEGER:
00197 out << *( int * ) m_vAddress;
00198 break;
00199 default:
00200 break;
00201 }
00202
00203 out << endl;
00204 }
00205
00206
00207
00208
00209
00210
00217 GenericValues::GenericValues( char *strName, int iMaxValues )
00218 {
00219 m_iValuesTotal = 0;
00220
00221 if( strName )
00222 m_strClassName = strdup( strName );
00223
00224 m_iMaxGenericValues = iMaxValues;
00225
00226
00227 m_values = new GenericValueT*[ iMaxValues ];
00228 }
00229
00232 GenericValues::~GenericValues( void )
00233 {
00234 for( int i = 0 ; i < getValuesTotal( ) ; i++ )
00235 delete m_values[ i ];
00236 delete m_values;
00237
00238 if( m_strClassName )
00239 free( m_strClassName );
00240 }
00241
00245 char* GenericValues::getClassName( )
00246 {
00247 return ( m_strClassName );
00248 }
00249
00252 int GenericValues::getValuesTotal( )
00253 {
00254 return ( m_iValuesTotal );
00255 }
00256
00266 bool GenericValues::addSetting( const char *strName, void *vAddress,
00267 GenericValueKind type )
00268 {
00269 if( getValuePtr( strName ) )
00270 {
00271 cerr << "Setting '" << strName << "' already installed." << endl;
00272 return false;
00273 }
00274 if( m_iValuesTotal == m_iMaxGenericValues )
00275 {
00276 cerr << "GenericValues::addSetting buffer for " << m_strClassName <<
00277 " is full (cannot add '" << strName << "')" << endl;
00278 return false;
00279 }
00280
00281 m_values[ m_iValuesTotal++ ] = new GenericValueT( strName, vAddress, type );
00282
00283 return ( true );
00284 }
00285
00294 GenericValueT* GenericValues::getValuePtr( const char *strName )
00295 {
00296 GenericValueT *ptr = 0;
00297
00298
00299
00300 for( int i = 0 ; i < getValuesTotal( ) ; i++ )
00301 {
00302 if( strcmp( m_values[ i ]->getName( ), strName ) == 0 )
00303 {
00304 ptr = m_values[ i ];
00305 break;
00306 }
00307 }
00308
00309 return ( ptr );
00310 }
00311
00325 char* GenericValues::getValue( const char *strName, char *strValue )
00326 {
00327 GenericValueT *parptr;
00328
00329 parptr = getValuePtr( strName );
00330
00331 if( parptr )
00332 strValue = parptr->getValue( strValue );
00333 else
00334 strValue[ 0 ] = '\0';
00335
00336 return ( strValue );
00337 }
00338
00348 bool GenericValues::setValue( const char *strName, const char *strValue )
00349 {
00350 bool bReturn = false;
00351 GenericValueT *parptr;
00352
00353 parptr = getValuePtr( strName );
00354
00355 if( parptr )
00356 bReturn = parptr->setValue( strValue );
00357
00358 return ( bReturn );
00359 }
00360
00372 bool GenericValues::readValues( const char *strFile, const char *strSeparator )
00373 {
00374 ifstream in( strFile );
00375
00376 if( !in )
00377 {
00378 cerr << "(GenericValues::readValues) Could not open file '" <<
00379 strFile << "'" << endl;
00380 return ( false );
00381 }
00382
00383 bool bReturn = true;
00384 char strLine[ 256 ], strName[ 100 ], strValue[ 100 ];
00385 char* c;
00386 int iLineNr = 0;
00387
00388
00389 while( in.getline( strLine, sizeof( strLine ) ) )
00390 {
00391 iLineNr++;
00392
00393
00394 if( !( strLine[ 0 ] == '\n' ||
00395 strLine[ 0 ] == '#' ||
00396 strLine[ 0 ] == '\0' ||
00397 ( strlen( strLine ) > 1 &&
00398 strLine[ 0 ] == ' ' &&
00399 strLine[ 1 ] == '#' ) ) )
00400 {
00401
00402 if( strSeparator && ( c = strstr( strLine, strSeparator ) ) != NULL )
00403 for( size_t i = 0; i < strlen( strSeparator ); i++ )
00404 *( c + i ) = ' ';
00405
00406
00407 if( !( sscanf( strLine, "%s%s", strName, strValue ) == 2 &&
00408 setValue( strName, strValue ) ) )
00409 {
00410 bReturn = false;
00411 cerr << "(GenericValues::readValues) '" << strFile << "' linenr "
00412 << iLineNr << ", error in '" << strLine << "'" << endl;
00413 }
00414 }
00415 }
00416
00417 return ( bReturn );
00418 }
00419
00433 bool GenericValues::saveValues( const char *strFile, const char *strSeparator,
00434 bool bAppend )
00435 {
00436 ofstream outf( strFile, ( bAppend == false ? ( ios::out )
00437 : ( ios::out | ios::app ) ) );
00438
00439 if( !outf )
00440 {
00441 cerr << "Could not open file '" << strFile << "'" << endl;
00442 return ( false );
00443 }
00444
00445
00446
00447 show( outf, strSeparator );
00448
00449 return ( true );
00450 }
00451
00460 void GenericValues::show( ostream& out, const char *strSeparator )
00461 {
00462 for( int i = 0; i < getValuesTotal( ); i++ )
00463 m_values[ i ]->show( out, strSeparator );
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501