fixed PAttrib.h,created LoadPAttrib.h/SavePAttrib.h
This commit is contained in:
31
inc/hgl/util/plist/LoadPAttrib.h
Normal file
31
inc/hgl/util/plist/LoadPAttrib.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/plist/PAttrib.h>
|
||||
#include<hgl/io/LoadStringList.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
|
||||
/**
|
||||
* 从文本文件中加载PList
|
||||
* @param filename 文件名
|
||||
* @param pa_map 属性列表
|
||||
* @return 是否加载成功
|
||||
*/
|
||||
template<typename C> static int LoadFromTextFile(const OSString &filename,PAttribMap<C> &pa_map)
|
||||
{
|
||||
StringList<C> sl;
|
||||
|
||||
if(LoadStringListFromTextFile(sl,filename)<=0)
|
||||
return(false);
|
||||
|
||||
int n=sl.GetCount();
|
||||
|
||||
while(n--)
|
||||
Add(pa_map,sl[n]);
|
||||
|
||||
return(true);
|
||||
}
|
||||
}//namespace hgl
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
#include<hgl/type/StringList.h>
|
||||
#include<hgl/type/String.h>
|
||||
#include<hgl/io/LoadStringList.h>
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
@@ -17,21 +17,21 @@ namespace hgl
|
||||
PAttribBase()=default;
|
||||
virtual ~PAttribBase()=default;
|
||||
|
||||
virtual const bool ParseFromString(const BaseString<C> &str)=0;
|
||||
virtual BaseString<C> MakeToString()const=0;
|
||||
virtual const bool ParseFromString(const String<C> &str)=0;
|
||||
virtual String<C> MakeToString()const=0;
|
||||
};
|
||||
|
||||
template<typename C,typename T> class PAttrib:public PAttribBase<C>
|
||||
{
|
||||
protected:
|
||||
|
||||
BaseString<C> name;
|
||||
String<C> name;
|
||||
T value;
|
||||
T default_value;
|
||||
|
||||
public:
|
||||
|
||||
PAttrib(const BaseString<C> &n,const T &v)
|
||||
PAttrib(const String<C> &n,const T &v)
|
||||
{
|
||||
name=n;
|
||||
value=v;
|
||||
@@ -40,8 +40,8 @@ namespace hgl
|
||||
|
||||
virtual ~PAttrib()=default;
|
||||
|
||||
virtual const bool ParseFromString(const BaseString<C> &str)=0;
|
||||
virtual BaseString<C> MakeToString()const=0;
|
||||
virtual const bool ParseFromString(const String<C> &str)=0;
|
||||
virtual String<C> 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<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
|
||||
PNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
|
||||
{
|
||||
min_value=min_v;
|
||||
max_value=max_v;
|
||||
@@ -63,7 +63,7 @@ namespace hgl
|
||||
|
||||
virtual ~PNumberAttrib()=default;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str) override
|
||||
const bool ParseFromString(const String<C> &str) override
|
||||
{
|
||||
if(ToNumber(str,this->value))
|
||||
{
|
||||
@@ -75,16 +75,57 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return BaseString<C>(this->value);
|
||||
return String<C>::numberOf(this->value);
|
||||
}
|
||||
};//class PNumberAttrib:public PAttrib<C,uint>
|
||||
|
||||
template<typename C> using PIntAttrib =PNumberAttrib<C,int >;
|
||||
template<typename C> using PUintAttrib =PNumberAttrib<C,uint >;
|
||||
template<typename C> using PFloatAttrib =PNumberAttrib<C,float >;
|
||||
template<typename C> using PDoubleAttrib=PNumberAttrib<C,double >;
|
||||
|
||||
template<typename C,typename T> class PFloatNumberAttrib:public PAttrib<C,T>
|
||||
{
|
||||
protected:
|
||||
|
||||
T min_value,max_value;
|
||||
|
||||
uint frac;
|
||||
|
||||
public:
|
||||
|
||||
PFloatNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v,const int f):PAttrib<C,T>(n,dv)
|
||||
{
|
||||
min_value=min_v;
|
||||
max_value=max_v;
|
||||
|
||||
frac=f;
|
||||
}
|
||||
|
||||
virtual ~PFloatNumberAttrib()=default;
|
||||
|
||||
const bool ParseFromString(const String<C> &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<C> MakeToString() const override
|
||||
{
|
||||
return String<C>::floatOf(this->value,frac);
|
||||
}
|
||||
};//class PFloatNumberAttrib:public PAttrib<C,uint>
|
||||
|
||||
template<typename C> using PFloatAttrib =PFloatNumberAttrib<C,float >;
|
||||
template<typename C> using PDoubleAttrib=PFloatNumberAttrib<C,double >;
|
||||
|
||||
template<typename C> class PBoolAttrib:public PAttrib<C,bool>
|
||||
{
|
||||
@@ -92,7 +133,7 @@ namespace hgl
|
||||
|
||||
using PAttrib<C,bool>::PAttrib;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str)
|
||||
const bool ParseFromString(const String<C> &str)
|
||||
{
|
||||
if(str.ToBool(this->value))
|
||||
return(true);
|
||||
@@ -101,36 +142,87 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return(this->value?"true":"false");
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C> class PStringAttrib:public PAttrib<C,BaseString<C>>
|
||||
template<typename C> class PStringAttrib:public PAttrib<C,String<C>>
|
||||
{
|
||||
public:
|
||||
|
||||
using PAttrib<C,BaseString<C>>::PAttrib;
|
||||
using PAttrib<C,String<C>>::PAttrib;
|
||||
|
||||
const bool ParseFromString(const BaseString<C> &str) override
|
||||
const bool ParseFromString(const String<C> &str) override
|
||||
{
|
||||
this->value=str;
|
||||
return(true);
|
||||
}
|
||||
|
||||
BaseString<C> MakeToString() const override
|
||||
String<C> MakeToString() const override
|
||||
{
|
||||
return this->value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C> using PAttribMap=Map<String<C>,PAttribBase<C> *>;
|
||||
|
||||
/**
|
||||
* 向属性列表中写入一个属性
|
||||
*/
|
||||
template<typename C> static bool Add(PAttribMap<C> &pa_map,const String<C> &str)
|
||||
{
|
||||
String<C> 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<C> *attr=GetListObject(pa_map,name);
|
||||
|
||||
if(attr)
|
||||
attr->ParseFromString(value);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename C> class PAttribSet
|
||||
{
|
||||
using PString=BaseString<C>;
|
||||
using PString=String<C>;
|
||||
using PStringList=StringList<PString>;
|
||||
|
||||
Map<BaseString<C>,PAttribBase<C> *> pa_map;
|
||||
PAttribMap<C> pa_map;
|
||||
|
||||
public:
|
||||
|
||||
operator PAttribMap<C> &() {return pa_map;}
|
||||
operator const PAttribMap<C> &()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<typename T>
|
||||
PFloatNumberAttrib<C,T> *CreateFloatAttrib(const PString &name,const T &dv,const T &min_v,const T &max_v,const int frac)
|
||||
{
|
||||
PFloatNumberAttrib<C,T> *obj=new PFloatNumberAttrib<C,T>(name,dv,min_v,max_v,frac);
|
||||
|
||||
pa_map.Add(name,obj);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
PBoolAttrib<C> *CreateBoolAttrib(const PString &name,const bool &dv)
|
||||
{
|
||||
PBoolAttrib<C> *obj=new PBoolAttrib<C>(name,dv);
|
||||
@@ -169,110 +280,22 @@ namespace hgl
|
||||
return pa_map.Add(name,attr);
|
||||
}
|
||||
|
||||
PAttribBase<C> *Get(const PString &name){return GetObject(pa_map,name);}
|
||||
bool Add(const PString &str)
|
||||
{
|
||||
return Add(pa_name,str);
|
||||
}
|
||||
|
||||
PAttribBase<C> *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<C> &key,PAttribBase<C> *value))
|
||||
void Enum(void (*enum_func)(const String<C> &key,PAttribBase<C> *value))
|
||||
{
|
||||
pa_map.Enum(enum_func);
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 保存到文本文件中
|
||||
*/
|
||||
template<ByteOrderMask BOM>
|
||||
bool SaveToTextFile(const OSString &filename,const PString &gap_ch=PString("\t")) ///<保存列表到文件
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> 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;i<count;i++)
|
||||
{
|
||||
tos.WriteString((*pa_obj)->left);
|
||||
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<C> *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<typename C> class PAttribSet
|
||||
|
||||
using UTF8PAttribSet =PAttribSet<char>;
|
||||
|
@@ -1,128 +0,0 @@
|
||||
#ifndef HGL_PLIST_INCLUDE
|
||||
#define HGL_PLIST_INCLUDE
|
||||
|
||||
#include<hgl/type/Map.h>
|
||||
#include<hgl/type/LoadStringList.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
/**
|
||||
* 属性列表,类似INI的管理类
|
||||
*/
|
||||
template<typename C> class PList:public Map<String<C>,String<C>> ///属性列表
|
||||
{
|
||||
public:
|
||||
|
||||
using PString=String<C>;
|
||||
using PStringList=StringList<C>;
|
||||
using PMap=Map<PString,PString>;
|
||||
|
||||
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<ByteOrderMask BOM>
|
||||
bool SaveToTextFile(const OSString &filename,const C &gap_ch='\t') ///<保存列表到文件
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> 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<char >;
|
||||
using UTF16PList=PList<u16char>;
|
||||
using WidePList =PList<wchar_t>;
|
||||
using OSPList =PList<os_char>;
|
||||
}//namespace hgl
|
||||
#endif//HGL_PLIST_INCLUDE
|
42
inc/hgl/util/plist/SavePAttrib.h
Normal file
42
inc/hgl/util/plist/SavePAttrib.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/util/plist/PAttrib.h>
|
||||
#include<hgl/io/FileOutputStream.h>
|
||||
#include<hgl/io/TextOutputStream.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
using namespace io;
|
||||
|
||||
/**
|
||||
* 保存PList到文本文件中
|
||||
* @param filename 文件名
|
||||
* @param pa_map 属性列表
|
||||
* @param gap_ch 分隔符
|
||||
* @return 保存的属性数量
|
||||
*/
|
||||
template<typename C,ByteOrderMask BOM> static int SaveToTextFile(const OSString &filename,const PAttribMap<C> &pa_map,const String<C> &gap_ch=String<C>("\t"))
|
||||
{
|
||||
FileOutputStream fos;
|
||||
EndianTextOutputStream<BOM> 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;i<count;i++)
|
||||
{
|
||||
tos.WriteString((*pa_obj)->key);
|
||||
tos.WriteString(gap_ch);
|
||||
tos.WriteString((*pa_obj)->value->MakeToString());
|
||||
tos.WriteLineEnd();
|
||||
|
||||
++pa_obj;
|
||||
}
|
||||
|
||||
return(count);
|
||||
}
|
||||
}//namespace hgl
|
Reference in New Issue
Block a user