Beispiel #1
0
/** Changes the server's technical note
  *
  * The xml document should be saved after this call.
  *
  * \param v The new server's technical note
  *
  */
void RainbruRPG::Server::xmlServerConf::setTechNote(const std::string& v){
  TiXmlText nameNode(v.c_str());

  TiXmlNode* childNode = root->FirstChild( "TechnicalNote" );
  if (childNode){
    childNode->Clear();
    childNode->InsertEndChild(nameNode);
  }
 
}
Beispiel #2
0
void XMLConfiguration::set_value(const string& section, const string& object, const string& value)
{
	bool section_found = false;
	bool object_found = false;
	
	TiXmlNode* child = 0;
	
	TiXmlNode* nephew = 0;
	
	if(m_document->NoChildren()==false)
	{
		
		while((!section_found) && ( child = m_document->IterateChildren( child ) ))
		{
			if(string(child->Value())==section)
			{
				section_found = true;
			}
		}
	}
		
	if(section_found == false)
	{
		LOG_INFO("XMLConfiguration: no section "<<section<<" found, creating.");
		child = new TiXmlElement( section.c_str() );
		m_document->LinkEndChild( child );
	}
	
	if(child->NoChildren()==false)
	{
		
		while((!object_found) &&( nephew = child->IterateChildren( nephew ) ))
		{
			if(string(nephew->Value())==object)
			{
				object_found = true;
			}
		}
	}
	
	if(object_found == false)
	{
		LOG_INFO("XMLConfiguration: no object "<<section<<"/"<<object<<" found, creating.");
		nephew = new TiXmlElement( object.c_str() );
		child->LinkEndChild( nephew );
	}
	
	nephew->Clear();
	
	TiXmlText* text_element = new TiXmlText( value.c_str() );
	nephew->LinkEndChild( text_element );
	
}
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();
}
Beispiel #4
0
int CHsMiscellany::SetSectionValue( CString lpszSection, CString lpszValue )
{

	CString strPath = "";
	TiXmlNode* pNode = NULL;

	if (lpszSection.IsEmpty())
	{// 删除所有<Section>下子节点

		strPath = "//Miscellany/Section";
		pNode = m_pConfigBase->GetNode(strPath, "", "", UserDoc);
		if (pNode && !::IsBadReadPtr(pNode, 1))
		{
			pNode->Clear(); // 删除所有子节点
			m_pConfigBase->GetXmlDocument(UserDoc)->SetModified(TRUE);
		}
		return 1;
	}
	else if (lpszValue.IsEmpty())
	{// 删除<Section>下 id=lpszSection的节点
		
		strPath = "//Miscellany/Section";
		pNode = m_pConfigBase->GetNode(strPath, "", "", UserDoc);
		if (pNode && !::IsBadReadPtr(pNode, 1))
		{
			strPath = "//Miscellany/Section/Item";
			TiXmlNode* pChildNode = m_pConfigBase->GetNode(strPath, "id", lpszSection, UserDoc);
			if (pChildNode && !::IsBadReadPtr(pChildNode, 1))
			{
				pNode->RemoveChild(pChildNode);
			}
		}
		return 1;
	}
	else
	{// 都不为空 更新节点

		strPath = "//Miscellany/Section";
		pNode = m_pConfigBase->SetNode(strPath);
		if (pNode && !::IsBadReadPtr(pNode, 1))
		{
			TiXmlNode* pChildNode = pNode->FirstChild();
			while(pChildNode)
			{
				if ( pChildNode->ToElement()->Attribute("id") == lpszSection)
				{
					m_pConfigBase->SetNodeAttrString(pChildNode, "value", lpszValue);
					return lpszValue.GetLength();
				}

				pChildNode = pChildNode->NextSibling();
			}

			TiXmlElement* pElement = new TiXmlElement("Item");
			pElement->SetAttribute("id", lpszSection);
			pElement->SetAttribute("value", lpszValue);
			pNode->LinkEndChild(pElement);
		}


		return lpszValue.GetLength();
	}

}