⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlattrib.h

📁 可移植的xml库。已经在windows和linux上测试通过。只使用C++ Runtine
💻 H
字号:
#ifndef LIBXML_XMLATTRIB_H
#define LIBXML_XMLATTRIB_H

//xmlattrib.h
    
class CXMLNode;
class CXMLAttribCache;

class CXMLAttrib
{
	char *m_val;		//属性的值
	char *m_name;		//属性的名称
	CXMLNode *m_node;	//归属的XML节点
	CXMLAttrib *m_prev;	//前一个属性
	CXMLAttrib *m_next;	//下一个属性
	unsigned short m_nbs;//名称缓冲区的总大小
	unsigned short m_vbs;//值缓冲区的总大小
	unsigned short m_nlen;//名称的长度
	unsigned short m_vlen;//值的长度
	CXMLAttribCache *m_xac;//缓存
	void reset();
	void clean();
public:
	//构造函数ctor
	CXMLAttrib();
	CXMLAttrib(const char *p);
	CXMLAttrib(const CXMLAttrib& a);
	CXMLAttrib(const char *name,int val);
	CXMLAttrib(const char *name,bool val);
	CXMLAttrib(const char *name,long val);
	CXMLAttrib(const char *name,unsigned val);
	CXMLAttrib(const char *name,const char *val);
	CXMLAttrib(const char *name,unsigned long val);

	//析构函数dtor
	~CXMLAttrib();

	//引用/转换值convertor
	operator char() const;	//转换成字符型,值必须只有一个字节长
	operator int() const;	//使用atoi把值转换成int
	operator long() const;	//使用atol把值转换成long
	operator bool() const;	//使用atoi把值转换成bool
	operator char*() const;	//引用值
	operator unsigned() const;		//使用atoi把值转换成unsigned
	operator const char*() const;	//引用值
	operator unsigned long() const;	//使用atol把值转换成unsigned long

	//赋值assignment
	CXMLAttrib& operator=(int val);			//char,int
	CXMLAttrib& operator=(bool val);
	CXMLAttrib& operator=(long val);
	CXMLAttrib& operator=(unsigned val);		//unsigned char, unsigned
	CXMLAttrib& operator=(const char *val);		//char*, const char*
	CXMLAttrib& operator=(unsigned long val);	//unsigned long
	CXMLAttrib& operator=(const CXMLAttrib& a);

	//比较值相等compare value to equal
	bool operator==(int val) const;				//char,int,long
	bool operator==(bool val) const;
	bool operator==(long val) const;
	bool operator==(unsigned val) const;		//unsigned char, unsigned
	bool operator==(const char *val) const;		//char*, const char*
	bool operator==(unsigned long val) const;	//unsigned long
	bool operator==(const CXMLAttrib& a) const;

	//比较值compare value to different
	bool operator!=(int val) const;				//char,int,long
	bool operator!=(bool val) const;
	bool operator!=(long val) const;
	bool operator!=(unsigned val) const;		//unsigned char, unsigned
	bool operator!=(const char *val) const;		//char*, const char*
	bool operator!=(unsigned long val) const;	//unsigned long
	bool operator!=(const CXMLAttrib& a) const;

	//名字查询器name query
	bool isNameOf(const char *p,unsigned len=0) const;		//不分大小写匹配属性的名字
	bool isNameOfExact(const char *p,unsigned len=0) const;//区分大小写匹配属性的名字

	//值查询器value query
	//bool isValueOf(const char *p) const;	//不分大小写匹配属性的值 - 用"=="吧
	bool isValueOfExact(const char *p,unsigned len=0) const;//区分大小写匹配属性的值

	//名字和值查询器
	bool isSameTo(const CXMLAttrib& a) const;
	bool isSameToExact(const CXMLAttrib& a) const;

	bool isChildOf(const CXMLNode *x) const;

	//存储查询器memory query
	unsigned strBytes() const;	//字符串形式的字节数,不包括'\0'
	unsigned blockBytes() const;//内存形式的字节数,不包括'\0'
	
	//成员访问器accessor
	CXMLNode* getNode() const;
	CXMLAttrib* getPrev() const;
	CXMLAttrib* getNext() const;
	const char* getName() const;
	const char* getValue() const;
	unsigned updateNode(CXMLNode *p);
	CXMLAttribCache* getCache() const;
	unsigned short getNameLen() const;
	unsigned short getValueLen() const;
	CXMLAttrib* getlastSibling() const;
	CXMLAttrib* getFirstSibling() const;
	unsigned short getNameBufSize() const;
	unsigned short getValueBufSize() const;

	//成员控制器controller
	int setValue(int val);
	int setValue(bool val);
	void setNode(CXMLNode *p);
	int setName(const char *p);
	int setValue(unsigned val);
	void setPrev(CXMLAttrib *p);
	void setNext(CXMLAttrib *p);
	int setValue(const char *p);
	int clone(const CXMLAttrib& a);
	int setValue(long val);
	int setValue(unsigned long val);
	void setCache(CXMLAttribCache *xac);
	int setBoth(const char *name,int val);
	int setBoth(const char *name,bool val);
	int setBoth(const char *name,long val);
	int setBoth(const char *name,unsigned val);
	int setBoth(const char *name,const char *val);
	int replaceValue(const char *val,unsigned len);
	int replaceName(const char *name,unsigned len);
	int setBoth(const char *name,unsigned long val);

	void free();

	//表示形式转换器represent convertor
	int save(FILE *fp) const;
	char* toStr(char *p) const;	//导出
	int load(const char*& b,const char *e);//载入
	//static void skipName(const char*& p); //implmented as inlined xmlSkipName()
	static int skipValue(const char*& b,const char *e);
	static CXMLAttrib* load(CXMLAttribCache *xac,const char*& b,const char *e,int *err);//载入
};

inline void CXMLAttrib::reset(){memset(this,0,sizeof(*this));}

inline CXMLAttrib::operator char() const{assert(m_vlen==1);return *m_val;}
inline CXMLAttrib::operator int() const{return atoi(m_val);}
inline CXMLAttrib::operator long() const{return atol(m_val);}
#ifdef WIN32
#    pragma warning(disable:4800)
#endif
inline CXMLAttrib::operator bool() const{return (bool)atoi(m_val);}
#ifdef WIN32
#    pragma warning(default:4800)
#endif
inline CXMLAttrib::operator char*() const{return (char *)m_val;}
inline CXMLAttrib::operator unsigned() const{return (unsigned)atoi(m_val);}
inline CXMLAttrib::operator const char*() const{return m_val;}
inline CXMLAttrib::operator unsigned long() const{return (unsigned long)atol(m_val);}

inline CXMLAttrib& CXMLAttrib::operator=(int val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(bool val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(long val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(unsigned val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(const char *val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(unsigned long val)
{
	if(setValue(val)) assert(false);
	return *this;
}
inline CXMLAttrib& CXMLAttrib::operator=(const CXMLAttrib& a)
{
	if(clone(a)) assert(false);
	return *this;
}

inline bool CXMLAttrib::operator==(int val) const{return atoi(m_val)==val;}
#ifdef WIN32
#    pragma warning(disable:4800)
#endif
inline bool CXMLAttrib::operator==(bool val) const{return (bool)atoi(m_val)==val;}
#ifdef WIN32
#    pragma warning(default:4800)
#endif
inline bool CXMLAttrib::operator==(long val) const{return atol(m_val)==val;}
inline bool CXMLAttrib::operator==(unsigned val) const{return (unsigned)atoi(m_val)==val;}
inline bool CXMLAttrib::operator==(const char *val) const{return !strcmpi(m_val,val);}
inline bool CXMLAttrib::operator==(unsigned long val) const{return (unsigned long)atol(m_val)==val;}
inline bool CXMLAttrib::operator==(const CXMLAttrib& a) const{return isSameTo(a);}

inline bool CXMLAttrib::operator!=(int val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(bool val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(long val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(unsigned val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(const char *val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(unsigned long val) const{return !((*this)==val);}
inline bool CXMLAttrib::operator!=(const CXMLAttrib& a) const{return !isSameTo(a);}

inline const char* CXMLAttrib::getName() const{return m_name;}
inline const char* CXMLAttrib::getValue() const{return m_val;}
inline unsigned short CXMLAttrib::getNameLen() const{return m_nlen;}
inline unsigned short CXMLAttrib::getValueLen() const{return m_vlen;}
inline unsigned short CXMLAttrib::getNameBufSize() const{return m_nbs;}
inline unsigned short CXMLAttrib::getValueBufSize() const{return m_vbs;}
inline CXMLAttribCache* CXMLAttrib::getCache() const{return m_xac;}
inline CXMLAttrib* CXMLAttrib::getPrev() const{return m_prev;}
inline CXMLAttrib* CXMLAttrib::getNext() const{return m_next;}
inline CXMLNode* CXMLAttrib::getNode() const{return m_node;}

inline int CXMLAttrib::setValue(int val)
{
	char buf[12];
	sprintf(buf,"%d",val);
	return setValue(buf);
}
inline int CXMLAttrib::setValue(bool val)
{
	char buf[12];
	sprintf(buf,"%d",val);
	return setValue(buf);
}
inline int CXMLAttrib::setValue(unsigned val)
{
	char buf[12];
	sprintf(buf,"%u",val);
	return setValue(buf);
}
inline int CXMLAttrib::setValue(long val)
{
	char buf[12];
	sprintf(buf,"%ld",val);
	return setValue(buf);
}
inline int CXMLAttrib::setValue(unsigned long val)
{
	char buf[12];
	//ultoa(val,buf,10);
	sprintf(buf,"%lu",val);
	return setValue(buf);
}
inline int CXMLAttrib::clone(const CXMLAttrib& a){return setBoth(a.m_name,a.m_val);}
inline int CXMLAttrib::setBoth(const char *name,int val)
{
	char buf[12];
	sprintf(buf,"%d",val);
	return setBoth(name,buf);
}
inline int CXMLAttrib::setBoth(const char *name,bool val)
{
	char buf[12];
	sprintf(buf,"%d",val);
	return setBoth(name,buf);
}
inline int CXMLAttrib::setBoth(const char *name,long val)
{
	char buf[12];
	//ltoa(val,buf,10);
	sprintf(buf,"%ld",val);
	return setBoth(name,buf);
}
inline int CXMLAttrib::setBoth(const char *name,unsigned val)
{
	char buf[12];
	sprintf(buf,"%d",val);
	return setBoth(name,buf);
}
inline int CXMLAttrib::setBoth(const char *name,unsigned long val)
{
	char buf[12];
	//_ultoa(val,buf,10);
	sprintf(buf,"%lu",val);
	return setBoth(name,buf);
}
inline void CXMLAttrib::setCache(CXMLAttribCache *xac){m_xac=xac;}
inline void CXMLAttrib::setPrev(CXMLAttrib *p){m_prev=p;}
inline void CXMLAttrib::setNext(CXMLAttrib *p){m_next=p;}
inline void CXMLAttrib::setNode(CXMLNode *p){m_node=p;}

inline bool CXMLAttrib::isNameOf(const char *p,unsigned len) const
{
	if(len && m_nlen!=len) return false;
	return !strcmpi(m_name,p);
}
inline bool CXMLAttrib::isNameOfExact(const char *p,unsigned len) const
{
	if(len && m_nlen!=len) return false;
	return !strcmp(m_name,p);
}

//inline bool CXMLAttrib::isValueOf(const char *p) const{return !strcmp(m_val,p);}
inline bool CXMLAttrib::isValueOfExact(const char *p,unsigned len) const
{
	if(len && m_vlen!=len) return false;
	return !strcmp(m_val,p);
}

inline bool CXMLAttrib::isChildOf(const CXMLNode *x) const{return m_node==x;}

inline bool CXMLAttrib::isSameTo(const CXMLAttrib& a) const
{
	return m_nlen==a.m_nlen && m_vlen==a.m_vlen && !strcmpi(m_name,a.m_name) && !strcmpi(m_val,a.m_val);
}
inline bool CXMLAttrib::isSameToExact(const CXMLAttrib& a) const
{
	return m_nlen==a.m_nlen && m_vlen==a.m_vlen && !strcmp(m_name,a.m_name) && !strcmp(m_val,a.m_val);
}

inline unsigned CXMLAttrib::blockBytes() const
{
	assert(m_name && m_val);
	return m_nlen+1+m_vlen+1+sizeof(*this);//consumed memory bytes(include '\0')
}

//parser utilities
inline bool xmlIsBlank(const char c)
{
	return (c==' ' || c=='\r' || c=='\n' || c=='\t');
}
inline bool xmlIsDemilator(const char c)
{
	return c=='<' || c=='>' || c=='/' || c==' ' || c=='\r' || c=='\n' || c=='\t';
}
inline bool xmlIsAttribDemilator(const char c)
{
	return c=='=' || c=='<' || c=='>' || c=='/' || c==' ' || c=='\r' || c=='\n' || c=='\t';
}
inline bool xmlIsAttribTerminator(const char c)
{
	return c=='<' || c=='>' || c=='/' || c==' ' || c=='\r' || c=='\n' || c=='\t';
}
inline void xmlSkipBlanks(const char*& p)
{
	for(;*p && xmlIsBlank(*p);++p);
}
inline void xmlSkipBlanksReverse(const char*& p)
{
	for(;xmlIsBlank(*p);--p);
}
inline void xmlSkipSymbol(const char*& p)
{
	for(;*p && !xmlIsDemilator(*p);++p);
}
inline void xmlSkipAttribName(const char*& p)
{
	for(;*p && !xmlIsAttribDemilator(*p);++p);
}

#endif//LIBXML_XMLATTRIB_H

⌨️ 快捷键说明

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