www.pudn.com > ICE_1.4.zip > q_xml.h


// Class automatically generated by Dev-C++ New Class wizard 
 
#ifndef Q_XML_H 
#define Q_XML_H 
 
/*********************** 
	q_XML -- Quick XML  v0.1.0 
	by Paul Zirkle	1/10/04 
	keless@skyesurfer.net 
	------------------ 
the following class will help you quickly and  
easily save data in a pseudo (read: non-compliant)  
XML format, as well as read it back in.  
 
see example code at the bottom of this file 
 
TODO: 
 READING-- 
 * make sure CLOSE tags correspond to current Node 
 * make sure nextAttr() deals with attrs on either side of a sub-node 
 
 
************************/ 
 
 
#include  
#include  
#include  
//#include  
 
#define q_XML_VERSION "0.1.1" 
//uncomment this to enable printf() debug output 
//#define q_XML_DEBUG 
 
#define q_XML_READER(fname) q_XML(fname, QXML_READ) 
#define q_XML_WRITENEW(fname) q_XML(fname, QXML_NUKEWRITE) 
#define q_XML_WRITEAPPEND(fname) q_XML(fname, QXML_WRITE) 
 
 
 
#ifdef q_XML_DEBUG 
#define QXML_DEBUG(stuff) stuff 
#else 
#define QXML_DEBUG(stuff) /* code removed */ 
#endif 
 
//file handling mode (read/writing) 
enum eQXML_MODES { 
    QXML_WRITE = 0, 
    QXML_NUKEWRITE, 
    QXML_READ, 
     
    QXML_MODES_END    //number of last QXML enum val 
}; 
 
//reading parse modes 
enum eQXMLP_MODE { 
	QXMLP_VOID = 0,	 // outside of any tag 
	QXMLP_TAG_START, // <     (goes to NODE/ATTR/CLOSE or error) 
	QXMLP_TAG_NODE,  // < node= 
	QXMLP_TAG_ATTR,  // < attr= 
	QXMLP_TAG_CLOSE, //  m_tokenStack; 
 
	//reading vars 
	eQXMLP_MODE	 m_parse; 
	std::string currTag; 
	std::string currName; 
	std::string currType; 
	std::string currVal; 
 
	public: 
		// filname : of the XML file to open 
		// write   : writing to xml, or reading from 
		// nuke    : when writing, destroy old file instead of appending to it 
		q_XML(const char*filename, eQXML_MODES mode = QXML_READ); 
		 
		//class destructor 
		~q_XML(); 
		 
		//writing functions 
		void createNewNode(const char*nodeName, const char*type); 
		void addAttribute(const char*attrType, const char*attrName, const char*attrVal); 
		void addAttrInt(const char*attrName, int attrVal); 
		void addAttrFloat(const char*attrName, float attrVal); 
		void addAttrStr(const char*attrName, const char* attrVal); 
 		void addAttrBool(const char*attrName, bool attrVal); 
		void endCurrNode(); 
		void endNode(const char*nodeName); 
		 
		//reading functions 
		std::string nextNode(); //moves to the next node and returns it's name, "" for none 
		std::string nextAttr(); //moves to next attr node and returns its name, "" for none 
		node_info getCurrNode(); 
		attr_info getCurrAttr(); 
		 
		//error handling funcitons    
		bool isError() { return m_err; } 
 
private: 
		//private reading functions 
		bool nextTag(); //advance to next '<' 
		bool seekTagEnd(); 
		bool readTag(); 
		bool readNode(); 
		bool readAttr(); 
		std::string readClose();  //return name of closed node 
		std::string readVal(); 
 
 
		void dump() { 
			std::string parse; 
			switch(m_parse) { 
				case QXMLP_VOID: parse = "QXMLP_VOID"; break; 	 // outside of any tag 
				case QXMLP_TAG_START: parse = "QXMLP_TAG_START"; break; // <     (goes to NODE/ATTR/CLOSE or error) 
				case QXMLP_TAG_NODE: parse = "QXMLP_TAG_NODE"; break;  // < node= 
				case QXMLP_TAG_ATTR: parse = "QXMLP_TAG_ATTR"; break;  // < attr= 
				case QXMLP_TAG_CLOSE: parse = "QXMLP_TAG_CLOSE"; break; // createNewNode("graphics", "preferences"); 
    xml->addAttribute("bool", "fullscreen", "F"); 
    xml->addAttribute("string", "resolution", "640x480"); 
    xml->endCurrNode(); 
    xml->createNewNode("resolutions", "options"); 
    xml->addAttribute("string", "resolution", "640x480"); 
    xml->addAttribute("string", "resolution", "800x600"); 
    xml->addAttribute("string", "resolution", "1024x760"); 
    delete xml; 
PRODUCES: 
< node="root" type="qXML" > 
	< attr="version" type="string" value="0.1.0" > 
	< node="graphics" type="preferences" > 
		< attr="fullscreen" type="bool" value="F" > 
		< attr="resolution" type="string" value="640x480" > 
	 
	< node="resolutions" type="options" > 
		< attr="resolution" type="string" value="640x480" > 
		< attr="resolution" type="string" value="800x600" > 
		< attr="resolution" type="string" value="1024x760" > 
	 
 
    
EXAMPLE CODE (READING): 
	std::string temp; 
	//* read in preferences from a pseudo XML file 
	q_XML *xml = new q_XML_READER("text.pref"); 
	temp  = xml->nextNode(); 
	printf("read node: %s\n", temp.c_str() ); 
	temp  = xml->nextNode(); 
	printf("read node: %s\n", temp.c_str() ); 
	xml->nextAttr(); 
	xml->nextAttr(); 
	attr_info attribute = xml->getCurrAttr(); 
	printf("2nd attr: %s as %s equals %s\n",  
		attribute.attrName.c_str(),  
		attribute.attrType.c_str(),  
		attribute.attrValue.c_str()); 
 
	temp = xml->nextNode(); 
	if(temp == "") 
		printf("no more nodes\n"); 
	delete xml; 
PROCUCES: 
read node: graphics 
read node: resolutions 
2nd attr: resolution as string equals 800x600 
no more nodes 
 
******************/ 
 
 
#endif // Q_XML_H