# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

NLMISC::CConfigFile Class Reference

CConfigFile class. More...

#include <config_file.h>

List of all members.

Public Methods

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

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

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

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

void save () const
 save the config file. More...

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

void reparse (const char *filename=NULL, bool callingCallback=true)
 reload and reparse the file. More...

void print () const
 display all variables with nlinfo (debug use). More...

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

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

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. More...

void setLastModifiedNow ()
std::string getFilename () const
 returns the configfile name. More...


Static Public Methods

void CConfigFile::setTimeout (uint32 timeout)
 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".
More...


void checkConfigFiles ()
 Internal use only. More...


Public Attributes

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


Private Methods

uint32 getLastModified ()
 Internal use only. More...


Private Attributes

void(* _Callback )()
 Internal use only. More...

std::vector< CVar_Vars
 Internal use only. More...

std::string _FileName
 Internal use only. More...

uint32 _LastModified
 Internal use only. More...


Static Private Attributes

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


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 \cvar13=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.


Constructor & Destructor Documentation

NLMISC::CConfigFile::CConfigFile   [inline]
 

Definition at line 198 of file config_file.h.

References _Callback.

NLMISC::CConfigFile::~CConfigFile   [virtual]
 

Definition at line 209 of file config_file.cpp.

References _ConfigFiles.


Member Function Documentation

void NLMISC::CConfigFile::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".

void NLMISC::CConfigFile::checkConfigFiles   [static]
 

Internal use only.

Definition at line 631 of file config_file.cpp.

References _ConfigFiles, _Timeout, and nlwarning.

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

Return true if the variable exists, false otherwise.

Definition at line 410 of file config_file.cpp.

References _Vars.

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

returns the configfile name.

Definition at line 241 of file config_file.h.

References _FileName.

uint32 NLMISC::CConfigFile::getLastModified   [private]
 

Internal use only.

Definition at line 606 of file config_file.cpp.

References _FileName.

Referenced by reparse, and setLastModifiedNow.

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

Get a variable with the variable name.

Definition at line 364 of file config_file.cpp.

References _FileName, _Vars, and UnknownVariables.

Referenced by NLNET::IService::main, and NLNET::CUdpSimSock::setSimValues.

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 388 of file config_file.cpp.

References _Vars, and UnknownVariables.

Referenced by load, and NLNET::IService::main.

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

load and parse the file.

Definition at line 226 of file config_file.cpp.

References _ConfigFiles, _FileName, getVarPtr, nlinfo, nlwarning, and reparse.

Referenced by NLNET::IService::main.

bool NLMISC::CConfigFile::loaded  
 

Returns true if the file has been loaded.

Definition at line 264 of file config_file.cpp.

void NLMISC::CConfigFile::print CLog   log const
 

display all variables with nlinfo (debug use).

Definition at line 492 of file config_file.cpp.

References _FileName, _Vars, and NLMISC::createDebug.

void NLMISC::CConfigFile::print   const
 

display all variables with nlinfo (debug use).

Definition at line 487 of file config_file.cpp.

References NLMISC::InfoLog.

void NLMISC::CConfigFile::reparse const char *    filename = NULL,
bool    callingCallback = true
 

reload and reparse the file.

Definition at line 270 of file config_file.cpp.

References _Callback, _FileName, _LastModified, _Vars, cf_CurrentLine, cf_ifile, cf_OverwriteExistingVariable, cfparse, cfrestart, getLastModified, nlassert, and nlwarning.

Referenced by load.

void NLMISC::CConfigFile::save   const
 

save the config file.

Definition at line 423 of file config_file.cpp.

References _FileName, _Vars, and nlwarning.

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 575 of file config_file.cpp.

References _Vars.

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

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

Definition at line 570 of file config_file.cpp.

References _Callback.

Referenced by NLNET::IService::main, and NLNET::CUdpSimSock::setSimValues.

void NLMISC::CConfigFile::setLastModifiedNow  
 

Definition at line 593 of file config_file.cpp.

References _LastModified, and getLastModified.


Member Data 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 602 of file config_file.cpp.

Referenced by checkConfigFiles, load, and ~CConfigFile.

std::string NLMISC::CConfigFile::_FileName [private]
 

Internal use only.

Definition at line 259 of file config_file.h.

Referenced by getFilename, getLastModified, getVar, load, print, reparse, and save.

uint32 NLMISC::CConfigFile::_LastModified [private]
 

Internal use only.

Definition at line 264 of file config_file.h.

Referenced by reparse, and setLastModifiedNow.

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

Definition at line 604 of file config_file.cpp.

Referenced by checkConfigFiles.

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

Internal use only.

Definition at line 256 of file config_file.h.

Referenced by exists, getVar, getVarPtr, print, reparse, save, and setCallback.

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 238 of file config_file.h.

Referenced by getVar, and getVarPtr.


The documentation for this class was generated from the following files: