📄 genericvalues.cpp
字号:
\return the number of generic values stored in the collection so far */
int GenericValues::getValuesTotal( )
{
return ( m_iValuesTotal );
}
/*! This method adds a new generic value to the collection and
provides a connection between a string name and the variable name
of the generic value.
\param strName a string denoting the name associated with the
variable of the generic value which is added (the variable can be
reached through this name)
\param vAddress a (void) pointer to the actual variable itself
\param type the (generic) type of the variable which is added
\return a boolean indicating whether the generic value has been succesfully
added */
bool GenericValues::addSetting( const char *strName, void *vAddress,
GenericValueKind type )
{
if( getValuePtr( strName ) ) // setting already installed
{
cerr << "Setting '" << strName << "' already installed." << endl;
return false;
}
if( m_iValuesTotal == m_iMaxGenericValues ) // buffer is full
{
cerr << "GenericValues::addSetting buffer for " << m_strClassName <<
" is full (cannot add '" << strName << "')" << endl;
return false;
}
m_values[ m_iValuesTotal++ ] = new GenericValueT( strName, vAddress, type );
return ( true );
}
/*! This (private) method returns a pointer to the GenericValueT
object of which the name associated with the variable matches the
argument passed to the method.
\param strName a string denoting the name associated with the
variable of the GenericValueT object to which a pointer should be
returned
\return a pointer to the GenericValueT object of which the name
associated with the variable matches the argument; 0 when a
GenericValueT object with a variable associated with the given
name does not exist */
GenericValueT* GenericValues::getValuePtr( const char *strName )
{
GenericValueT *ptr = 0;
// search through the collection for a GenericValueT object of which the name
// associated with the variable matches the argument passed to the method
for( int i = 0 ; i < getValuesTotal( ) ; i++ )
{
if( strcmp( m_values[ i ]->getName( ), strName ) == 0 )
{
ptr = m_values[ i ];
break;
}
}
return ( ptr );
}
/*! This method determines the value of the variable of which the name
associated with it matches the first argument to the method. The value is
converted to a string which is put into the second argument (note that
enough memory must be allocated for this char*). This same string is also
returned by the method. This enables you to use the method as an argument
to a function such as 'strcpy'.
\param strName a string denoting the name associated with the variable of
which the value should be returned
\param strValue a string which after the method call will contain the value
of the variable of which the name associated with it matches the first
argument to the method
\return a string containing the value of the variable of which the name
associated with it matches the first argument to the method */
char* GenericValues::getValue( const char *strName, char *strValue )
{
GenericValueT *parptr;
parptr = getValuePtr( strName );
if( parptr )
strValue = parptr->getValue( strValue ); // this method returns a string
else
strValue[ 0 ] = '\0';
return ( strValue );
}
/*! This method sets the variable of which the name associated with it
matches the first argument to the method to the value indicated by
the second argument. The value is always supplied as a string
which is then converted into the right type for the generic value
in question.
\param strName a string denoting the name associated with the
variable which must be set
\param strValue a string denoting the value to which the variable
indicated by the first argument should be set
\return a boolean indicating whether the update was successful */
bool GenericValues::setValue( const char *strName, const char *strValue )
{
bool bReturn = false;
GenericValueT *parptr;
parptr = getValuePtr( strName );
if( parptr )
bReturn = parptr->setValue( strValue ); // string is converted to its type
return ( bReturn );
}
/*! This method reads generic values from a file indicated by the
first argument to the method and stores them in the collection. In
this file the names associated with each value must be listed
first followed by a separator and then the value. The names
associated with each value should match the string names with
which the corresponding generic value is added using 'addSetting'.
\param strFile a string denoting the name of the file from which the values
must be read
\param strSeparator a string representing the separator which is written
between name and value in the configuration file
\return a boolean indicating whether the values have been read correctly */
bool GenericValues::readValues( const char *strFile, const char *strSeparator )
{
ifstream in( strFile );
if( !in )
{
cerr << "(GenericValues::readValues) Could not open file '" <<
strFile << "'" << endl;
return ( false );
}
bool bReturn = true;
char strLine[ 256 ], strName[ 100 ], strValue[ 100 ];
char* c;
int iLineNr = 0;
// read each line in the configuration file and store the values
while( in.getline( strLine, sizeof( strLine ) ) )
{
iLineNr++;
// skip comment lines and empty lines; " #" is for server.conf version 7.xx
if( !( strLine[ 0 ] == '\n' ||
strLine[ 0 ] == '#' ||
strLine[ 0 ] == '\0' ||
( strlen( strLine ) > 1 &&
strLine[ 0 ] == ' ' &&
strLine[ 1 ] == '#' ) ) )
{
// convert all characters belonging to the separator to spaces
if( strSeparator && ( c = strstr( strLine, strSeparator ) ) != NULL )
for( size_t i = 0; i < strlen( strSeparator ); i++ )
*( c + i ) = ' ';
// read the name and value on this line and store the value
if( !( sscanf( strLine, "%s%s", strName, strValue ) == 2 &&
setValue( strName, strValue ) ) )
{
bReturn = false;
cerr << "(GenericValues::readValues) '" << strFile << "' linenr "
<< iLineNr << ", error in '" << strLine << "'" << endl;
}
}
}
return ( bReturn );
}
/*! This method writes the generic values in the current collection to
a file indicated by the first argument to the method. The string
name associated with the value and the value itself are separated
by a separator which is specified as the second argument.
\param strFile a string denoting the name of the file to which the
generic values should be written
\param strSeparator a string representing the separator which
should be written between the name associated with each value and
the value itself
\param bAppend a boolean indicating whether the values should be
appended to the given file (=true) or if current contents of the
file should be overwritten (=false)
\return a boolean indicating whether the values in the current
collection have been correctly written to the given file */
bool GenericValues::saveValues( const char *strFile, const char *strSeparator,
bool bAppend )
{
ofstream outf( strFile, ( bAppend == false ? ( ios::out )
: ( ios::out | ios::app ) ) );
if( !outf )
{
cerr << "Could not open file '" << strFile << "'" << endl;
return ( false );
}
// values are written to file using 'show' (note that
// output stream to write to is a file in this case)
show( outf, strSeparator );
return ( true );
}
/*! This display method writes all the generic values in the current
collection to the output stream indicated by the first argument to
the method. The name associated with each value and the value
itself are separated by a separator which is specified as the
second argument.
\param out the output stream to which the current collection of
generic values should be written
\param strSeparator a string representing the separator which
should be written between the name associated with each value and
the value itself */
void GenericValues::show( ostream& out, const char *strSeparator )
{
for( int i = 0; i < getValuesTotal( ); i++ )
m_values[ i ]->show( out, strSeparator );
}
/********************* TESTING PURPOSES **************************************/
/*
int main( void )
{
GenericValues g("ServerSettings", 6);
int i = 10;
char c[256];
sprintf( c, "hallo" );
bool b = true;
double d = 10.14;
cout << g.addSetting( "var1", &i, GENERIC_VALUE_INTEGER ) << endl;
cout << g.addSetting( "var2", c, GENERIC_VALUE_STRING ) << endl;
cout << g.addSetting( "var3", &d, GENERIC_VALUE_DOUBLE ) << endl;
cout << g.addSetting( "var4", &b, GENERIC_VALUE_BOOLEAN ) << endl;
g.show( cout, ":" );
g.setValue( "var1", "11" );
g.setValue( "var2", "hoi" );
g.setValue( "var3", "20.2342" );
g.setValue( "var4", "false" );
g.show( cout, ":" );
// g.setIntegerValue( "var1", 22 );
// g.setStringValue ( "var2", "hoi2" );
// g.setDoubleValue ( "var3", 30.2342 );
// g.setBooleanValue( "var4", true );
// g.show( cout, ":" );
// g.readValues( "server.conf", ":" );
// g.show( cout, ":" );
return ( 0 );
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -