一个手工读写INI文件的类(二)[4]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 bluebohe 的 blog

 {
  lvalue=atoi((lpctstr)str);
  return true;
 }
 return false;
}
//修改或者添加一条当前组中的数据
/*
参数: lpctstr strkey 要修改的数据的键值
lpctstr strvalue要修改的数据的内容
返回值:
备注: 如果当前组在配置数据中存在,则修改或者添加该组对应键值的数据,如果当前组灾配置数据中不存在,则先创建该组
*/

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 true;
 }
 
}
//将配置数据保存到文件
/*
参数: lpctstr strfilename 保存文件的名称
返回值:成功返回true 失败返回false
*/

bool ccfgdata::savecfgdata(lpctstr strfilename)
{
 cfile file;
 if(!file.open(strfilename,cfile::modecreate|cfile::modewrite))
  return false;
 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;
}
//将配置数据保存到字符串
/*
参数: cstring* pstr 要保存的字符串指针
返回值:成功返回true 失败返回false
备注: 程序流程和上面基本相同,不同的保存进入字符串中
*/

bool ccfgdata::savetostr(cstring *pstr)
{
 if(pstr==null)return false;
 *pstr="";
 position pos=m_strmapmap.getstartposition();
 cstring str[4];
 while(pos)
 {
  cmapstringtostring* pstrmap;
  m_strmapmap.getnextassoc(pos,str[2],(void*&)pstrmap);
  if(pstrmap!=null)
  {
   str[3]=*pstr;
   pstr->format("%s[%s]\r\n",str[3],str[2]);
   position pos1=pstrmap->getstartposition();
   while(pos1)
   {
    pstrmap->getnextassoc(pos1,str[0],str[1]);
    str[3]=*pstr;
    pstr->format("%s%s=%s\r\n",str[3],str[0],str[1]);
    
   }
  }
 }
 return true;
 

本文关键:ini CMap CMapStringToPtr
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top