# 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  

config_file.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_CONFIG_FILE_H
00027 #define NL_CONFIG_FILE_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/common.h"
00031 #include "nel/misc/debug.h"
00032 #include "nel/misc/log.h"
00033 
00034 #include <vector>
00035 #include <string>
00036 #include <stdio.h>
00037 
00038 namespace NLMISC
00039 {
00040 
00119 class CConfigFile
00120 {
00121 public:
00122 
00132         struct CVar
00133         {
00134         public:
00135 
00136                 CVar () : Type(T_UNKNOWN) {}
00137 
00139 
00140 
00141                 int                                      asInt          (int index=0) const;
00143                 double                           asDouble       (int index=0) const;
00145                 float                            asFloat        (int index=0) const;
00147                 const std::string       &asString       (int index=0) const;
00149 
00152 
00153 
00154                 void                            setAsInt        (int val, int index=0);
00156                 void                            setAsDouble     (double val, int index=0);
00158                 void                            setAsFloat      (float val, int index=0);
00160                 void                            setAsString     (std::string val, int index=0);
00161 
00163                 void                            setAsInt        (std::vector<int> vals);
00165                 void                            setAsDouble     (std::vector<double> vals);
00167                 void                            setAsFloat      (std::vector<float> vals);
00169                 void                            setAsString     (std::vector<std::string> vals);
00170                 
00172 
00173                 bool            operator==	(const CVar& var) const;
00174                 bool            operator!=	(const CVar& var) const;
00175 
00176                 // add this variable with var
00177                 void            add (const CVar &var);
00178 
00179                 // Get the size of the variable. It's the number of element of the array or 1 if it's not an array.
00180                 int                     size () const;
00181 
00183 
00184                 static char *TypeName[];
00185 
00186                 enum TVarType { T_UNKNOWN, T_INT, T_STRING, T_REAL };
00187 
00188                 std::string                                     Name;
00189                 TVarType                                        Type;
00190                 bool                                            Comp;
00191                 std::vector<int>                        IntValues;
00192                 std::vector<double>                     RealValues;
00193                 std::vector<std::string>        StrValues;
00194                 void                                            (*Callback)(CVar &var);
00196         };
00197 
00198         CConfigFile() : _Callback(NULL) {}
00199         
00200         virtual ~CConfigFile ();
00201 
00203         CVar &getVar (const std::string &varName);
00204 
00206         CVar *getVarPtr (const std::string &varName);
00207 
00209         bool exists (const std::string &varName);
00210 
00212         void load (const std::string &fileName);
00213 
00215         void save () const;
00216 
00218         bool loaded();
00219 
00221         void reparse (const char *filename = NULL, bool callingCallback = true);
00222 
00224         void print () const;
00225 
00227         void print (CLog *log) const;
00228 
00230         void setCallback (void (*cb)());
00231 
00233         void setCallback (const std::string &VarName, void (*cb)(CConfigFile::CVar &var));
00234 
00235         void setLastModifiedNow ();
00236 
00238         std::vector<std::string> UnknownVariables;
00239 
00241         std::string getFilename () const { return _FileName; }
00242 
00245         static void CConfigFile::setTimeout (uint32 timeout);
00246 
00248         static void checkConfigFiles ();
00249 
00250 private:
00251 
00253         void (*_Callback)();
00254 
00256         std::vector<CVar>       _Vars;
00257 
00259         std::string _FileName;
00260 
00262         uint32  getLastModified ();
00264         uint32  _LastModified;
00265 
00266         static uint32   _Timeout;
00267 
00268         static std::vector<CConfigFile *> *_ConfigFiles;
00269 };
00270 
00271 struct EConfigFile : public Exception
00272 {
00273         EConfigFile() { _Reason = "Unknown Config File Exception";}
00274 };
00275 
00276 struct EBadType : public EConfigFile
00277 {
00278         EBadType (const std::string &varName, int varType, int wantedType)
00279         {
00280                 static char str[NLMISC::MaxCStringSize];
00281                 smprintf (str, NLMISC::MaxCStringSize, "Bad variable type, variable \"%s\" is a %s and not a %s", varName.c_str (), CConfigFile::CVar::TypeName[varType], CConfigFile::CVar::TypeName[wantedType]);
00282                 _Reason = str;
00283                 nlinfo("Exception will be launched: %s", _Reason.c_str());
00284         }
00285 };
00286 
00287 struct EBadSize : public EConfigFile
00288 {
00289         EBadSize (const std::string &varName, int varSize, int varIndex)
00290         {
00291                 static char str[NLMISC::MaxCStringSize];
00292                 smprintf (str, NLMISC::MaxCStringSize, "Trying to access to the index %d but the variable \"%s\" size is %d", varIndex, varName.c_str (), varSize);
00293                 _Reason = str;
00294                 nlinfo("Exception will be launched: %s", _Reason.c_str());
00295         }
00296 };
00297 
00298 struct EUnknownVar : public EConfigFile
00299 {
00300         EUnknownVar (const std::string &filename, const std::string &varName)
00301         {
00302                 static char str[NLMISC::MaxCStringSize];
00303                 smprintf (str, NLMISC::MaxCStringSize, "variable \"%s\" not found in file \"%s\"", varName.c_str (), filename.c_str());
00304                 _Reason = str;
00305                 nlinfo("Exception will be launched: %s", _Reason.c_str());
00306         }
00307 };
00308 
00309 struct EParseError : public EConfigFile
00310 {
00311         EParseError (const std::string &fileName, int currentLine)
00312         {
00313                 static char str[NLMISC::MaxCStringSize];
00314                 smprintf (str, NLMISC::MaxCStringSize, "Parse error on the \"%s\" file, line %d", fileName.c_str (), currentLine);
00315                 _Reason = str;
00316                 nlinfo("Exception will be launched: %s", _Reason.c_str());
00317         }
00318 };
00319 
00320 struct EFileNotFound : public EConfigFile
00321 {
00322         EFileNotFound (const std::string &fileName)
00323         {
00324                 static char str[NLMISC::MaxCStringSize];
00325                 smprintf (str, NLMISC::MaxCStringSize, "File \"%s\" not found", fileName.c_str ());
00326                 _Reason = str;
00327         }
00328 };
00329 
00330 } // NLMISC
00331 
00332 #endif // NL_CONFIG_FILE_H
00333 
00334 /* End of config_file.h */