Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

Cfg::Handler Class Reference

#include <CfgHandler.h>


Public Member Functions

 Handler (const string &aFile="")
virtual ~Handler (void)
unsigned long GetSize (void) const
 the amoun of element in the cfg file.

const string & GetFilename (void) const
 the filename of the cf file.

const string & GetPath (void) const
 the path of the cf file.

void SetData (const string &aData)
 Assigns a data pool of the format of a cfg file for fast retrieval.

void Flush (const string &aFilename="")
 Writes the cfg file if altered otherwise does nothing.

int GetCfgInt (const string &aSection, const string &aId, const int aDefaultValue=0)
int GetCfgHex (const string &aSection, const string &aId, const int aDefaultValue=0)
double GetCfgDbl (const string &aSection, const string &aId, const double aDefaultValue=0.0)
vector< double > & GetCfgDbl (const string &aSection, const string &aId, vector< double > &ans)
string GetCfgString (const string &aSection, const string &aId, const string &aDefaultValue="")
string GetCfgFile (const string &aSection, const string &aId, const string &aDefaultValue="")
Ars::RGBColor GetCfgColorHex (const string &aSection, const string &aId, const Ars::RGBColor c)
void SetCfgInt (const string &aSection, const string &aId, const int aValue)
void SetCfgHex (const string &aSection, const string &aId, const int aValue)
void SetCfgDbl (const string &aSection, const string &aId, const double aValue)
void SetCfgString (const string &aSection, const string &aId, const string &aValue)
void SetCfgFile (const string &aSection, const string &aId, const string &aValue)
void SetCfgColorHex (const string &aSection, const string &aId, const Ars::RGBColor c)
const bool IsTheFile (const char *aFile) const
const bool operator== (const Handler &h) const

Protected Member Functions

string GetCdr (const icstring &aSection, const string &aId)
map< icstring, vector< string
> >::iterator 
AddLine (map< icstring, vector< string > >::iterator pos, const string &aline)

Private Attributes

string CfgFile
string cfgPath
 file name and path name of the cfg file.

map< icstring, vector< string > > vList
 for each section maps the key value

bool dirty
 set to true when an element has been altered

unsigned long cnt


Constructor & Destructor Documentation

Cfg::Handler::Handler const string &  aFile = ""  ) 
 

Definition at line 59 of file CfgHandler.cpp.

References AddLine(), CfgFile, cfgPath, icstring, and vList.

00060 : CfgFile( aFile ) , dirty( false ) , cnt( 0 )
00061 {
00062         string::size_type       n = CfgFile.rfind( '\\' );
00063         if( n == string::npos )
00064                 n = CfgFile.rfind( '/' );
00065         if( n == string::npos )
00066                 cfgPath = "./";
00067         else
00068                 cfgPath = CfgFile.substr( 0 , n + 1 );
00069 
00070         ifstream        ifd( CfgFile.c_str() );
00071         
00072         vector<string>  e;
00073         vList.insert( make_pair( icstring( "" ) , e ) );
00074         map<icstring, vector<string> >::iterator pos = vList.find( "" );
00075 
00076         while( !ifd.fail() )
00077         {
00078                 char    buffer[4096] = "";
00079                 ifd.getline( buffer , sizeof buffer );
00080                 pos = AddLine( pos , buffer );
00081                 if( ifd.eof() )
00082                         break;
00083                 
00084         }
00085 }

Cfg::Handler::~Handler void   )  [virtual]
 

Definition at line 87 of file CfgHandler.cpp.

References vList.

00088 {
00089         for( map<icstring , vector<string> >::iterator it = vList.begin() ; it != vList.end() ; ++it )
00090                 it->second.clear();
00091         vList.clear();
00092 }


Member Function Documentation

map< icstring, vector< string > >::iterator Cfg::Handler::AddLine map< icstring, vector< string > >::iterator  pos,
const string &  aline
[protected]
 

Definition at line 94 of file CfgHandler.cpp.

References cnt, icstring, Ars::strip(), and vList.

Referenced by Handler(), SetCfgColorHex(), SetCfgDbl(), SetCfgFile(), SetCfgHex(), SetCfgInt(), SetCfgString(), and SetData().

00095 {
00096         //      If this is a section... [...]
00097         //      Creates a new section or returns the section iterator.
00098         if( aline[0] == '[' )
00099         {
00100                 string::size_type       at = aline.find( ']' );
00101                 if( at != string::npos )
00102                 {
00103                         icstring ics = aline.substr( 1 , at - 1 ).c_str();
00104                         map<icstring, vector<string> >::iterator pos2 = vList.find( ics );
00105                         if( pos2 == vList.end() )
00106                         {
00107                                 vector<string>  e;
00108                                 icstring        ics = aline.substr( 1 , at - 1 ).c_str();
00109                                 return vList.insert( make_pair( ics , e ) ).first;
00110                         }
00111                         return pos2;
00112                 }
00113         }
00114 
00115         //      We have to add a pair car = crd - otherwise this is considered as a comment...
00116         if( aline.find( "=" ) != string::npos && aline[0] != '#' && aline.substr( 0 , 2 ) != "//" )
00117         {
00118                 icstring        tmp = aline.substr( 0 , aline.find( '=' ) ).c_str();
00119                 icstring        s = Ars::strip( tmp );
00120                 for( vector<string>::iterator at = pos->second.begin() ; at != pos->second.end() ; ++at )
00121                 {
00122                         tmp = at->substr( 0 , at->find( '=' ) ).c_str();
00123                         icstring        ics = Ars::strip( tmp );
00124                         if( s == ics )
00125                         {
00126                                 *at = aline;
00127                                 return pos;
00128                         }
00129                 }
00130                 cnt += 1;
00131         }
00132 
00133         pos->second.push_back( aline );
00134         return pos;
00135 }

void Cfg::Handler::Flush const string &  aFilename = ""  ) 
 

Writes the cfg file if altered otherwise does nothing.

Definition at line 156 of file CfgHandler.cpp.

References CfgFile, dirty, and vList.

Referenced by Ars::ArsApplication::GenDefaultCfg(), Borqueror::GenDefaultCfg(), and Parameters::MainLoop().

00157 {
00158         if( !aFilename.empty()  || dirty )
00159         {
00160                 ofstream        of( (aFilename.empty() ? CfgFile.c_str() : aFilename.c_str()) , ios::out );
00161                 for( map<icstring,vector<string> >::iterator it = vList.begin() ; it != vList.end() ; ++it )
00162                 {
00163                         if( !it->first.empty() )
00164                                 of << "[" << it->first.c_str() << "]" << endl;
00165                         copy( it->second.begin() , it->second.end() , ostream_iterator<string>( of , "\n" ) );
00166                 }
00167         }
00168         dirty = false;
00169 }

string Cfg::Handler::GetCdr const icstring aSection,
const string &  aId
[protected]
 

Definition at line 171 of file CfgHandler.cpp.

References icstring, Ars::strip(), and vList.

Referenced by GetCfgDbl(), GetCfgFile(), GetCfgHex(), GetCfgInt(), and GetCfgString().

00172 {
00173         map<icstring, vector<string> >::iterator pos = vList.find( aSection );
00174         if( pos != vList.end() )
00175         {
00176                 for( vector<string>::iterator at = pos->second.begin() ; at != pos->second.end() ; ++at )
00177                 {
00178                         string::size_type n;
00179                         if( (n = at->find( '=' )) != string::npos )
00180                         {
00181                                 icstring        tmp = at->substr( 0 , n ).c_str();
00182                                 icstring        ics = Ars::strip( tmp );
00183                                 if( ics == icstring( aId.c_str() ) )
00184                                         return at->substr( at->find_first_not_of( ' ' , n + 1 ) );
00185 
00186                         }
00187                 }
00188         }
00189         return string( "" );
00190 }

Ars::RGBColor Cfg::Handler::GetCfgColorHex const string &  aSection,
const string &  aId,
const Ars::RGBColor  c
 

Definition at line 216 of file CfgHandler.cpp.

References Ars::RGBColor::alpha, Ars::RGBColor::blue, GetCfgInt(), Ars::RGBColor::green, and Ars::RGBColor::red.

Referenced by Ars::ArsApplication::Init(), and Ars::Thing::Thing().

00217 {
00218         unsigned long int       c = GetCfgInt( aSection , aId , (col.red << 16) + (col.green << 8) + col.blue + (col.alpha << 24) );
00219         return Ars::RGBColor( (unsigned char)( (c >> 16) & 0xff ) , (unsigned char)( (c >> 8) & 0xff ) , (unsigned char)( c & 0xff ) , (unsigned char)( c & 0xff000000 ? (c >> 24) & 0xff : 0xff ) );
00220 }

vector< double > & Cfg::Handler::GetCfgDbl const string &  aSection,
const string &  aId,
vector< double > &  ans
 

Definition at line 233 of file CfgHandler.cpp.

References GetCdr(), and icstring.

00234 {
00235         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00236 
00237         while( !s.empty() )
00238         {
00239                 double val = 0.0;
00240                 sscanf( s.c_str() , "%lf" , &val );
00241                 ans.push_back( val );
00242                 size_t  n = s.find_first_of( " \t\n" );
00243                 if( n != string::npos )
00244                         if( (n = s.find_first_not_of( " \t\n" , n )) != string::npos )
00245                                 s = s.substr( n );
00246                         else
00247                                 break;
00248                 else
00249                         break;
00250         }
00251         return ans;
00252 }

double Cfg::Handler::GetCfgDbl const string &  aSection,
const string &  aId,
const double  aDefaultValue = 0.0
 

Definition at line 222 of file CfgHandler.cpp.

References GetCdr(), and icstring.

00223 {
00224         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00225 
00226         double  val = aDefaultValue;
00227         if( !s.empty() )
00228                 sscanf( s.c_str() , "%lf" , &val );
00229 
00230         return val;
00231 }

string Cfg::Handler::GetCfgFile const string &  aSection,
const string &  aId,
const string &  aDefaultValue = ""
 

Definition at line 261 of file CfgHandler.cpp.

References cfgPath, GetCdr(), and icstring.

Referenced by Cfg::GetCfg(), Ars::ArsApplication::Init(), Ars::Painter::Init(), Ars::Sounds::Load(), Ars::Graf::LoadImg(), MainGame::PlayIntro(), Cfg::Push(), Ars::XScrollBar::SetupWindow(), and Ars::XMPEG::SetupWindow().

00262 {
00263         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00264 
00265         return (s.empty() ? aDefaultValue : (cfgPath + s));
00266 }

int Cfg::Handler::GetCfgHex const string &  aSection,
const string &  aId,
const int  aDefaultValue = 0
 

Definition at line 205 of file CfgHandler.cpp.

References GetCdr(), and icstring.

00206 {
00207         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00208 
00209         int     val = aDefaultValue;
00210         if( !s.empty() )
00211                 sscanf( s.c_str() , "%x" , &val );
00212 
00213         return val;
00214 }

int Cfg::Handler::GetCfgInt const string &  aSection,
const string &  aId,
const int  aDefaultValue = 0
 

Definition at line 192 of file CfgHandler.cpp.

References GetCdr(), and icstring.

Referenced by Parameters::CreateGalaxy(), GetCfgColorHex(), Ars::ExprIntParser::Init(), Ars::ExprParser::Context::Init(), Borqueror::Init(), Parameters::InitDialog(), Galaxy::Load(), InitGalaxyScreen::Run(), GalaxyScreen::SetupWindow(), and Ars::View::View().

00193 {
00194         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00195 
00196         int     val = aDefaultValue;
00197         if( s.substr( 0 , 2 ) == "0x" || s.substr( 0 , 2 ) == "0X" )
00198                 sscanf( s.substr( 2 ).c_str() , "%x" , &val );
00199         else if( !s.empty() )
00200                 sscanf( s.c_str() , "%d" , &val );
00201 
00202         return val;
00203 }

string Cfg::Handler::GetCfgString const string &  aSection,
const string &  aId,
const string &  aDefaultValue = ""
 

Definition at line 254 of file CfgHandler.cpp.

References GetCdr(), and icstring.

Referenced by Parameters::CreateGalaxy(), MainGame::EnableWindow(), Galaxy::evMessage(), Ars::ArsApplication::Init(), Borqueror::Init(), Ars::Painter::Init(), Parameters::MainLoop(), and HallOfFame::SetupWindow().

00255 {
00256         string  s = GetCdr( icstring( aSection.c_str() ) , aId );
00257 
00258         return (s.empty() ? aDefaultValue : s);
00259 }

const string& Cfg::Handler::GetFilename void   )  const [inline]
 

the filename of the cf file.

Definition at line 33 of file CfgHandler.h.

References CfgFile.

Referenced by MainGame::EnableWindow(), and Cfg::Pop().

00033 {       return CfgFile; }

const string& Cfg::Handler::GetPath void   )  const [inline]
 

the path of the cf file.

Definition at line 36 of file CfgHandler.h.

References cfgPath.

Referenced by Ars::Preload().

00036 {       return cfgPath; }

unsigned long Cfg::Handler::GetSize void   )  const [inline]
 

the amoun of element in the cfg file.

Definition at line 30 of file CfgHandler.h.

References cnt.

00030 {       return cnt;     }

const bool Cfg::Handler::IsTheFile const char *  aFile  )  const [inline]
 

Definition at line 59 of file CfgHandler.h.

References CfgFile.

Referenced by operator==().

00059 {       return CfgFile == aFile;        }

const bool Cfg::Handler::operator== const Handler h  )  const [inline]
 

Definition at line 60 of file CfgHandler.h.

References CfgFile, and IsTheFile().

00060 {       return h.IsTheFile( CfgFile.c_str() );  }

void Cfg::Handler::SetCfgColorHex const string &  aSection,
const string &  aId,
const Ars::RGBColor  c
 

Definition at line 308 of file CfgHandler.cpp.

References AddLine(), Ars::RGBColor::alpha, Ars::RGBColor::blue, dirty, Ars::RGBColor::green, Ars::RGBColor::red, and vList.

Referenced by Ars::ArsApplication::GenDefaultCfg(), and Borqueror::GenDefaultCfg().

00309 {
00310         dirty = true;
00311         char    buffer[4096];
00312         sprintf( buffer , " = 0x%02x%02x%02x%02x" , c.alpha , c.red , c.green , c.blue );
00313         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , aId + buffer ); 
00314 }

void Cfg::Handler::SetCfgDbl const string &  aSection,
const string &  aId,
const double  aValue
 

Definition at line 284 of file CfgHandler.cpp.

References AddLine(), dirty, and vList.

00285 {
00286         dirty = true;
00287         char    buffer[4096];
00288         sprintf( buffer , " = %f" , aValue );
00289         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , aId + buffer ); 
00290 }

void Cfg::Handler::SetCfgFile const string &  aSection,
const string &  aId,
const string &  aValue
 

Definition at line 298 of file CfgHandler.cpp.

References AddLine(), cfgPath, dirty, and vList.

Referenced by Ars::ArsApplication::GenDefaultCfg(), and Borqueror::GenDefaultCfg().

00299 {
00300         string  v = aValue;
00301         string::size_type       n = v.find( cfgPath );
00302         if( n != string::npos )
00303                 v = v.substr( n + cfgPath.length() );
00304         dirty = true;
00305         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , string( aId + " = " ) + v ); 
00306 }

void Cfg::Handler::SetCfgHex const string &  aSection,
const string &  aId,
const int  aValue
 

Definition at line 276 of file CfgHandler.cpp.

References AddLine(), dirty, and vList.

00277 {
00278         dirty = true;
00279         char    buffer[4096];
00280         sprintf( buffer , " = 0x%08x" , aValue );
00281         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , aId + buffer ); 
00282 }

void Cfg::Handler::SetCfgInt const string &  aSection,
const string &  aId,
const int  aValue
 

Definition at line 268 of file CfgHandler.cpp.

References AddLine(), dirty, and vList.

Referenced by Borqueror::GenDefaultCfg(), and Parameters::MainLoop().

00269 {
00270         dirty = true;
00271         char    buffer[4096];
00272         sprintf( buffer , " = %d" , aValue );
00273         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , aId + buffer ); 
00274 }

void Cfg::Handler::SetCfgString const string &  aSection,
const string &  aId,
const string &  aValue
 

Definition at line 292 of file CfgHandler.cpp.

References AddLine(), dirty, and vList.

Referenced by Borqueror::GenDefaultCfg(), and Parameters::MainLoop().

00293 {
00294         dirty = true;
00295         AddLine( AddLine( vList.begin() , string( "[" ) + aSection + "]" ) , string( aId + " = " ) + aValue ); 
00296 }

void Cfg::Handler::SetData const string &  aData  ) 
 

Assigns a data pool of the format of a cfg file for fast retrieval.

Definition at line 137 of file CfgHandler.cpp.

References AddLine(), cnt, dirty, icstring, and vList.

00138 {
00139         dirty = false;
00140         vList.clear();
00141         string::size_type       n0 = 0 , n1 = aData.find( "\n" , n0 );
00142         cnt = 0;
00143 
00144         vector<string>  e;
00145         vList.insert( make_pair( icstring( "" ) , e ) );
00146         map<icstring, vector<string> >::iterator pos = vList.find( "" );
00147 
00148         while( n1 != string::npos )
00149         {
00150                 pos = AddLine( pos , string( aData.substr( n0 , n1 - n0 ) ) );
00151                 n0 = n1 + 1;
00152                 n1 = aData.find( "\n" , n0 );
00153         }
00154 }


Field Documentation

string Cfg::Handler::CfgFile [private]
 

Definition at line 11 of file CfgHandler.h.

Referenced by Flush(), GetFilename(), Handler(), IsTheFile(), and operator==().

string Cfg::Handler::cfgPath [private]
 

file name and path name of the cfg file.

Definition at line 11 of file CfgHandler.h.

Referenced by GetCfgFile(), GetPath(), Handler(), and SetCfgFile().

unsigned long Cfg::Handler::cnt [private]
 

Definition at line 16 of file CfgHandler.h.

Referenced by AddLine(), GetSize(), and SetData().

bool Cfg::Handler::dirty [private]
 

set to true when an element has been altered

Definition at line 15 of file CfgHandler.h.

Referenced by Flush(), SetCfgColorHex(), SetCfgDbl(), SetCfgFile(), SetCfgHex(), SetCfgInt(), SetCfgString(), and SetData().

map<icstring , vector<string> > Cfg::Handler::vList [private]
 

for each section maps the key value

Definition at line 13 of file CfgHandler.h.

Referenced by AddLine(), Flush(), GetCdr(), Handler(), SetCfgColorHex(), SetCfgDbl(), SetCfgFile(), SetCfgHex(), SetCfgInt(), SetCfgString(), SetData(), and ~Handler().


The documentation for this class was generated from the following files:
Generated on Fri Dec 5 04:07:05 2003 for Borqueror by doxygen 1.3.3