NLMISC::CConfigFile Class Reference

#include <config_file.h>


Detailed Description

CConfigFile class. Useful when you want to have a configuration file with variables. It manages integers, real (double), and string basic types. A variable can be an array of basic type. In this case, all elements of the array must have the same type.

If you setup the global callback before loading, it'll be call after the load() function.

Example:

try { CConfigFile cf; // Load and parse "test.txt" file cf.load ("test.txt"); // Attach a callback to the var1 variable. When the var1 will changed, this cvar1cb function will be called cf.setCallback ("var1", var1cb); // Get the foo variable (suppose it's a string variable) CConfigFile::CVar &foo = cf.getVar ("foo"); // Display the content of the variable printf ("foo = %s\n", foo.asString ().c_str ()); // Get the bar variable (suppose it's an array of int) CConfigFile::CVar &bar = cf.getVar ("bar"); // Display the content of the all elements of the bar variable printf ("bar have %d elements : \n", bar.size ()); for (int i = 0; i < bar.size (); i++) printf ("%d ", bar.asInt (i)); printf("\n"); } catch (EConfigFile &e) { // Something goes wrong... catch that printf ("%s\n", e.what ()); } *

Example of config file:

// one line comment / * big comment on more than one line * / var1 = 123; // var1 type:int, value:123 var2 = "456.25"; // var2 type:string, value:"456.25" var3 = 123.123; // var3 type:real, value:123.123 // the resulting type is type of the first left value var4 = 123.123 + 2; // var4 type:real, value:125.123 var5 = 123 + 2.1; // var5 type:int, value:125 var6 = (-112+1) * 3 - 14; // var6 type:int, value:-347 var7 = var1 + 1; // var7 type:int, value:124 var8 = var2 + 10; // var8 type:string, value:456.2510 (convert 10 into a string and concat it) var9 = 10.15 + var2; // var9 type:real, value:466.4 (convert var2 into a real and add it) var10 = { 10.0, 51.1 }; // var10 type:realarray, value:{10.0,51.1} var11 = { "str1", "str2", "str3" }; // var11 type:stringarray, value:{"str1", "str2", "str3"} var12 = { 10+var1, var1-var7 }; // var12 type:intarray, value:{133,-1} *

Operators are '+', '-', '*', '/'. You can't use operators on a array variable, for example, you can't do =var12+1. If you have 2 variables with the same name, the first value will be remplaced by the second one.

Bug:
if you terminate the config file with a comment without carriage returns it'll generate an exception, add a carriage returns
Author:
Vianney Lecroart

Nevrax France

Date:
2000

Definition at line 119 of file config_file.h.

Public Member Functions

 CConfigFile ()
void clear ()
 Clear all the variable array (including information on variable callback etc).

void clearVars ()
 set to 0 or "" all variable in the array (but not destroy them)

void display (CLog *log) const
 display all variables with nlinfo (debug use)

void display () const
 display all variables with nlinfo (debug use)

bool exists (const std::string &varName)
 Return true if the variable exists, false otherwise.

std::string getFilename () const
 returns the config file name

uint getNumVar () const
 Get the variable count.

CVargetVar (uint varId)
 Get a variable.

CVargetVar (const std::string &varName)
 Get a variable with the variable name.

CVargetVarPtr (const std::string &varName)
 Get a variable pointer with the variable name, without throwing exception. Return NULL if not found.

CVarinsertVar (const std::string &varName, const CVar &varToCopy)
 Add a variable. If the variable already exit, return it.

void load (const std::string &fileName)
 load and parse the file

bool loaded ()
 Returns true if the file has been loaded.

void reparse ()
 reload and reparse the file

void save () const
 save the config file

void setCallback (const std::string &VarName, void(*cb)(CConfigFile::CVar &var))
 set a callback function to a variable, it will be called when this variable is modified

void setCallback (void(*cb)())
 set a callback function that is called when the config file is modified

virtual ~CConfigFile ()

Static Public Member Functions

void checkConfigFiles ()
 Internal use only.

void setTimeout (uint32 timeout)

Data Fields

std::vector< std::string > UnknownVariables
 contains the variable names that getVar() and getVarPtr() tried to access but not present in the cfg


Private Attributes

void(* _Callback )()
 Internal use only.

std::vector< CVar_Vars
 Internal use only.

std::vector< std::string > FileNames
std::vector< uint32LastModified

Static Private Attributes

std::vector< CConfigFile * > * _ConfigFiles = NULL
uint32 _Timeout = 1000


Constructor & Destructor Documentation

NLMISC::CConfigFile::CConfigFile  )  [inline]
 

Definition at line 202 of file config_file.h.

References _Callback.

00202 : _Callback(NULL) {}

NLMISC::CConfigFile::~CConfigFile  )  [virtual]
 

Definition at line 227 of file config_file.cpp.

References _ConfigFiles.

00228 {
00229         if (_ConfigFiles == NULL || (*_ConfigFiles).empty ()) return;
00230 
00231         vector<CConfigFile *>::iterator it = find ((*_ConfigFiles).begin (), (*_ConfigFiles).end (), this);
00232         if (it != (*_ConfigFiles).end ())
00233         {
00234                 (*_ConfigFiles).erase (it);
00235         }
00236 
00237         if ((*_ConfigFiles).empty())
00238         {
00239                 delete _ConfigFiles;
00240                 _ConfigFiles = NULL;
00241         }
00242 }


Member Function Documentation

void NLMISC::CConfigFile::checkConfigFiles  )  [static]
 

Internal use only.

Definition at line 681 of file config_file.cpp.

References _ConfigFiles, _Timeout, nlassert, nlwarning, and uint.

00682 {
00683         if (_ConfigFiles == NULL) return;
00684 
00685         static time_t LastCheckTime = time (NULL);
00686         if (_Timeout > 0 && (float)(time (NULL) - LastCheckTime)*1000.0f < (float)_Timeout) return;
00687 
00688         LastCheckTime = time (NULL);
00689 
00690         bool needReparse;
00691         for (vector<CConfigFile *>::iterator it = (*_ConfigFiles).begin (); it != (*_ConfigFiles).end (); it++)
00692         {
00693                 needReparse = false;
00694                 nlassert ((*it)->FileNames.size() == (*it)->LastModified.size());
00695                 for (uint i = 0; i < (*it)->FileNames.size(); i++)
00696                 {
00697                         if ((*it)->LastModified[i] != CFile::getFileModificationDate((*it)->FileNames[i]))
00698                         {
00699                                 needReparse = true;
00700                                 (*it)->LastModified[i] = CFile::getFileModificationDate((*it)->FileNames[i]);
00701                         }
00702                 }
00703                 if (needReparse)
00704                 {
00705                         try
00706                         {
00707                                 (*it)->reparse ();
00708                         }
00709                         catch (EConfigFile &e)
00710                         {
00711                                 nlwarning ("CF: Exception will re-read modified config file '%s': %s", (*it)->getFilename().c_str(), e.what ());
00712                         }
00713                 }
00714         }
00715 }

void NLMISC::CConfigFile::clear  ) 
 

Clear all the variable array (including information on variable callback etc).

Definition at line 722 of file config_file.cpp.

00723 {
00724         _Vars.clear ();
00725 }

void NLMISC::CConfigFile::clearVars  ) 
 

set to 0 or "" all variable in the array (but not destroy them)

Definition at line 727 of file config_file.cpp.

00728 {
00729         for (vector<CVar>::iterator it = _Vars.begin (); it != _Vars.end (); it++)
00730         {
00731                 (*it).Type = CVar::T_UNKNOWN;
00732         }
00733 }

void NLMISC::CConfigFile::display CLog log  )  const
 

display all variables with nlinfo (debug use)

Definition at line 559 of file config_file.cpp.

References NLMISC::createDebug(), NLMISC::CLog::displayRaw(), NLMISC::CLog::displayRawNL(), and getFilename().

00560 {
00561         createDebug ();
00562 
00563         log->displayRawNL ("Config file %s have %d variables and %d root config file:", getFilename().c_str(), _Vars.size(), FileNames.size()-1);
00564         log->displayRaw ("Root config files: ");
00565         for(int i = 1; i < (int)FileNames.size(); i++)
00566         {
00567                 log->displayRaw (FileNames[i].c_str());
00568         }
00569         log->displayRawNL ("");
00570         log->displayRawNL ("------------------------------------------------------");
00571         for(int i = 0; i < (int)_Vars.size(); i++)
00572         {
00573                 log->displayRaw ((_Vars[i].Callback==NULL)?"   ":"CB ");
00574                 log->displayRaw ((_Vars[i].Root)?"Root ":"     ");
00575                 if (_Vars[i].Comp)
00576                 {
00577                         switch (_Vars[i].Type)
00578                         {
00579                         case CConfigFile::CVar::T_INT:
00580                         {
00581                                 log->displayRaw ("%-20s { ", _Vars[i].Name.c_str());
00582                                 for (int it=0; it < (int)_Vars[i].IntValues.size(); it++)
00583                                 {
00584                                         log->displayRaw ("'%d' ", _Vars[i].IntValues[it]);
00585                                 }
00586                                 log->displayRawNL ("}");
00587                                 break;
00588                         }
00589                         case CConfigFile::CVar::T_STRING:
00590                         {
00591                                 log->displayRaw ("%-20s { ", _Vars[i].Name.c_str());
00592                                 for (int st=0; st < (int)_Vars[i].StrValues.size(); st++)
00593                                 {
00594                                         log->displayRaw ("\"%s\" ", _Vars[i].StrValues[st].c_str());
00595                                 }
00596                                 log->displayRawNL ("}");
00597                                 break;
00598                         }
00599                         case CConfigFile::CVar::T_REAL:
00600                         {
00601                                 log->displayRaw ("%-20s { " , _Vars[i].Name.c_str());
00602                                 for (int rt=0; rt < (int)_Vars[i].RealValues.size(); rt++)
00603                                 {
00604                                         log->displayRaw ("`%f` ", _Vars[i].RealValues[rt]);
00605                                 }
00606                                 log->displayRawNL ("}");
00607                                 break;
00608                         }
00609                         case CConfigFile::CVar::T_UNKNOWN:
00610                         {
00611                                  log->displayRawNL ("%-20s { }" , _Vars[i].Name.c_str());
00612                                 break;
00613                         }
00614                         default:
00615                         {
00616                                 log->displayRawNL ("%-20s <default case comp> (%d)" , _Vars[i].Name.c_str(), _Vars[i].Type);
00617                                 break;
00618                         }
00619                         }
00620                 }
00621                 else
00622                 {
00623                         switch (_Vars[i].Type)
00624                         {
00625                         case CConfigFile::CVar::T_INT:
00626                                 log->displayRawNL ("%-20s '%d'", _Vars[i].Name.c_str(), _Vars[i].IntValues[0]);
00627                                 break;
00628                         case CConfigFile::CVar::T_STRING:
00629                                 log->displayRawNL ("%-20s \"%s\"", _Vars[i].Name.c_str(), _Vars[i].StrValues[0].c_str());
00630                                 break;
00631                         case CConfigFile::CVar::T_REAL:
00632                                 log->displayRawNL ("%-20s `%f`", _Vars[i].Name.c_str(), _Vars[i].RealValues[0]);
00633                                 break;
00634                         case CConfigFile::CVar::T_UNKNOWN:
00635                                 log->displayRawNL ("%-20s <Unknown>", _Vars[i].Name.c_str());
00636                                 break;
00637                         default:
00638                         {
00639                                 log->displayRawNL ("%-20s <default case> (%d)" , _Vars[i].Name.c_str(), _Vars[i].Type);
00640                                 break;
00641                         }
00642                         }
00643                 }
00644         }
00645 }

void NLMISC::CConfigFile::display  )  const
 

display all variables with nlinfo (debug use)

Definition at line 554 of file config_file.cpp.

References NLMISC::InfoLog.

00555 {
00556         display (InfoLog);
00557 }

bool NLMISC::CConfigFile::exists const std::string &  varName  ) 
 

Return true if the variable exists, false otherwise.

Definition at line 461 of file config_file.cpp.

References uint.

Referenced by NLNET::IService::main().

00462 {
00463         for (uint i = 0; i < _Vars.size(); i++)
00464         {
00465                 // the type could be T_UNKNOWN if we add a callback on this name but this var is not in the config file
00466                 if (_Vars[i].Name == varName && (_Vars[i].Type != CVar::T_UNKNOWN || _Vars[i].Comp))
00467                 {
00468                         return true;
00469                 }
00470         }
00471         return false;
00472 }

std::string NLMISC::CConfigFile::getFilename  )  const [inline]
 

returns the config file name

Definition at line 258 of file config_file.h.

Referenced by display(), getVar(), NLMISC::IVariable::init(), save(), and setCallback().

00258 { return FileNames[0]; }

uint NLMISC::CConfigFile::getNumVar  )  const
 

Get the variable count.

Definition at line 735 of file config_file.cpp.

References uint.

00736 {
00737         return _Vars.size ();
00738 }

CConfigFile::CVar * NLMISC::CConfigFile::getVar uint  varId  ) 
 

Get a variable.

Definition at line 740 of file config_file.cpp.

References uint.

00741 {
00742         return &(_Vars[varId]);
00743 }

CConfigFile::CVar & NLMISC::CConfigFile::getVar const std::string &  varName  ) 
 

Get a variable with the variable name.

Definition at line 431 of file config_file.cpp.

References getFilename(), and getVarPtr().

Referenced by NLNET::IService::main(), NLLIGO::CLigoConfig::read(), and NLNET::CUdpSimSock::setSimValues().

00432 {
00433         CVar *var =  getVarPtr (varName);
00434         if (var == 0) 
00435                 throw EUnknownVar (getFilename(), varName);
00436         else
00437                 return *var;
00438 }

CConfigFile::CVar * NLMISC::CConfigFile::getVarPtr const std::string &  varName  ) 
 

Get a variable pointer with the variable name, without throwing exception. Return NULL if not found.

Definition at line 441 of file config_file.cpp.

References uint, and UnknownVariables.

Referenced by getVar(), NLMISC::CWordsDictionary::init(), NLMISC::IVariable::init(), insertVar(), NLNET::IService::main(), and reparse().

00442 {
00443         uint i;
00444         for (i = 0; i < _Vars.size(); i++)
00445         {
00446                 // the type could be T_UNKNOWN if we add a callback on this name but this var is not in the config file
00447                 if (_Vars[i].Name == varName && (_Vars[i].Type != CVar::T_UNKNOWN || _Vars[i].Comp))
00448                         return &(_Vars[i]);
00449         }
00450 
00451         // if not found, add it in the array if necessary
00452         for (i = 0; i < UnknownVariables.size(); i++)
00453                 if(UnknownVariables[i] == varName)
00454                         break;
00455         if (i == UnknownVariables.size())
00456                 UnknownVariables.push_back(varName);
00457 
00458         return NULL;
00459 }

CConfigFile::CVar * NLMISC::CConfigFile::insertVar const std::string &  varName,
const CVar varToCopy
 

Add a variable. If the variable already exit, return it.

Definition at line 745 of file config_file.cpp.

References getVarPtr(), NLMISC::CConfigFile::CVar::Name, and NLMISC::CConfigFile::CVar::Root.

00746 {
00747         // Get the var
00748         CVar *var = getVarPtr (varName);
00749         if (!var)
00750         {
00751                 _Vars.push_back (varToCopy);
00752                 var = &(_Vars.back ());
00753                 var->Root = false;
00754                 var->Name = varName;
00755         }
00756         return var;
00757 }

void NLMISC::CConfigFile::load const std::string &  fileName  ) 
 

load and parse the file

Definition at line 244 of file config_file.cpp.

References _ConfigFiles, nlwarning, and reparse().

Referenced by NLMISC::CWordsDictionary::init(), NLNET::IService::main(), and NLLIGO::CLigoConfig::read().

00245 {
00246         if(fileName.empty())
00247         {
00248                 nlwarning ("CF: Can't load a empty file name configfile");
00249                 return;
00250         }
00251 
00252         FileNames.clear ();
00253         FileNames.push_back (fileName);
00254 
00255         if (_ConfigFiles == NULL)
00256         {
00257                 _ConfigFiles = new std::vector<CConfigFile *>;
00258         }
00259         (*CConfigFile::_ConfigFiles).push_back (this);
00260         reparse ();
00261 
00262 /*      _FileName.clear ();
00263         _FileName.push_back (fileName);
00264 
00265         if (_ConfigFiles == NULL)
00266         {
00267                 _ConfigFiles = new std::vector<CConfigFile *>;
00268         }
00269         (*CConfigFile::_ConfigFiles).push_back (this);
00270         reparse ();
00271 
00272         // If we find a linked config file, load it but don't overload already existant variable
00273         CVar *var = getVarPtr ("RootConfigFilename");
00274         if (var)
00275         {
00276                 string RootConfigFilename = var->asString();
00277                 nlinfo ("RootConfigFilename variable found in the '%s' config file, parse it (%s)", fileName.c_str(), RootConfigFilename.c_str());
00278 
00279                 string path = CFile::getPath(fileName);
00280 
00281                 if (!path.empty())
00282                         path +=  "/";
00283 
00284                 path += RootConfigFilename;
00285 
00286                 reparse (path.c_str());
00287         }
00288 */
00289 //      print ();
00290 }

bool NLMISC::CConfigFile::loaded  ) 
 

Returns true if the file has been loaded.

Definition at line 292 of file config_file.cpp.

00293 {
00294         return !CConfigFile::FileNames.empty();
00295 }

void NLMISC::CConfigFile::reparse  ) 
 

reload and reparse the file

Definition at line 297 of file config_file.cpp.

References _Callback, NLMISC::CConfigFile::CVar::asString(), cf_CurrentLine, cf_ifile, cf_OverwriteExistingVariable, cfparse(), cfrestart(), NLMISC::CIFile::close(), NLMISC::CFile::fileExists(), NLMISC::CPath::getFullPath(), getVarPtr(), LoadRoot, nldebug, nlinfo, nlwarning, NLMISC::CIFile::open(), and NLMISC::CPath::standardizePath().

Referenced by load().

00298 {
00299         if (FileNames.empty())
00300         {
00301                 nlwarning ("CF: Can't reparse config file because file name is empty");
00302                 return;
00303         }
00304 
00305         string fn = FileNames[0];
00306 
00307         FileNames.clear ();
00308         LastModified.clear ();
00309 
00310 //      clearVars ();
00311 
00312         while (!fn.empty())
00313         {
00314                 fn = NLMISC::CPath::getFullPath(fn, false);
00315                 nldebug ("CF: Adding config file '%s' in the config file", fn.c_str());
00316                 FileNames.push_back (fn);
00317                 LastModified.push_back (CFile::getFileModificationDate(fn));
00318 
00319                 if (cf_ifile.open (fn))
00320                 {
00321                         cfrestart (NULL);
00322                         cf_OverwriteExistingVariable = (FileNames.size()==1);
00323                         LoadRoot = (FileNames.size()>1);
00324                         bool parsingOK = (cfparse (&(_Vars)) == 0);
00325                         cf_ifile.close();
00326                         if (!parsingOK)
00327                         {
00328                                 nlwarning ("CF: Parsing error in file %s line %d", fn.c_str(), cf_CurrentLine);
00329                                 throw EParseError (fn, cf_CurrentLine);
00330                         }
00331                 }
00332                 else
00333                 {
00334                         nlwarning ("CF: Config file '%s' not found in the path '%s'", fn.c_str(), CPath::getCurrentPath().c_str());
00335                         throw EFileNotFound (fn);
00336                 }
00337                 cf_ifile.close ();
00338 
00339                 // If we find a linked config file, load it but don't overload already existant variable
00340                 CVar *var = getVarPtr ("RootConfigFilename");
00341                 if (var)
00342                 {
00343                         string RootConfigFilename = var->asString();
00344 
00345                         if (!NLMISC::CFile::fileExists(RootConfigFilename))
00346                         {
00347                                 // file is not found, try with the path of the master cfg
00348                                 string path = NLMISC::CPath::standardizePath (NLMISC::CFile::getPath(FileNames[0]));
00349                                 RootConfigFilename = path + RootConfigFilename;
00350                         }
00351 
00352                         RootConfigFilename = NLMISC::CPath::getFullPath(RootConfigFilename, false);
00353 
00354                         if (RootConfigFilename != fn)
00355                         {
00356                                 nlinfo ("CF: RootConfigFilename variable found in the '%s' config file, parse the root config file '%s'", fn.c_str(), RootConfigFilename.c_str());
00357                                 fn = RootConfigFilename;
00358                         }
00359                         else
00360                                 fn.clear ();
00361                 }
00362                 else
00363                         fn.clear ();    
00364         }
00365 
00366         if (_Callback != NULL) 
00367                 _Callback();
00368 
00369 /*      if (filename == NULL)
00370         {
00371                 _LastModified = getLastModified ();
00372 
00373                 nlassert (!_FileName.empty());
00374 
00375                 if (cf_ifile.open (_FileName[0]))
00376                 {
00377                         // if we clear all the array, we'll lost the callback on variable and all information
00378                         //              _Vars.clear();
00379                         cfrestart (NULL);
00380                         cf_OverwriteExistingVariable = true;
00381                         LoadRoot = false;
00382                         bool parsingOK = (cfparse (&(_Vars)) == 0);
00383                         cf_ifile.close();
00384                         if (!parsingOK)
00385                         {
00386                                 nlwarning ("Parsing error in file %s line %d", _FileName.c_str(), cf_CurrentLine);
00387                                 throw EParseError (_FileName, cf_CurrentLine);
00388                         }
00389                 }
00390                 else
00391                 {
00392                         nlwarning ("ConfigFile '%s' not found in the path '%s'", _FileName.c_str(), CPath::getCurrentPath().c_str());
00393                         throw EFileNotFound (_FileName);
00394                 }
00395         }
00396         else
00397         {
00398                 nlassert (strlen(filename)>0);
00399 
00400                 // load external config filename, don't overwrite existant variable
00401                 if (cf_ifile.open (filename))
00402                 {
00403                         cfrestart (NULL);
00404                         cf_OverwriteExistingVariable = false;
00405                         LoadRoot = true;
00406                         bool parsingOK = (cfparse (&(_Vars)) == 0);
00407                         cf_ifile.close ();
00408                         if (!parsingOK)
00409                         {
00410                                 nlwarning ("Parsing error in file %s line %d", filename, cf_CurrentLine);
00411                                 throw EParseError (filename, cf_CurrentLine);
00412                         }
00413                 }
00414                 else
00415                 {
00416                         nlwarning ("RootConfigFilename '%s' not found", _FileName.c_str());
00417                 }
00418         }
00419 
00420         if (callingCallback)
00421         {
00422                 if (_Callback != NULL) 
00423                         _Callback();
00424         }
00425 */
00426 
00427 }

void NLMISC::CConfigFile::save  )  const
 

save the config file

Definition at line 474 of file config_file.cpp.

References getFilename(), and nlwarning.

00475 {
00476         FILE *fp = fopen (getFilename().c_str (), "w");
00477         if (fp == NULL)
00478         {
00479                 nlwarning ("CF: Couldn't create %s file", getFilename().c_str ());
00480                 return;
00481         }
00482 
00483         for(int i = 0; i < (int)_Vars.size(); i++)
00484         {
00485                 // Not a root value 
00486                 if (!_Vars[i].Root)
00487                 {
00488                         if (_Vars[i].Comp)
00489                         {
00490                                 fprintf(fp, "%-20s = {", _Vars[i].Name.c_str());
00491                                 switch (_Vars[i].Type)
00492                                 {
00493                                 case CConfigFile::CVar::T_INT:
00494                                 {
00495                                         for (int it=0; it < (int)_Vars[i].IntValues.size(); it++)
00496                                         {
00497                                                 if (it%_Vars[i].SaveWrap == 0)
00498                                                 {
00499                                                         fprintf(fp, "\n\t");
00500                                                 }
00501                                                 fprintf(fp, "%d%s", _Vars[i].IntValues[it], it<(int)_Vars[i].IntValues.size()-1?", ":" ");
00502                                         }
00503                                         break;
00504                                 }
00505                                 case CConfigFile::CVar::T_STRING:
00506                                 {
00507                                         for (int st=0; st < (int)_Vars[i].StrValues.size(); st++)
00508                                         {
00509                                                 if (st%_Vars[i].SaveWrap == 0)
00510                                                 {
00511                                                         fprintf(fp, "\n\t");
00512                                                 }
00513                                                 fprintf(fp, "\"%s\"%s", _Vars[i].StrValues[st].c_str(), st<(int)_Vars[i].StrValues.size()-1?", ":" ");
00514                                         }
00515                                         break;
00516                                 }
00517                                 case CConfigFile::CVar::T_REAL:
00518                                 {
00519                                         for (int rt=0; rt < (int)_Vars[i].RealValues.size(); rt++)
00520                                         {
00521                                                 if (rt%_Vars[i].SaveWrap == 0)
00522                                                 {
00523                                                         fprintf(fp, "\n\t");
00524                                                 }
00525                                                 fprintf(fp, "%.10f%s", _Vars[i].RealValues[rt], rt<(int)_Vars[i].RealValues.size()-1?", ":" ");
00526                                         }
00527                                         break;
00528                                 }
00529                                 default: break;
00530                                 }
00531                                 fprintf(fp, "\n};\n");
00532                         }
00533                         else
00534                         {
00535                                 switch (_Vars[i].Type)
00536                                 {
00537                                 case CConfigFile::CVar::T_INT:
00538                                         fprintf(fp, "%-20s = %d;\n", _Vars[i].Name.c_str(), _Vars[i].IntValues[0]);
00539                                         break;
00540                                 case CConfigFile::CVar::T_STRING:
00541                                         fprintf(fp, "%-20s = \"%s\";\n", _Vars[i].Name.c_str(), _Vars[i].StrValues[0].c_str());
00542                                         break;
00543                                 case CConfigFile::CVar::T_REAL:
00544                                         fprintf(fp, "%-20s = %.10f;\n", _Vars[i].Name.c_str(), _Vars[i].RealValues[0]);
00545                                         break;
00546                                 default: break;
00547                                 }
00548                         }
00549                 }
00550         }
00551         fclose (fp);
00552 }

void NLMISC::CConfigFile::setCallback const std::string &  VarName,
void(*  cb)(CConfigFile::CVar &var)
 

set a callback function to a variable, it will be called when this variable is modified

Definition at line 653 of file config_file.cpp.

References NLMISC::CConfigFile::CVar::Callback, NLMISC::CConfigFile::CVar::Comp, getFilename(), NLMISC::CConfigFile::CVar::Name, nlinfo, and NLMISC::CConfigFile::CVar::Type.

00654 {
00655         for (vector<CVar>::iterator it = _Vars.begin (); it != _Vars.end (); it++)
00656         {
00657                 if (VarName == (*it).Name)
00658                 {
00659                         (*it).Callback = cb;
00660                         nlinfo ("CF: Setting callback when the variable '%s' on the file '%s' is modified externaly", VarName.c_str(), getFilename().c_str());
00661                         return;
00662                 }
00663         }
00664         // VarName doesn't exist, add it now for the future
00665         CVar Var;
00666         Var.Name = VarName;
00667         Var.Callback = cb;
00668         Var.Type = CVar::T_UNKNOWN;
00669         Var.Comp = false;
00670         _Vars.push_back (Var);
00671         nlinfo ("CF: Setting callback when the variable '%s' on the file '%s' is modified externaly (actually unknown)", VarName.c_str(), getFilename().c_str());
00672 }

void NLMISC::CConfigFile::setCallback void(*  cb)()  ) 
 

set a callback function that is called when the config file is modified

Definition at line 647 of file config_file.cpp.

References _Callback, getFilename(), and nlinfo.

Referenced by NLMISC::IVariable::init(), NLNET::IService::main(), and NLNET::CUdpSimSock::setSimValues().

00648 {
00649         _Callback = cb;
00650         nlinfo ("CF: Setting callback when the file '%s' is modified externaly", FileNames.empty()?"":getFilename().c_str());
00651 }

void NLMISC::CConfigFile::setTimeout uint32  timeout  )  [static]
 

set the time between 2 file checking (default value is 1 second)

Parameters:
timeout time in millisecond, if timeout=0, the check will be made each "frame"

Definition at line 717 of file config_file.cpp.

References _Timeout, and uint32.

00718 {
00719         _Timeout = timeout;
00720 }


Field Documentation

void(* NLMISC::CConfigFile::_Callback)() [private]
 

Internal use only.

Referenced by CConfigFile(), reparse(), and setCallback().

vector< CConfigFile * > * NLMISC::CConfigFile::_ConfigFiles = NULL [static, private]
 

Definition at line 677 of file config_file.cpp.

Referenced by checkConfigFiles(), load(), and ~CConfigFile().

uint32 NLMISC::CConfigFile::_Timeout = 1000 [static, private]
 

Definition at line 679 of file config_file.cpp.

Referenced by checkConfigFiles(), and setTimeout().

std::vector<CVar> NLMISC::CConfigFile::_Vars [private]
 

Internal use only.

Definition at line 273 of file config_file.h.

std::vector<std::string> NLMISC::CConfigFile::FileNames [private]
 

Definition at line 280 of file config_file.h.

std::vector<uint32> NLMISC::CConfigFile::LastModified [private]
 

Definition at line 281 of file config_file.h.

std::vector<std::string> NLMISC::CConfigFile::UnknownVariables
 

contains the variable names that getVar() and getVarPtr() tried to access but not present in the cfg

Definition at line 255 of file config_file.h.

Referenced by getVarPtr().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:06:34 2004 for NeL by doxygen 1.3.6