www.pudn.com > ICE_1.4.zip > ICE_options.cpp


// Class automatically generated by Dev-C++ New Class wizard 
 
#include "ICE_options.h" // class's header file 
 
//realize static members here 
// DONT SET INITIAL VALUES HERE, DO THAT IN THE INIT() FUNCTION!! 
bool ICE_Options::needsSaving; 
std::map ICE_Options::m_prefs; 
std::map ICE_Options::m_config;	 
 
// class constructor 
ICE_Options::ICE_Options() 
{ 
	// insert your code here 
} 
 
// class destructor 
ICE_Options::~ICE_Options() 
{ 
    //make sure preferences are saved 
} 
 
void ICE_Options::Save() { 
	if(!needsSaving) return; 
	std::map::iterator itr; 
 
	q_XML* xml = new q_XML_WRITENEW(OPTIONS_FILE); 
	if( xml->isError() ) return; 
 
	xml->addAttrStr("version", OPTIONS_VERSION); 
 
	//save configuration hash 
	xml->createNewNode("configuration", "device configuration"); 
	itr = m_config.begin(); 
	while(itr != m_config.end()) { 
		//xml->addAttrStr("fullscreen", m_config["fullscreen"].c_str()); 
		xml->addAttrStr((*itr).first.c_str(), (*itr).second.c_str()); 
		itr++; 
	} 
	xml->endCurrNode(); 
 
	//save preferences hash 
	xml->createNewNode("preferences", "user preferences"); 
	itr = m_prefs.begin(); 
	while(itr != m_prefs.end()) { 
		//xml->addAttrStr("sound on", m_config["sound on"].c_str()); 
		xml->addAttrStr((*itr).first.c_str(), (*itr).second.c_str()); 
		itr++; 
	} 
	xml->endCurrNode(); 
 
	delete xml; 
	needsSaving = false; 
} 
 
void ICE_Options::Initialize() { 
	q_XML* xml = new q_XML_READER(OPTIONS_FILE); 
	if( ! xml->isError() ) {  
		//read from file 
		attr_info attr; 
		node_info node; 
		 
		xml->nextAttr(); 
		attr = xml->getCurrAttr(); 
		if(attr.attrValue != OPTIONS_VERSION || xml->isError()) { 
			QXML_DEBUG( printf("ERROR! wrong ICE options version\n"); ); 
			delete xml; 
			return; 
		} 
		if(xml->nextNode() != "configuration" || xml->isError()) { 
			QXML_DEBUG( printf("ERROR! malformed ICE options: configuration node not found\n"); ); 
			delete xml; 
			return; 
		} 
		while(xml->nextAttr() != "" && !xml->isError()) { 
			attr = xml->getCurrAttr(); 
			m_config[attr.attrName] = attr.attrValue; 
			QXML_DEBUG( printf("SETTING ATTR PREF [%s]=[%s]\n", attr.attrName.c_str(), attr.attrValue.c_str()); ); 
		} 
 
		if(xml->nextNode() != "preferences" || xml->isError()) { 
			QXML_DEBUG( printf("ERROR! malformed ICE options: preferences node not found\n"); ); 
			delete xml; 
			return; 
		} 
		while(xml->nextAttr() != "" && !xml->isError()) { 
			attr = xml->getCurrAttr(); 
			m_prefs[attr.attrName] = attr.attrValue; 
			QXML_DEBUG( printf("SETTING ATTR PREF [%s]=[%s]\n", attr.attrName.c_str(), attr.attrValue.c_str()); ); 
		} 
		QXML_DEBUG( printf("DONE READING ATTRS\n"); ); 
		needsSaving = false; 
		delete xml; 
	}else {  
		delete xml; 
		xml = NULL; 
		//initialize static values 
		m_config["fullscreen"] = "F"; 
		m_config["resolution"] = "640x480"; 
		m_config["colordepth"] = "16"; 
		m_config["device"] = "OpenGL"; //"Software"; "DirectX8"; 
		m_config["configured"] = "F"; 
 
		needsSaving = true; 
		Save(); 
	} 
} 
 
std::string ICE_Options::getConfig(std::string key) { 
	return m_config[key]; 
} 
void ICE_Options::setConfig(std::string key, std::string val) { 
	m_config[key] = val; 
	needsSaving = true; 
} 
std::string ICE_Options::getPreference(std::string key) { 
	return m_prefs[key]; 
}