c 服务器配置文件,单实例模式的一个C++读取配置文件的类CConfig2Map - 高性能服务器开发;C++......

#include"config2map.h"CConfig2Map*CConfig2Map::_instance=NULL;

CConfig2Map::CConfig2Map(constchar*pszinifilename)throw(constchar*)

{

#ifdef _DEBUG

CLog log;#endifFILE*pfile=fopen(pszinifilename,"r");char*pszerrmsg;if(!pfile)

{

pszerrmsg=newchar[1024];

memset(pszerrmsg,0x00,1024);

SNPRINTF(pszerrmsg,1023,"failed to open file [%s]",pszinifilename);throw(pszerrmsg);

}

#ifdef _DEBUGintireadline=0;#endifcharszBuf[1024]={0};intbuflen=0;char*key;char*keybegin,*keyend;intbkeyendfound=0;

PNODELIST pnodelist=NULL;while(fgets(szBuf,1023, pfile))

{

buflen=strlen(szBuf);if(buflen<3)continue;if(buflen>=1)

{if(szBuf[buflen-1]==0x0a||szBuf[buflen-1]==0x0d)

szBuf[buflen-1]=0x00;

}if(buflen>=2)

{if(szBuf[buflen-2]==0x0a||szBuf[buflen-2]==0x0d)

szBuf[buflen-2]=0x00;

}

#ifdef _DEBUG

log.log(LOG_DBG,"%d=%s",buflen,szBuf);#endifchar*p=szBuf;if(*p=='#'||*p=='\n'||*p=='\0')continue;if(*p=='[')

{

#ifdef _DEBUG

printf("found [\n");#endifkeybegin=p+1;

bkeyendfound=0;while(*p)

{if(*p==']')

{

#ifdef _DEBUG

printf("found ]\n");#endifbkeyendfound=1;

keyend=p-1;break;

}

p++;

}if(!bkeyendfound)

{

#ifdef _DEBUG

printf("key [] not match\n");#endifthrow("key [] not match\n");

}if(keybegin>keyend)

{

#ifdef _DEBUG

printf("invalid key[\"\"]\n");#endifthrow("invalid key[""]");

}

key=newchar[keyend-keybegin+1+1];

memset(key,0x00,keyend-keybegin+1+1);

strncpy(key,keybegin,keyend-keybegin+1);//key[keyend-keybegin+1+1]='\0';//2010-10-13:这个错误,找了一天啊key[keyend-keybegin+1]='\0';

#ifdef _DEBUG

printf("new key=%s\n", key);#endifpnodelist=newCNodeList();

(*this)[key]=pnodelist;continue;

}while(*p&&*p++!='=');if(p==(szBuf+strlen(szBuf)))continue;charname[256]={0};charvalue[1024]={0};

strncpy(name, szBuf, p-szBuf-1);

strncpy(value, p, szBuf+strlen(szBuf)-p);if(pnodelist==NULL)

{

#ifdef _DEBUG

printf("key not found,%s=%s discard\n",name,value);#endif}else{

PNODE pnode=newNODE(name,value);

pnodelist->push_back(pnode);

}//proc(pvoid,key,value);}

fclose(pfile);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值