First the .h
Code: Select all
#pragma once
namespace TA3D
{
namespace UTILS
{
class cConfig
{
private:
typedef struct m_Key
{
String KeyName;
String KeyValue;
m_Key()
{
KeyName = String( "" );
KeyValue = String( "" );
}
};
typedef std::vector< m_Key * > KeyList;
typedef KeyList::iterator KeyItor;
String m_sConfigFile;
KeyList m_Keys;
bool m_Dirty;
m_Key *GetKey( const String &KeyName );
m_Key *GetOrCreateKey( const String &KeyName );
public:
cConfig( const String &FileName );
~cConfig();
bool EraseKey( const String &name );
void SetKey( const String &name, const real32 v );
void SetKey( const String &name, const real64 v );
void SetKey( const String &name, const sint32 v );
void SetKey( const String &name, const bool v );
void SetKey( const String &name, const String &v );
real32 KeyValueFloat( const String &name );
real64 KeyValueDouble( const String &name );
sint32 KeyValueInt( const String &name );
bool KeyValueBool( const String &name );
String KeyValueString( const String &name );
void SaveConfigFile();
void ReadConfigFile();
}; // class cConfig
} // namespace UTILS
} // namespace TA3D
Code: Select all
#include "stdafx.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include "TA3D_NameSpace.h"
#include "taconfig.h"
using namespace TA3D::UTILS;
cConfig::cConfig( const String &FileName ) : m_sConfigFile( FileName )
{
m_Dirty = false;
}
cConfig::~cConfig()
{
if( m_Dirty )
SaveConfigFile();
// cleanup
}
TA3D::UTILS::cConfig::m_Key *cConfig::GetKey( const String &KeyName )
{
KeyItor k_Position;
m_Key *Result = NULL;
for( k_Position = this->m_Keys.begin(); k_Position != this->m_Keys.end(); k_Position++ )
{
if( (*k_Position)->KeyName == KeyName )
{
Result =(*k_Position);
break;
}
}
return Result;
}
TA3D::UTILS::cConfig::m_Key *cConfig::GetOrCreateKey( const String &KeyName )
{
KeyItor k_Position;
m_Key *Result = GetKey( KeyName );
if( Result == NULL )
{
Result = new m_Key;
Result->KeyName = KeyName;
}
return Result;
}
bool cConfig::EraseKey( const String &name )
{
KeyItor k_Position;
String KeyName = Lowercase( name );
bool Result = false;
for( k_Position = m_Keys.begin(); k_Position != m_Keys.end(); k_Position++ )
{
if( (*k_Position)->KeyName == KeyName )
{
m_Dirty = true;
delete( *k_Position );
m_Keys.erase( k_Position );
Result = true;
break;
}
}
return Result;
}
void cConfig::SetKey( const String &name, const real32 v ) // float
{
std::stringstream s;
m_Key *Key = GetOrCreateKey( Lowercase( name ) );
s<<v;
Key->KeyValue=s.str();
m_Dirty = true;
}
void cConfig::SetKey( const String &name, const real64 v ) // double
{
std::stringstream s;
m_Key *Key = GetOrCreateKey( Lowercase( name ) );
s<<v;
Key->KeyValue=s.str();
m_Dirty = true;
}
void cConfig::SetKey( const String &name, const bool v ) // bool
{
std::stringstream s;
m_Key *Key = GetOrCreateKey( Lowercase( name ) );
s<<( (v) ? "true" : "false" );
Key->KeyValue=s.str();
m_Dirty = true;
}
void cConfig::SetKey( const String &name, const sint32 v ) // signed int 32
{
std::stringstream s;
m_Key *Key = GetOrCreateKey( Lowercase( name ) );
s<<v;
Key->KeyValue=s.str();
m_Dirty = true;
}
void cConfig::SetKey( const String &name, const String &v ) // string
{
m_Key *Key = GetOrCreateKey( Lowercase( name ) );
Key->KeyValue=String( v );
m_Dirty = true;
}
real32 cConfig::KeyValueFloat( const String &name )
{
m_Key *Key = GetKey( Lowercase( name ) );
return ( (Key==NULL) ? 0.0f : (float)atof( Key->KeyValue.c_str() ) );
}
real64 cConfig::KeyValueDouble( const String &name )
{
m_Key *Key = GetKey( Lowercase( name ) );
return ( (Key==NULL) ? (double)0.0 : (double)atof( Key->KeyValue.c_str() ) );
}
sint32 cConfig::KeyValueInt( const String &name )
{
m_Key *Key = GetKey( Lowercase( name ) );
return ( (Key==NULL) ? 0 : (sint32)atoi( Key->KeyValue.c_str() ) );
}
bool cConfig::KeyValueBool( const String &name )
{
m_Key *Key = GetKey( Lowercase( name ) );
return ( (Key==NULL) ? false : Key->KeyValue=="true" );
}
String cConfig::KeyValueString( const String &name )
{
m_Key *Key = GetKey( Lowercase( name ) );
return ( (Key==NULL) ? "" : Key->KeyValue );
}
void cConfig::ReadConfigFile()
{
std::ifstream file( m_sConfigFile.c_str(), ios::in );
if( !file.is_open() ) return;
String line, name, value;
int posEqual;
while ( !file.eof() )
{
std::getline( file, line, '\n' );
if( !line.length() ) continue;
if (line[0] == '#' || line[0] == ';' ) continue;
if( (posEqual=(int)line.find( '=' )) != string::npos )
{
name = TrimString( line.substr( 0,posEqual ) );
value = TrimString( line.substr( posEqual+1 ) );
m_Key *Key=GetOrCreateKey( Lowercase( name ) );
Key->KeyValue = String( value );
}
}
file.close();
}
void cConfig::SaveConfigFile()
{
std::ofstream file( m_sConfigFile.c_str(), ios::out | ios::trunc );
if( !file.is_open() ) return;
KeyItor k_Position;
for( k_Position = this->m_Keys.begin(); k_Position != this->m_Keys.end(); k_Position++ )
file << (*k_Position)->KeyName << "=" << (*k_Position)->KeyValue << "\n";
file.close();
}
I also need to add some comments to the code thou it should be pretty easy for most to follow. The std headers should also be moved into stdafx.h as other files will probably make use of them.
I move the translation code into its own files, and will post a rewrite of them tonight/tommorow.
Final draft of this code should be aviable by otnight which i'll also post.
++Cire.