读配置文件的实现

来源:互联网 发布:数据可视化利器 d3.js 编辑:程序博客网 时间:2024/04/28 03:52

#define SAS3_CONFIGFILE "/mnt/heidun/config/SAS3_Database.conf"

GetStrKeyValue(mysqlusername,"LUserName","RemoteMysql",SAS3_CONFIGFILE);//读配置文件

GetStrKeyValue(mysqlpassword,"LPassWord","RemoteMysql",SAS3_CONFIGFILE);

GetStrKeyValue(mysqldatabaseip,"DataBaseIP","RemoteMysql",SAS3_CONFIGFILE);
GetStrKeyValue(mysqldbname,"mysqlDBName","RemoteMysql",SAS3_CONFIGFILE);

 

BOOL GetStrKeyValue(LPCTSTR strValue,LPCTSTR strKey,LPCTSTR strSection,LPCTSTR strFileName)
{
 FILE *fp;
 char buf[STD_BUF];
 char *pStart,*pEnd,*index;

 if((fp=fopen(strFileName,"r"))==NULL)
 {
  printf("error happen in open file:%s/n",strFileName);
  return FALSE;
 }

 while(fgets(buf,STD_BUF,fp)!=NULL)
 {
  index = buf;
  while( *(index) == 10 || *(index)== 13 || *(index)== ' ' || *(index)== 9)
   index++;
  if((*index != '#') && (*index != 0x0a) && (*index != ';') && (index != NULL))
  {
   pStart = strchr(index,'[');
   pEnd = strchr(index,']');
   if(pStart!=NULL && pEnd!=NULL)
   {
    char sSection[50];
    memset(sSection,0,50);
    memcpy(sSection,pStart+1,pEnd-pStart-1);
    if(strcmp(sSection,strSection)==0)
    { //����ҵ�����Ҫ��Section
     while(fgets(buf,STD_BUF,fp)!=NULL)
     {
      while( *(index) == 10 || *(index)== 13 || *(index)== ' ' || *(index)== 9)
       index++;
      if((*index != '#') && (*index != 0x0a) && (*index != ';') && (index != NULL))
      {
       pStart = strchr(index,'=');
       if(pStart!=NULL)
       {
        char sKey[50];
        memset(sKey,0,50);
        memcpy(sKey,index,pStart-index);
        if(strcmp(sKey,strKey)==0)
        { //����ҵ�����Ҫ��Key
         char sValue[255];
                                    ++pStart;
                                    if(*(pStart)=='/'' || *(pStart)=='/"')
                                    {
                                       char *pEnd1,*pEnd2;
                                       pStart++;
                                       pEnd1 = strchr(pStart,'/'');
                                       pEnd2 = strchr(pStart,'/"');
                                       if(pEnd1!=NULL) *pEnd1 = '/0';
                                       if(pEnd2!=NULL) *pEnd2 = '/0';
                                    }
         strcpy(sValue,pStart);
         strcpy((char *)strValue,sValue);
         fclose(fp);
         return TRUE;
        }
       }
      }
     }
     break;
    }
   }//end of if("["  "]');
  }
 }

原创粉丝点击