#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);
}