2020-09-17 23:30:25 +08:00
|
|
|
|
#ifndef HGL_UTIL_XML_ELEMENT_PARSE_KV_INCLUDE
|
2020-08-21 21:11:07 +08:00
|
|
|
|
#define HGL_UTIL_XML_ELEMENT_PARSE_KV_INCLUDE
|
|
|
|
|
|
|
|
|
|
#include<hgl/util/xml/ElementParse.h>
|
2020-09-03 15:58:38 +08:00
|
|
|
|
#include<hgl/type/String.h>
|
2020-08-21 21:11:07 +08:00
|
|
|
|
#include<hgl/type/Map.h>
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace xml
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* KV模式XML节点解析器
|
|
|
|
|
*/
|
|
|
|
|
class ElementParseKV:public ElementParse
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
using AttrsMap=Map<UTF8String,UTF8String>;
|
2023-04-20 21:46:04 +08:00
|
|
|
|
using AttrItem=KeyValue<UTF8String,UTF8String>;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
AttrsMap attrs_map;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
const AttrItem *GetAttrItem(const UTF8String &name);
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
2023-04-20 21:46:04 +08:00
|
|
|
|
template<typename T> const bool GetInteger (const UTF8String &name,T &value){const AttrItem *ai=GetAttrItem(name);return(ai?stoi(ai->value.c_str(),value):false);}
|
|
|
|
|
template<typename T> const bool GetUInteger (const UTF8String &name,T &value){const AttrItem *ai=GetAttrItem(name);return(ai?stou(ai->value.c_str(),value):false);}
|
|
|
|
|
template<typename T> const bool GetFloat (const UTF8String &name,T &value){const AttrItem *ai=GetAttrItem(name);return(ai?stof(ai->value.c_str(),value):false);}
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
virtual ~ElementParseKV()=default;
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
virtual void Attr(const u8char *flag,const u8char *info) override;
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
const bool IsExist (const UTF8String &name)const{return attrs_map.KeyExist(name);}
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
2023-04-20 21:46:04 +08:00
|
|
|
|
const u8char * ToCString (const UTF8String &name){const AttrItem *ai=GetAttrItem(name);return(ai?ai->value.c_str():nullptr);}
|
2020-08-25 21:47:05 +08:00
|
|
|
|
const u8char * operator[] (const UTF8String &name){return ToCString(name);}
|
2020-08-21 21:11:07 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2020-08-25 21:47:05 +08:00
|
|
|
|
const bool Get(const UTF8String &name,UTF8String &str);
|
|
|
|
|
const bool Get(const UTF8String &name,UTF16String &str);
|
|
|
|
|
const bool Get(const UTF8String &name,u8char &ch);
|
|
|
|
|
const bool Get(const UTF8String &name,bool &value);
|
|
|
|
|
|
|
|
|
|
const bool Get(const UTF8String &name, int8 &value){return GetInteger < int8 >(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name,uint8 &value){return GetUInteger<uint8 >(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name, int16 &value){return GetInteger < int16>(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name,uint16 &value){return GetUInteger<uint16>(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name, int32 &value){return GetInteger < int32>(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name,uint32 &value){return GetUInteger<uint32>(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name, int64 &value){return GetInteger < int64>(name,value);}
|
|
|
|
|
const bool Get(const UTF8String &name,uint64 &value){return GetUInteger<uint64>(name,value);}
|
|
|
|
|
|
|
|
|
|
const bool GetHexStr(const UTF8String &name,uint8 *data);
|
2020-08-21 21:11:07 +08:00
|
|
|
|
};//class ElementParseKV:public ElementParse
|
|
|
|
|
}//namespace xml
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_UTIL_XML_ELEMENT_PARSE_KV_INCLUDE
|