Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2653645
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2007-12-11 09:14:20


正巧我今天作了一个读写配置文件的类,可能有BUG,但是只要配置文件格式对的话,就没太大问题,没有内存泄漏。  

配置文件格式config.ini  

[雷达路径]  
RADARPATH=d:\best\;F:\ML45\SYSDATA\BEST\  
SAVEPATH=E:\RadarInfo.dat  
SAVEDBDSN=RadarInfo  
[导出周期]  
EXPORTPERIOD=1  

 

源代码  

class   CCfgData      
{  
public:  
 CCfgData();  
 virtual   ~CCfgData();  


 void   SetGroup(LPCTSTR   strGroup);  
 BOOL   SetData(LPCTSTR   strKey,LPCTSTR   strValue);  
 BOOL   GetStrData(LPCTSTR   strKey,CString   &strValue);  
 void   LoadCfgData(LPCTSTR   strFileName);  
 BOOL   SaveCfgData(LPCTSTR   strFileName);  
protected:  
 void   RemoveAll();  
 CMapStringToPtr   m_StrMapMap;  
 CString m_strGroup;  
};  


CCfgData::CCfgData()  
{  
 RemoveAll();  
 m_strGroup="设置";  
}  

CCfgData::~CCfgData()  
{  
 RemoveAll();  
}  


void   CCfgData::RemoveAll()  
{  
 POSITION   pos=m_StrMapMap.GetStartPosition();  
 while(pos)  
 {  
  CMapStringToString*   pStrMap;  
  CString   str;  
  m_StrMapMap.GetNextAssoc(pos,str,(void*&)pStrMap);  
  if(pStrMap!=NULL)  
  {  
   pStrMap->RemoveAll();  
   delete   pStrMap;  
  }  
 }  
 m_StrMapMap.RemoveAll();  

}  
void   CCfgData::LoadCfgData(LPCTSTR   strFileName)  
{  
 int   iReadLen=0;  
 CString   str[3];  
 int   iState=0;  
 unsigned   char   ch;  
 RemoveAll();  
 CFile   file;  
 file.Open(strFileName,   CFile::modeRead);  
 file.Seek(0,CFile::begin);  
 str[0]="";  
 str[1]="";  
 str[2]="";  
 CMapStringToString*   pStrMap=NULL;  
 do  
 {  
  iReadLen=file.Read(&ch,1);  
  if(iReadLen!=0)  
  {  
   if(ch>0x80)//中文  
   {  
    str[iState]+=ch;  
    iReadLen=file.Read(&ch,1);  
    if(iReadLen!=0)  
    {  
     str[iState]+=ch;  
    }  
   }else  
   {  
    switch(ch)  
    {  
    case   '[':  
     if(str[0]==""&&str[1]=="")  
     {  
      pStrMap=NULL;  
      iState=2;  
      str[2]="";  
     }else  
     {  
      str[iState]+=ch;  
     }  
     break;  
    case   ']':  
     if(iState==2&&str[2]!="")  
     {  
      iState=0;  
      pStrMap=new   CMapStringToString;  
      m_StrMapMap.SetAt(str[2],pStrMap);  
     }else  
     {  
      str[iState]+=ch;  
     }  
     break;  
    case   '=':  
     iState=1;  
     str[1]="";  
     break;  
    case   0x0d:  
    case   0x0a:  
     iState=0;  
     if(str[0]!=""&&str[1]!=""&&pStrMap!=NULL)  
     {  
      pStrMap->SetAt((LPCTSTR)str[0],(LPCTSTR)str[1]);  
     }  
     str[0]="";  
     str[1]="";  
     break;  
    default:  
     str[iState]+=ch;  
     break;  
    }  
   }  
  }  
 }while(iReadLen!=0);  
 file.Close();  

}  

void   CCfgData::SetGroup(LPCTSTR   strGroup)  
{  
 m_strGroup=strGroup;  
}  

BOOL   CCfgData::GetStrData(LPCTSTR   strKey,   CString   &strValue)  
{  
 CMapStringToString*   pStrMap=NULL;  
 if(m_StrMapMap.Lookup(m_strGroup,(void*&)pStrMap))  
 {  
  if(pStrMap->Lookup(strKey,strValue))  
   return   TRUE;  
  return   FALSE;  
 }  
 return   FALSE;  
}  

BOOL   CCfgData::SetData(LPCTSTR   strKey,   LPCTSTR   strValue)  
{  
 CMapStringToString*   pStrMap=NULL;  
 if(m_StrMapMap.Lookup(m_strGroup,(void*&)pStrMap))  
 {  
  pStrMap->SetAt(strKey,strValue);  
  return   TRUE;  
 }else  
 {  
  pStrMap=new   CMapStringToString;  
  m_StrMapMap.SetAt(m_strGroup,pStrMap);  
  pStrMap->SetAt(strKey,strValue);  
  return   FALSE;  
 }  

}  

BOOL   CCfgData::SaveCfgData(LPCTSTR   strFileName)  
{  
 CFile   file;  
 file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);  
 POSITION   pos=m_StrMapMap.GetStartPosition();  
 char   ch[6]="[]\r\n=";  
 CString   str[3];  
 while(pos)  
 {  
  CMapStringToString*   pStrMap;  
  m_StrMapMap.GetNextAssoc(pos,str[2],(void*&)pStrMap);  
  if(pStrMap!=NULL)  
  {  
   file.Write(&ch[0],1);  
   file.Write((LPSTR)(LPCTSTR)str[2],str[2].GetLength());  
   file.Write(&ch[1],1);  
   file.Write(&ch[2],2);  
   POSITION   pos1=pStrMap->GetStartPosition();  
   while(pos1)  
   {  
    pStrMap->GetNextAssoc(pos1,str[0],str[1]);  
    file.Write((LPSTR)(LPCTSTR)str[0],str[0].GetLength());  
    file.Write(&ch[4],1);  
    file.Write((LPSTR)(LPCTSTR)str[1],str[1].GetLength());  
    file.Write(&ch[2],2);  
   }  
  }  
 }  
 file.Close();  
 return   TRUE;  
}  

阅读(1611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~