bool CSettings::IsList(String strSetting) { SettingsValue * setting = GetSetting(strSetting); if(setting && setting->IsList()) return true; return false; }
bool CSettings::IsInteger(CString strSetting) { SettingsValue * setting = GetSetting(strSetting); if(setting && setting->IsInteger()) return true; return false; }
String CSettings::GetEx(String strSetting) { String strValue; // Get the setting SettingsValue * setting = GetSetting(strSetting); // Does the setting exist? if(setting) { if(setting->IsBool()) strValue.FromBoolean(setting->bValue); else if(setting->IsInteger()) strValue.FromInteger(setting->iValue); else if(setting->IsFloat()) strValue.FromFloat(setting->fValue); else if(setting->IsString()) strValue = setting->strValue; } return strValue; }
// getConfig() SQInteger CServerNatives::GetConfig(SQVM * pVM) { // Create a new table sq_newtable(pVM); for(std::map<String, SettingsValue *>::iterator iter = CSettings::GetValues()->begin(); iter != CSettings::GetValues()->end(); iter++) { // Get the setting SettingsValue * setting = iter->second; // Push the setting name onto the stack sq_pushstring(pVM, iter->first.Get(), iter->first.GetLength()); // Push the value onto the stack if(setting->IsBool()) sq_pushbool(pVM, setting->bValue); else if(setting->IsInteger()) sq_pushinteger(pVM, setting->iValue); else if(setting->IsFloat()) sq_pushfloat(pVM, setting->fValue); else if(setting->IsString()) sq_pushstring(pVM, setting->strValue, setting->strValue.GetLength()); else if(setting->IsList()) { // Create a new array sq_newarray(pVM, 0); for(std::list<String>::iterator iter2 = setting->listValue.begin(); iter2 != setting->listValue.end(); iter2++) { // Push the list value onto the stack sq_pushstring(pVM, (*iter2).Get(), (*iter2).GetLength()); // Create a new array slot sq_arrayappend(pVM, -2); } } // Create a new table slot sq_createslot(pVM, -3); } return 1; }
// getConfig() int CServerNatives::GetConfig(lua_State * pVM) { // Create a new table script_newtable(pVM, 0); for(std::map<String, SettingsValue *>::iterator iter = CSettings::GetValues()->begin(); iter != CSettings::GetValues()->end(); iter++) { // Get the setting SettingsValue * setting = iter->second; // Push the setting name onto the stack script_pushlstring(pVM, iter->first.Get(), iter->first.GetLength()); // Push the value onto the stack if(setting->IsBool()) script_pushbool(pVM, setting->bValue); else if(setting->IsInteger()) script_pushinteger(pVM, setting->iValue); else if(setting->IsFloat()) script_pushfloat(pVM, setting->fValue); else if(setting->IsString()) script_pushlstring(pVM, setting->strValue, setting->strValue.GetLength()); else if(setting->IsList()) { // Create a new array CScriptArray a(pVM); for(std::list<String>::iterator iter2 = setting->listValue.begin(); iter2 != setting->listValue.end(); iter2++) { // Push the list value onto the stack a.push(iter2->Get(), iter2->GetLength()); // Create a new array slot //script_arrayappend(pVM, -2); } } // Create a new table slot script_tableset(pVM, -3); } return 1; }
bool CSettings::Save() { // Are we not flagged as open? if(!m_bOpen) return false; // Are we not flagged as allowed to save the file? if(!m_bSave) return false; // Loop through all values for(std::map<String, SettingsValue *>::iterator iter = m_values.begin(); iter != m_values.end(); iter++) { // Get the setting pointer SettingsValue * setting = iter->second; // Find all nodes for this value bool bFoundNode = false; for(TiXmlNode * pNode = m_XMLDocument.RootElement()->FirstChildElement(iter->first.Get()); pNode; pNode = pNode->NextSibling()) { // Is this not an element node? if(pNode->Type() != TiXmlNode::ELEMENT) continue; // Is this not the node we are looking for? if(iter->first.Compare(pNode->Value())) continue; // Is this a list node? if(setting->IsList()) { // Remove the node m_XMLDocument.RootElement()->RemoveChild(pNode); } else { // Clear the node pNode->Clear(); // Get the node value String strValue = GetEx(iter->first); // Create a new node value TiXmlText * pNewNodeValue = new TiXmlText(strValue.Get()); // Add the new node value to the new node pNode->LinkEndChild(pNewNodeValue); } // Flag as found a node bFoundNode = true; break; } // Is this a list value? if(setting->IsList()) { // Loop through each list item for(std::list<String>::iterator iter2 = setting->listValue.begin(); iter2 != setting->listValue.end(); iter2++) { // Create a new node TiXmlElement * pNewNode = new TiXmlElement(iter->first.Get()); // Create a new node value TiXmlText * pNewNodeValue = new TiXmlText((*iter2).Get()); // Add the new node value to the new node pNewNode->LinkEndChild(pNewNodeValue); // Add the new node to the XML document m_XMLDocument.RootElement()->LinkEndChild(pNewNode); } } else { // Do we need to create a new node? if(!bFoundNode) { // Create a new node TiXmlElement * pNewNode = new TiXmlElement(iter->first.Get()); // Get the node value String strValue = GetEx(iter->first); // Create a new node value TiXmlText * pNewNodeValue = new TiXmlText(strValue.Get()); // Add the new node value to the new node pNewNode->LinkEndChild(pNewNodeValue); // Add the new node to the XML document m_XMLDocument.RootElement()->LinkEndChild(pNewNode); } } } // Save the XML document return m_XMLDocument.SaveFile(); }