00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00177 void add (const CVar &var);
00178
00179
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 }
00331
00332 #endif // NL_CONFIG_FILE_H
00333
00334