diff --git a/inc/hgl/util/plist/LoadPAttrib.h b/inc/hgl/util/plist/LoadPAttrib.h new file mode 100644 index 0000000..8af5881 --- /dev/null +++ b/inc/hgl/util/plist/LoadPAttrib.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +namespace hgl +{ + using namespace io; + + + /** + * 从文本文件中加载PList + * @param filename 文件名 + * @param pa_map 属性列表 + * @return 是否加载成功 + */ + template static int LoadFromTextFile(const OSString &filename,PAttribMap &pa_map) + { + StringList sl; + + if(LoadStringListFromTextFile(sl,filename)<=0) + return(false); + + int n=sl.GetCount(); + + while(n--) + Add(pa_map,sl[n]); + + return(true); + } +}//namespace hgl diff --git a/inc/hgl/util/plist/PAttrib.h b/inc/hgl/util/plist/PAttrib.h index 4fa5c2a..db06498 100644 --- a/inc/hgl/util/plist/PAttrib.h +++ b/inc/hgl/util/plist/PAttrib.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include #include @@ -17,21 +17,21 @@ namespace hgl PAttribBase()=default; virtual ~PAttribBase()=default; - virtual const bool ParseFromString(const BaseString &str)=0; - virtual BaseString MakeToString()const=0; + virtual const bool ParseFromString(const String &str)=0; + virtual String MakeToString()const=0; }; template class PAttrib:public PAttribBase { protected: - BaseString name; + String name; T value; T default_value; public: - PAttrib(const BaseString &n,const T &v) + PAttrib(const String &n,const T &v) { name=n; value=v; @@ -40,8 +40,8 @@ namespace hgl virtual ~PAttrib()=default; - virtual const bool ParseFromString(const BaseString &str)=0; - virtual BaseString MakeToString()const=0; + virtual const bool ParseFromString(const String &str)=0; + virtual String MakeToString()const=0; virtual const T &Get(){return value;} virtual void Set(const T &v){value=v;} @@ -55,7 +55,7 @@ namespace hgl public: - PNumberAttrib(const BaseString &n,const T &dv,const T &min_v,const T &max_v):PAttrib(n,dv) + PNumberAttrib(const String &n,const T &dv,const T &min_v,const T &max_v):PAttrib(n,dv) { min_value=min_v; max_value=max_v; @@ -63,7 +63,7 @@ namespace hgl virtual ~PNumberAttrib()=default; - const bool ParseFromString(const BaseString &str) override + const bool ParseFromString(const String &str) override { if(ToNumber(str,this->value)) { @@ -75,16 +75,57 @@ namespace hgl return(false); } - BaseString MakeToString() const override + String MakeToString() const override { - return BaseString(this->value); + return String::numberOf(this->value); } };//class PNumberAttrib:public PAttrib template using PIntAttrib =PNumberAttrib; template using PUintAttrib =PNumberAttrib; - template using PFloatAttrib =PNumberAttrib; - template using PDoubleAttrib=PNumberAttrib; + + template class PFloatNumberAttrib:public PAttrib + { + protected: + + T min_value,max_value; + + uint frac; + + public: + + PFloatNumberAttrib(const String &n,const T &dv,const T &min_v,const T &max_v,const int f):PAttrib(n,dv) + { + min_value=min_v; + max_value=max_v; + + frac=f; + } + + virtual ~PFloatNumberAttrib()=default; + + const bool ParseFromString(const String &str) override + { + if(ToNumber(str,this->value)) + { + if(this->value>=min_value&&this->value<=max_value) + return(true); + } + + this->value=this->default_value; + return(false); + } + + void SetFrac(uint f){frac=f;} + + String MakeToString() const override + { + return String::floatOf(this->value,frac); + } + };//class PFloatNumberAttrib:public PAttrib + + template using PFloatAttrib =PFloatNumberAttrib; + template using PDoubleAttrib=PFloatNumberAttrib; template class PBoolAttrib:public PAttrib { @@ -92,7 +133,7 @@ namespace hgl using PAttrib::PAttrib; - const bool ParseFromString(const BaseString &str) + const bool ParseFromString(const String &str) { if(str.ToBool(this->value)) return(true); @@ -101,36 +142,87 @@ namespace hgl return(false); } - BaseString MakeToString() const override + String MakeToString() const override { return(this->value?"true":"false"); } }; - template class PStringAttrib:public PAttrib> + template class PStringAttrib:public PAttrib> { public: - using PAttrib>::PAttrib; + using PAttrib>::PAttrib; - const bool ParseFromString(const BaseString &str) override + const bool ParseFromString(const String &str) override { this->value=str; return(true); } - BaseString MakeToString() const override + String MakeToString() const override { return this->value; } }; + template using PAttribMap=Map,PAttribBase *>; + + /** + * 向属性列表中写入一个属性 + */ + template static bool Add(PAttribMap &pa_map,const String &str) + { + String name; + C *value; + int off; + + if(str.Length()<2)return(false); + + if(((off=str.FindChar(C('\t')))==-1) + &&((off=str.FindChar(C(' '))) ==-1) + &&((off=str.FindChar(C('='))) ==-1) + &&((off=str.FindChar(C(':'))) ==-1)) + return(false); + + name.Strcpy(str,off); + off++; + + value=str.c_str()+off; + + while(true) + { + if(*value == C('\t') + ||*value == C('=') + ||*value == C(' ') + ||*value == C(':')) + { + value++; + continue; + } + + break; + } + + PAttribBase *attr=GetListObject(pa_map,name); + + if(attr) + attr->ParseFromString(value); + + return(true); + } + template class PAttribSet { - using PString=BaseString; + using PString=String; using PStringList=StringList; - Map,PAttribBase *> pa_map; + PAttribMap pa_map; + + public: + + operator PAttribMap &() {return pa_map;} + operator const PAttribMap &()const {return pa_map;} public: @@ -144,6 +236,25 @@ namespace hgl return obj; } + /** + * 创建一个浮点数属性 + * @param name 属性名称 + * @param dv 默认值 + * @param min_v 最小值 + * @param max_v 最大值 + * @param frac 小数点后位数 + * @return 属性对象 + */ + template + PFloatNumberAttrib *CreateFloatAttrib(const PString &name,const T &dv,const T &min_v,const T &max_v,const int frac) + { + PFloatNumberAttrib *obj=new PFloatNumberAttrib(name,dv,min_v,max_v,frac); + + pa_map.Add(name,obj); + + return obj; + } + PBoolAttrib *CreateBoolAttrib(const PString &name,const bool &dv) { PBoolAttrib *obj=new PBoolAttrib(name,dv); @@ -169,110 +280,22 @@ namespace hgl return pa_map.Add(name,attr); } - PAttribBase *Get(const PString &name){return GetObject(pa_map,name);} + bool Add(const PString &str) + { + return Add(pa_name,str); + } + + PAttribBase *Get(const PString &name){return GetListObject(pa_map,name);} void Delete(const PString &name){pa_map.DeleteByKey(name);} void Clear(){pa_map.Clear();} void ClearData(){pa_map.ClearData();} - void Enum(void (*enum_func)(const BaseString &key,PAttribBase *value)) + void Enum(void (*enum_func)(const String &key,PAttribBase *value)) { pa_map.Enum(enum_func); }; - - public: - - /** - * 保存到文本文件中 - */ - template - bool SaveToTextFile(const OSString &filename,const PString &gap_ch=PString("\t")) ///<保存列表到文件 - { - FileOutputStream fos; - EndianTextOutputStream tos(&fos); - - if(!fos.CreateTrunc(filename))return(false); - - tos.WriteBOM(); - - const int count=pa_map.GetCount(); - auto **pa_obj=pa_map.GetDataList(); - for(int i=0;ileft); - tos.WriteString(gap_ch); - tos.WriteString((*pa_obj)->right->MakeToString()); - tos.WriteLineEnd(); - - ++pa_obj; - } - - return(true); - } - - private: - - bool Add(const PString &str) ///<向列表中增加一项 - { - PString name; - C *value; - int off; - - if(str.Length()<2)return(false); - - if(((off=str.FindChar(C('\t')))==-1) - &&((off=str.FindChar(C(' '))) ==-1) - &&((off=str.FindChar(C('='))) ==-1) - &&((off=str.FindChar(C(':'))) ==-1)) - return(false); - - name.Strcpy(str,off); - off++; - - value=str.c_str()+off; - - while(true) - { - if(*value == C('\t') - ||*value == C('=') - ||*value == C(' ') - ||*value == C(':')) - { - value++; - continue; - } - - break; - } - - PAttribBase *attr=Get(name); - - if(attr) - attr->ParseFromString(value); - - return(true); - } - - public: - - /** - * 从文本文件中加载 - */ - virtual bool LoadFromTextFile(const OSString &filename) ///<从文件中加载列表 - { - PStringList sl; - - if(LoadStringListFromTextFile(sl,filename)<=0) - return(false); - - int n=sl.GetCount(); - - while(n--) - Add(sl[n]); - - return(true); - } };//template class PAttribSet using UTF8PAttribSet =PAttribSet; diff --git a/inc/hgl/util/plist/PList.h b/inc/hgl/util/plist/PList.h deleted file mode 100644 index 65ffccc..0000000 --- a/inc/hgl/util/plist/PList.h +++ /dev/null @@ -1,128 +0,0 @@ -#ifndef HGL_PLIST_INCLUDE -#define HGL_PLIST_INCLUDE - -#include -#include -#include -#include -namespace hgl -{ - using namespace io; - - /** - * 属性列表,类似INI的管理类 - */ - template class PList:public Map,String> ///属性列表 - { - public: - - using PString=String; - using PStringList=StringList; - using PMap=Map; - - protected: - - void ReadData(const PStringList &sl) - { - int n=sl.GetCount(); - - while(n--) - Add(sl[n]); - } - - public: - - virtual ~PList()=default; - - virtual bool Add(const PString &key) ///<向列表中增加一项 - { - PString name; - C *value; - int off; - - if(key.Length()<2)return(false); - - if(((off=key.FindChar(C('\t')))==-1) - &&((off=key.FindChar(C(' ' )))==-1) - &&((off=key.FindChar(C('=' )))==-1) - &&((off=key.FindChar(C(':' )))==-1)) - return(false); - - name.Strcpy(key,off); - off++; - - value=key.c_str()+off; - - while(true) - { - if(*value == C('\t') - ||*value == C('=') - ||*value == C(' ') - ||*value == C(':')) - value++; - else - { - PString str=value; - - PMap::Add(name,str); - - return(true); - } - } - }//bool PList::Add - - virtual bool Add(const PString &key,const PString &value) ///<向列表中增加一项 - { - return PMap::Add(key,value); - } - - /** - * 从文本文件中加载 - */ - virtual bool LoadFromTextFile(const OSString &filename,const CharSet &cs=OSCharSet) ///<从文件中加载列表 - { - PStringList sl; - - if(LoadStringListFromTextFile(sl,filename,cs)<=0) - return(false); - - ReadData(sl); - - return(true); - } - - /** - * 保存到文本文件中 - */ - template - bool SaveToTextFile(const OSString &filename,const C &gap_ch='\t') ///<保存列表到文件 - { - FileOutputStream fos; - EndianTextOutputStream tos(&fos); - - if(!fos.CreateTrunc(filename))return(false); - - PString gap_str=PString::charOf(gap_ch); - - int n=this->data_list.GetCount(); - - tos.WriteBOM(); - - while(n--) - { - PString f,s; - - if(GetBySerial(n,f,s)) - tos.WriteLine(f+gap_str+s); - } - - return(true); - } - };//class PList - - using UTF8PList =PList; - using UTF16PList=PList; - using WidePList =PList; - using OSPList =PList; -}//namespace hgl -#endif//HGL_PLIST_INCLUDE diff --git a/inc/hgl/util/plist/SavePAttrib.h b/inc/hgl/util/plist/SavePAttrib.h new file mode 100644 index 0000000..c4c3f0e --- /dev/null +++ b/inc/hgl/util/plist/SavePAttrib.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +namespace hgl +{ + using namespace io; + + /** + * 保存PList到文本文件中 + * @param filename 文件名 + * @param pa_map 属性列表 + * @param gap_ch 分隔符 + * @return 保存的属性数量 + */ + template static int SaveToTextFile(const OSString &filename,const PAttribMap &pa_map,const String &gap_ch=String("\t")) + { + FileOutputStream fos; + EndianTextOutputStream tos(&fos); + + if(!fos.CreateTrunc(filename))return(-1); + + tos.WriteBOM(); + + const int count=pa_map.GetCount(); + auto **pa_obj=pa_map.GetDataList(); + + for(int i=0;ikey); + tos.WriteString(gap_ch); + tos.WriteString((*pa_obj)->value->MakeToString()); + tos.WriteLineEnd(); + + ++pa_obj; + } + + return(count); + } +}//namespace hgl