config.cpp

00001 #include "config.h"
00002 #include "debug.h"
00003 #include "fast_atof.h"
00004 
00005 using namespace irr;
00006 using namespace core;
00007 using namespace io;
00008 
00009 extern Debug* dbg;
00010 
00011 Config::Config(IrrlichtDevice* d)
00012 {
00013     //ctor
00014     dbg->log("create Config");
00015     device = d;
00016 }
00017 
00018 Config::~Config()
00019 {
00020     //dtor
00021     // erase all parameters
00022     s32 count = getParameterCount();
00023 
00024     for (s32 i=0; i<count; i++)
00025     {
00026         setParameter(getParameterName(i), 0);
00027     }
00028     Parameters.clear();
00029     dbg->log("Config deleted");
00030 }
00031 
00032 void Config::loadConfig(c8* fileName)
00033 {
00034     dbg->log("loadConfig", fileName);
00035     // read configuration from xml file
00036     IXMLReaderUTF8* xml = device->getFileSystem()->createXMLReaderUTF8(fileName);
00037 
00038     while(xml && xml->read())
00039     {
00040         switch (xml->getNodeType())
00041         {
00042                 case EXN_ELEMENT:
00043             {
00044                 const c8* nodeData = xml->getNodeData();
00045                 const c8* nodeName = xml->getNodeName();
00046 
00047                 s32 count = xml->getAttributeCount();
00048                 for (s32 i=0; i<count; i++)
00049                 {
00050                     const c8* name = xml->getAttributeName(i);
00051                     const c8* stringValue = xml->getAttributeValue(i);
00052                     if (name && stringValue)
00053                     {
00054                         setParameter(name, stringValue);
00055                     }
00056                 }
00057             }
00058             break;
00059         }
00060     }
00061     if (xml)
00062     {
00063         xml->drop();
00064     }
00065     else
00066     {
00067         dbg->log("cannot load config", fileName);
00068     }
00069 }
00070 
00071 void Config::safeConfig(c8* description, c8* fileName)
00072 {
00073     dbg->log("safeConfig", fileName);
00074     IXMLWriter* xml = device->getFileSystem()->createXMLWriter(fileName);
00075     xml->writeXMLHeader();
00076     stringw lineBreak("\r\n");
00077     xml->writeText(lineBreak.c_str());
00078 
00079     stringw element(description);
00080     s32 count = getParameterCount();
00081 
00082     for (s32 i=0; i<count; i++)
00083     {
00084         stringw name(getParameterName(i));
00085         stringw value(getParameterValue(i));
00086         xml->writeElement(element.c_str(), true, name.c_str(), value.c_str());
00087         xml->writeText(lineBreak.c_str());
00088     }
00089     if (xml)
00090     {
00091         xml->drop();
00092     }
00093     else
00094     {
00095         dbg->log("cannot safe config", fileName);
00096     }
00097 }
00098 
00099 void Config::safeDefaultConfig(const c8* fileName)
00100 {
00101     dbg->log("safeDefaultConfig", fileName);
00102     IXMLWriter* xml = device->getFileSystem()->createXMLWriter(fileName);
00103     xml->writeXMLHeader();
00104 
00105     stringw lineBreak("\r\n");
00106     xml->writeText(lineBreak.c_str());
00107 
00108     stringw element, name, value;
00109 
00110     element = "CONFIG";
00111     name = "SCREEN_X";
00112     value = "800";
00113     xml->writeElement(element.c_str(), true, name.c_str(), value.c_str());
00114     xml->writeText(lineBreak.c_str());
00115 
00116     name = "SCREEN_Y";
00117     value ="600";
00118     xml->writeElement(element.c_str(), true, name.c_str(), value.c_str());
00119     xml->writeText(lineBreak.c_str());
00120 
00121     name = "FULLSCREEN";
00122     value = "0";
00123     xml->writeElement(element.c_str(), true, name.c_str(), value.c_str());
00124     xml->writeText(lineBreak.c_str());
00125 
00126     name = "BITDEPTH";
00127     value = "16";
00128     xml->writeElement(element.c_str(), true, name.c_str(), value.c_str());
00129     xml->writeText(lineBreak.c_str());
00130 
00131     if (xml)
00132     {
00133         xml->drop();
00134     }
00135     else
00136     {
00137         dbg->log("cannot safe default config", fileName);
00138     }
00139 }
00140 
00144 void Config::setParameter(const c8* parameterName, const c8* value)
00145 {
00146         for (int i=0; i<(int)Parameters.size(); ++i)
00147                 if (Parameters[i].Name == parameterName)
00148                 {
00149                         if (!value)
00150                                 Parameters.erase(i);
00151                         else
00152                                 Parameters[i].Value = value;
00153                         return;
00154                 }
00155 
00156         if (value)
00157         {
00158                 Parameters.push_back(SStringPair());
00159                 Parameters.getLast().Name = parameterName;
00160                 Parameters.getLast().Value = value;
00161         }
00162 }
00163 
00168 const c8* Config::getParameter(const c8* parameterName)
00169 {
00170         for (int i=0; i<(int)Parameters.size(); ++i)
00171                 if (Parameters[i].Name == parameterName)
00172                         return Parameters[i].Value.c_str();
00173 
00174         return 0;
00175 }
00176 
00177 Config::SStringPair* Config::getParameterP(const c8* parameterName)
00178 {
00179         for (int i=0; i<(int)Parameters.size(); ++i)
00180                 if (Parameters[i].Name == parameterName)
00181                         return &Parameters[i];
00182 
00183         return 0;
00184 }
00185 
00186 
00188 void Config::setParameter(const c8* parameterName, bool value)
00189 {
00190         setParameter(parameterName, value ? "true" : "false");
00191 }
00192 
00197 bool Config::getParameterAsBool(const c8* parameterName)
00198 {
00199         bool ret = false;
00200 
00201         SStringPair* p = getParameterP(parameterName);
00202         if (p)
00203                 ret = (p->Value == "true");
00204 
00205         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00206         return ret;
00207 }
00208 
00210 void Config::setParameter(const c8* parameterName, s32 value)
00211 {
00212         core::stringc s = value;
00213         setParameter(parameterName, s.c_str());
00214 }
00215 
00220 s32 Config::getParameterAsInt(const c8* parameterName)
00221 {
00222         return (s32)getParameterAsFloat(parameterName);
00223 }
00224 
00226 void Config::setParameter(const c8* parameterName, f32 value)
00227 {
00228         char tmp[32];
00229         sprintf(tmp, "%f", value);
00230         setParameter(parameterName, tmp);
00231 }
00232 
00237 f32 Config::getParameterAsFloat(const c8* parameterName)
00238 {
00239         SStringPair* p = getParameterP(parameterName);
00240         if (!p)
00241                 return false;
00242 
00243         return core::fast_atof(p->Value.c_str());
00244 }
00245 
00246 
00248 s32 Config::getParameterCount()
00249 {
00250         return Parameters.size();
00251 }
00252 
00255 const c8* Config::getParameterName(s32 index)
00256 {
00257         if (index < 0 || index >= (int)Parameters.size())
00258                 return 0;
00259 
00260         return Parameters[index].Name.c_str();
00261 }
00262 
00265 const c8* Config::getParameterValue(s32 index)
00266 {
00267         if (index < 0 || index >= (int)Parameters.size())
00268                 return 0;
00269 
00270         return Parameters[index].Value.c_str();
00271 }
00272 
00273 

Generated on Sun Dec 2 17:09:57 2007 for Swarm by  doxygen 1.4.6-NO