xmlparser.h

来自「可移植的xml库。已经在windows和linux上测试通过。只使用C++ Ru」· C头文件 代码 · 共 159 行

H
159
字号
#ifndef LIBXML_XMLPARSER_H
#define LIBXML_XMLPARSER_H

//xmlparser.h

enum XMLPARSERSUGGESTION
{
	XPS_STOP=-1,	//结束分析
	XPS_CONTINUE=0,	//顺数据流分析下一个元素
	XPS_UPPERLEVEL,	//向上退n级
};

class CXMLParser;
class CXMLNodeCache;

struct CXMLClient
{
	//return XMLPARSERSUGGESTION value
	//called once every parsed node or chunk depth reached
	virtual int handleNode(CXMLParser& prs,CXMLNode *x)=0;
};

class CXMLParser
{
	enum
	{
		F_SAX			= 1,
		F_LOAD_COMMENT	= 2,
	};
	//cache
	CMemZone m_pb;		//parse buffer
	unsigned m_bufsize;	//use when parse file
	CXMLNodeCache m_xnc;

	//error diagnose
	int m_ecode;		//error code
	unsigned m_ln;		//last line
	unsigned m_char;	//last char
	char m_estr[16];	//last up to 15 bytes string for diagnose

	//parser state tracking
	unsigned m_depth;	//last depth
	CXMLNode *m_last;	//last parsed node

	//interfaces and extern entities
	CXMLCodec *m_x;		//tag CData CODEC
	CXMLClient *m_c;	//client instance

	//parser controls
	int m_navi;				//parser navigation
	unsigned m_flags;		//control flags
	unsigned m_naviexp;		//track depth for parser navigating
	unsigned m_stopLevel;	//filter out all deeper levels
	unsigned m_chunkLevel;	//split parsed tree into chunks and callback into client

	void init();
	int appendNTNode(CXMLNode *x);
	int appendTermNode(CXMLNode *x);
	int setupErrCtx(const char *p,int e);
public:
	CXMLParser();
	~CXMLParser();

	//error diagnose
	int errCode() const;
	unsigned errLine() const;
	unsigned errChar() const;
	const char* errStr() const;

	//accessor
	unsigned getDepth() const;
	CXMLNode* getLast() const;
	unsigned getBufSize() const;

	CXMLCodec* getCodec() const;

	//info query
	bool isSAX() const;
	bool isEmpty() const;
	bool isLoadComments() const;
	bool isClosedOn(const CXMLNode *x);
	bool isFlagSet(unsigned mask) const;

	unsigned getStopLevel() const;
	unsigned getChunkLevel() const;
	unsigned getCacheLimit() const;
	unsigned getOptions(unsigned mask) const;

	//controller
	void reset();
	CXMLNode* clearTree();
	void setSAX(bool sax);
	void setBufSize(unsigned bufsize);
	void setCacheLimit(unsigned limit);

	void setCodec(CXMLCodec *x);

	void setOptions(unsigned mask);
	void setLoadComments(bool load);
	void clearOptions(unsigned mask);
	void inverseOptions(unsigned mask);

	void setStopLevel(unsigned level);
	void setChunkLevel(unsigned level);

	CXMLNode* allocNode(bool nonew=false);
	CXMLAttrib* allocAttrib(bool nonew=false);

	//SAX parser
	int saxParse(CXMLClient *c,FILE *fp);
	int saxAppend(const char *buf,unsigned bytes=0);
	int saxParseFile(CXMLClient *c,const char *path);
	int saxParse(CXMLClient *c,const char *buf,unsigned bytes=0);

	//DOM parser
	int domParse(CXMLNode **root,FILE *fp);
	int domParseFile(CXMLNode **root,const char *path);
	int domParse(CXMLNode **root,const char *buf,unsigned bytes=0);
};

inline int CXMLParser::errCode() const{return m_ecode;}
inline unsigned CXMLParser::errLine() const{return m_ln;}
inline bool CXMLParser::isEmpty() const{return m_last==0;}
inline CXMLCodec* CXMLParser::getCodec() const{return m_x;}
inline unsigned CXMLParser::errChar() const{return m_char;}
inline CXMLNode* CXMLParser::getLast() const{return m_last;}
inline const char* CXMLParser::errStr() const{return m_estr;}
inline unsigned CXMLParser::getDepth() const{return m_depth;}
inline unsigned CXMLParser::getBufSize() const{return m_bufsize;}
inline bool CXMLParser::isSAX() const{return (m_flags & F_SAX)!=0;}
inline bool CXMLParser::isLoadComments() const{return (m_flags & F_LOAD_COMMENT)!=0;}
inline bool CXMLParser::isFlagSet(unsigned mask) const{return (m_flags & mask)!=0;}
inline unsigned CXMLParser::getStopLevel() const{return m_stopLevel;}
inline unsigned CXMLParser::getChunkLevel() const{return m_chunkLevel;}
inline unsigned CXMLParser::getCacheLimit() const{return m_pb.getLimit();}
inline unsigned CXMLParser::getOptions(unsigned mask) const{return m_flags & mask;}
inline void CXMLParser::setSAX(bool sax)
{
	if(sax) m_flags |= F_SAX;
	else m_flags &= ~F_SAX;
}
inline void CXMLParser::setBufSize(unsigned bufsize){m_bufsize=bufsize;}
inline void CXMLParser::setCacheLimit(unsigned limit){m_pb.setLimit(limit);}
inline void CXMLParser::setCodec(CXMLCodec *x){m_x=x;}
inline void CXMLParser::setOptions(unsigned mask){m_flags |= mask;}
inline void CXMLParser::setLoadComments(bool load)
{
	if(load) m_flags |= F_LOAD_COMMENT;
	else m_flags &= ~F_LOAD_COMMENT;
}
inline void CXMLParser::clearOptions(unsigned mask){m_flags &= ~mask;}
inline void CXMLParser::inverseOptions(unsigned mask){m_flags ^= mask;}
inline void CXMLParser::setStopLevel(unsigned level){m_stopLevel=level;}
inline void CXMLParser::setChunkLevel(unsigned level){m_chunkLevel=level;}
inline CXMLNode* CXMLParser::allocNode(bool nonew){return m_xnc.reuse(nonew);}
inline CXMLAttrib* CXMLParser::allocAttrib(bool nonew){return m_xnc.allocAttrib(nonew);}

#endif //LIBXML_XMLPARSER_H

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?