[资料] 最近用D做了个小项目,如果需要的话我可以部分开源,期望更多的人一起来学习D改进D。

robertluolinbo 2012-08-13
enum Encoding{
ISO8859_1,
GB2312,
GBK,
BIG5,
UTF_8,
UTF16_LE,
UTF16_BE,
UTF32_LE,
UTF32_BE
}

struct Cfg{
bool parent;
bool tel;
bool sms;
bool other;
}

public class ConfigFileUtil{
private string fileName;

public this(string iniFile,bool current=true){
if(current){
char[] path;
path.length=1024;
path[]=0;
GetModuleFileNameA(null,cast(char*)path,1024);
int ls=strlen(cast(char*)path);
int ps=path.lastIndexOf(r"\");
char[] rpath=path[0..ps];
string cw = cast(string)rpath;
this.fileName=format("%s%s%s",cw,endsWith(cw,r"\")?"":r"\",iniFile);
}
else
this.fileName=iniFile;
}

public bool save(string secName,string key,string value){
return cast(bool)WritePrivateProfileStringA(toMBSz(secName),toMBSz(key),toMBSz(value),toMBSz(fileName));
}

public string read(string secName,string key,string defaultValue=""){
string rt=null;
char[] tmp;
tmp.length=1024;
int rl= GetPrivateProfileStringA(toMBSz(secName),toMBSz(key),toMBSz(defaultValue),cast(char*)tmp,tmp.length,toMBSz(fileName));
if(rl>0){
tmp.length=rl+1;
rt=fromMBSz(cast(immutable char*)tmp.dup);
}
delete tmp;
return rt;
}

public bool readBool(string secName,string key){
string str=read(secName,key,"0");
return str !is null && str=="1";
}

public int readInt(string secName,string key,int defaultvalue){
string str=read(secName,key,format("%d",defaultvalue));
int rt=defaultvalue;
if(str !is null && str != "" && isNumeric(str)){
try{
rt=to!int(str);
}catch(Exception e){
rt=defaultvalue;
}
}
return rt;
}
}
hqs7636 2012-08-14
祝贺!!!

学习可以,改进就算了,那得神仙级的天才
Global site tag (gtag.js) - Google Analytics