Read/Write ini file

来源:互联网 发布:linux系统 编辑:程序博客网 时间:2024/05/16 01:14

// INIMANGER.h: interface for the CINIMANGER class. // //////////////////////////////////////////////////////////////////////  #if !defined(AFX_INIMANGER_H__E566489D_1FCE_48EA_9D2D_FA36B2EE246E__INCLUDED_) #define AFX_INIMANGER_H__E566489D_1FCE_48EA_9D2D_FA36B2EE246E__INCLUDED_  #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000  #defineMAX_CHARS1024  typedef struct tgaINIKEY { CString Section; CString Value; CString Key; }INIKEY,*LPINIKEY;  class CINIMANGER   { public: int EnumAllValue(CString* strValue,int nMaxNum);//枚举所有键值 int EnumKey(CString strSection,CString* strKey,int nMaxNum);//枚举指定小节的键 BOOL EnumSection(CString* strSection,int nMaxNum);//枚举所有小节 CString GetProfileName();//取得当前Ini文件名 int GetSectionKeyNum(CString strSection);//取得指定小节的键数目 int GetTotalKeyNum();//取得所有键数 int GetSectionNum();//取得所有小节数目 void SetKey(CString strSection,CString strKey,int iValue);//设置指定键的值 void SetKey(CString strSection,CString strKey,CString strValue);//同上 int GetKey(CString strSection,CString strKey,int iDefault);//得到指定键的值 CString GetKey(CString strSection,CString strKey,CString strDefault);//同上 BOOL Save();//将改动写回到Ini文件 BOOL Release();//释放Ini文件 CINIMANGER(); virtual ~CINIMANGER(); BOOL LoadProfileFile(CString strFileName);//载入Ini文件 private: DWORD* dwKeyNum;//每Section的Key项目数 DWORD dwSectionNum;//Ini文件的Section总的项目数 DWORD dwTotalKeyNum;//总的Key项目数 LPINIKEY* lpIniKey;//单个Ini文件的Key和Value CString* strSection;//Section列表 CString** strKey;//Key列表 CString strProfileName;//当前的Ini文件名  };  #endif // !defined(AFX_INIMANGER_H__E566489D_1FCE_48EA_9D2D_FA36B2EE246E__INCLUDED_) 
 
// INIMANGER.cpp: implementation of the CINIMANGER class.    //    //////////////////////////////////////////////////////////////////////       #include "stdafx.h"    #include "INIMANGER.h"       #ifdef _DEBUG    #undef THIS_FILE    static char THIS_FILE[]=__FILE__;   #define new DEBUG_NEW    #endif       //////////////////////////////////////////////////////////////////////    // Construction/Destruction    //////////////////////////////////////////////////////////////////////       CINIMANGER::CINIMANGER()   {       strSection=NULL;       dwKeyNum=NULL;       strKey=NULL;       dwSectionNum=0;       dwTotalKeyNum=0;       strProfileName="";   }      CINIMANGER::~CINIMANGER()   {       Release();   }      BOOL CINIMANGER::LoadProfileFile(CString strFileName)   {       char lpReturnValue[MAX_CHARS];       char lpKeyValue[MAX_CHARS];       DWORD i,j;       if(strProfileName!="") Release();       ::RtlZeroMemory(lpReturnValue,MAX_CHARS);       //枚举Section        ::GetPrivateProfileString(NULL,NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);       if(::lstrcmp(lpReturnValue,"")==0) return FALSE;       for(i=0;i<MAX_CHARS;i++)       {           if(lpReturnValue[i]==0)           {               if(dwSectionNum>=MAX_CHARS/2) return FALSE;               dwSectionNum++;               if(lpReturnValue[i+1]==0) break;               else continue;           }       }       strSection=new CString[dwSectionNum];       if(strSection==NULL) return FALSE;       j=0;       for(i=0;i<MAX_CHARS;i++)       {           if(lpReturnValue[i]!=0) strSection[j]+=lpReturnValue[i];           else if(lpReturnValue[i+1]!=0) j++;           else break;       }       ::RtlZeroMemory(lpReturnValue,MAX_CHARS);          //枚举所有Section的Key并取得其Value        dwKeyNum=new DWORD[dwSectionNum];       if(dwKeyNum==NULL) return FALSE;       for(i=0;i<dwSectionNum;i++) dwKeyNum[i]=0;       for(i=0;i<dwSectionNum;i++)       {           GetPrivateProfileString(strSection[i],NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);           for(int j=0;j<MAX_CHARS;j++)           {               //if(lpReturnValue[j]!=0) strKey[i][dwKeyNum[i]]+=lpReturnValue[j];                if(lpReturnValue[j]==0)               {                   if(dwKeyNum[i]>=MAX_CHARS/2) return FALSE;                   dwKeyNum[i]++;                   /*//取值                  ::RtlZeroMemory(lpKeyValue,MAX_CHARS);                  GetPrivateProfileString(strSection[i],strKey[i][dwKeyNum[i]-1],NULL,lpKeyValue,MAX_CHARS,strFileName);                  lpIniKey[dwTotalKeyNum]=new INIKEY;                  lpIniKey[dwTotalKeyNum]->Section=strSection[i];                  lpIniKey[dwTotalKeyNum]->Key=strKey[i][dwKeyNum[i]-1];                  lpIniKey[dwTotalKeyNum]->Value.Format("%s",lpKeyValue);*/                   if(dwTotalKeyNum>=MAX_CHARS) return FALSE;                   dwTotalKeyNum++;                   if(lpReturnValue[j+1]==0) break;                   else continue;               }           }       }       strKey=new CString*[dwSectionNum];       if(strKey==NULL) return FALSE;       for(i=0;i<dwSectionNum;i++)           strKey[i]=new CString[dwKeyNum[i]];       //取所有strKey        int k=0;       int m=0;       for(i=0;i<dwSectionNum;i++)       {           GetPrivateProfileString(strSection[i],NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);           k=0;           for(j=0;j<MAX_CHARS;j++)           {               if(lpReturnValue[j]!=0) strKey[i][k]+=lpReturnValue[j];               else if(lpReturnValue[j+1]==0) break;               else k++;           }       }       lpIniKey=new LPINIKEY[dwTotalKeyNum];       if(lpIniKey==NULL) return FALSE;       k=0;       for(i=0;i<dwSectionNum;i++)       {           for(j=0;j<dwKeyNum[i];j++)           {               ::RtlZeroMemory(lpKeyValue,MAX_CHARS);               GetPrivateProfileString(strSection[i],strKey[i][j],NULL,lpKeyValue,MAX_CHARS,strFileName);               lpIniKey[k]=new INIKEY;               lpIniKey[k]->Section=strSection[i];               lpIniKey[k]->Key=strKey[i][j];               lpIniKey[k]->Value.Format("%s",lpKeyValue);               k++;           }       }       strProfileName.Format("%s",strFileName);       return TRUE;   }      BOOL CINIMANGER::Release()   {       if(strProfileName=="") return FALSE;       if(strSection!=NULL)       {           delete [] strSection;           strSection=NULL;       }       if(dwKeyNum!=NULL)       {           delete [] dwKeyNum;           dwKeyNum=NULL;       }       if(strKey!=NULL)       {           for(DWORD i=0;i<dwSectionNum;i++)           {               if(strKey[i]!=NULL)               {                   delete [] strKey[i];                   strKey[i]=NULL;               }           }           delete [] strKey;       }       if(lpIniKey!=NULL)       {           for(DWORD i=0;i<dwTotalKeyNum;i++)           {               delete lpIniKey[i];               lpIniKey[i]=NULL;           }           delete [] lpIniKey;           lpIniKey=NULL;       }       dwSectionNum=0;       dwTotalKeyNum=0;       strProfileName="";       return TRUE;   }      BOOL CINIMANGER::Save()   {       if(strProfileName=="") return FALSE;       //检查INI文件        HANDLE hFile=CreateFile(strProfileName.GetBuffer(strProfileName.GetLength()),GENERIC_WRITE|GENERIC_READ,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);       CloseHandle(hFile);       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           WritePrivateProfileString(lpIniKey[i]->Section.GetBuffer(MAX_PATH),\                                       lpIniKey[i]->Key.GetBuffer(MAX_PATH),\                                       lpIniKey[i]->Value.GetBuffer(MAX_PATH),\                                       strProfileName.GetBuffer(MAX_PATH));       }       return TRUE;   }      CString CINIMANGER::GetKey(CString strSection,CString strKey,CString strDefault)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)               return lpIniKey[i]->Value;       }       return strDefault;   }      int CINIMANGER::GetKey(CString strSection,CString strKey,int iDefault)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               int iReturn=atoi(lpIniKey[i]->Value.GetBuffer(MAX_PATH));               return iReturn;           }       }       return iDefault;   }      void CINIMANGER::SetKey(CString strSection,CString strKey,CString strValue)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               lpIniKey[i]->Value=strValue;               return;           }       }       lpIniKey[dwTotalKeyNum]=new INIKEY;       lpIniKey[dwTotalKeyNum]->Section=strSection;       lpIniKey[dwTotalKeyNum]->Key=strKey;       lpIniKey[dwTotalKeyNum]->Value=strValue;       dwTotalKeyNum++;       for(DWORD j=0;j<dwSectionNum;j++)       {           if(strSection==this->strSection[j])           {               dwKeyNum[j]++;               return;           }       }       dwSectionNum++;       dwKeyNum[dwSectionNum-1]++;       return;   }      void CINIMANGER::SetKey(CString strSection,CString strKey,int iValue)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               lpIniKey[i]->Value.Format("%d",iValue);               return;           }       }       lpIniKey[dwTotalKeyNum]=new INIKEY;       lpIniKey[dwTotalKeyNum]->Section=strSection;       lpIniKey[dwTotalKeyNum]->Key=strKey;       lpIniKey[dwTotalKeyNum]->Value=iValue;       dwTotalKeyNum++;       for(DWORD j=0;j<dwSectionNum;j++)       {           if(strSection==this->strSection[j])           {               dwKeyNum[j]++;               return;           }       }       dwSectionNum++;       dwKeyNum[dwSectionNum-1]++;       return;   }      int CINIMANGER::GetSectionNum()   {       return dwSectionNum;   }      int CINIMANGER::GetTotalKeyNum()   {       return dwTotalKeyNum;   }      int CINIMANGER::GetSectionKeyNum(CString strSection)   {       for(DWORD i=0;i<dwSectionNum;i++)       {           if(this->strSection[i]==strSection) return dwKeyNum[i];       }       return 0;   }      CString CINIMANGER::GetProfileName()   {       return strProfileName;   }      int CINIMANGER::EnumSection(CString* strSection,int nMaxNum)   {       if((int)dwSectionNum>nMaxNum) return -1;       for(DWORD i=0;i<dwSectionNum;i++)           strSection[i]=this->strSection[i];       return dwSectionNum;   }      int CINIMANGER::EnumKey(CString strSection,CString* strKey,int nMaxNum)   {       for(DWORD i=0;i<dwSectionNum;i++)       {           if(this->strSection[i]==strSection)           {               if((int)dwKeyNum[i]>nMaxNum) return FALSE;               for(DWORD j=0;j<dwKeyNum[i];j++)                   strKey[j]=this->strKey[i][j];               return dwKeyNum[i];           }       }       return -1;   }      int CINIMANGER::EnumAllValue(CString* strValue,int nMaxNum)   {       if((int)dwTotalKeyNum>nMaxNum) return -1;       for(DWORD i=0;i<dwTotalKeyNum;i++)           strValue[i]=lpIniKey[i]->Value;       return dwTotalKeyNum;   }   


// INIMANGER.cpp: implementation of the CINIMANGER class.    //    //////////////////////////////////////////////////////////////////////       #include "stdafx.h"    #include "INIMANGER.h"       #ifdef _DEBUG    #undef THIS_FILE    static char THIS_FILE[]=__FILE__;   #define new DEBUG_NEW    #endif       //////////////////////////////////////////////////////////////////////    // Construction/Destruction    //////////////////////////////////////////////////////////////////////       CINIMANGER::CINIMANGER()   {       strSection=NULL;       dwKeyNum=NULL;       strKey=NULL;       dwSectionNum=0;       dwTotalKeyNum=0;       strProfileName="";   }      CINIMANGER::~CINIMANGER()   {       Release();   }      BOOL CINIMANGER::LoadProfileFile(CString strFileName)   {       char lpReturnValue[MAX_CHARS];       char lpKeyValue[MAX_CHARS];       DWORD i,j;       if(strProfileName!="") Release();       ::RtlZeroMemory(lpReturnValue,MAX_CHARS);       //枚举Section        ::GetPrivateProfileString(NULL,NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);       if(::lstrcmp(lpReturnValue,"")==0) return FALSE;       for(i=0;i<MAX_CHARS;i++)       {           if(lpReturnValue[i]==0)           {               if(dwSectionNum>=MAX_CHARS/2) return FALSE;               dwSectionNum++;               if(lpReturnValue[i+1]==0) break;               else continue;           }       }       strSection=new CString[dwSectionNum];       if(strSection==NULL) return FALSE;       j=0;       for(i=0;i<MAX_CHARS;i++)       {           if(lpReturnValue[i]!=0) strSection[j]+=lpReturnValue[i];           else if(lpReturnValue[i+1]!=0) j++;           else break;       }       ::RtlZeroMemory(lpReturnValue,MAX_CHARS);          //枚举所有Section的Key并取得其Value        dwKeyNum=new DWORD[dwSectionNum];       if(dwKeyNum==NULL) return FALSE;       for(i=0;i<dwSectionNum;i++) dwKeyNum[i]=0;       for(i=0;i<dwSectionNum;i++)       {           GetPrivateProfileString(strSection[i],NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);           for(int j=0;j<MAX_CHARS;j++)           {               //if(lpReturnValue[j]!=0) strKey[i][dwKeyNum[i]]+=lpReturnValue[j];                if(lpReturnValue[j]==0)               {                   if(dwKeyNum[i]>=MAX_CHARS/2) return FALSE;                   dwKeyNum[i]++;                   /*//取值                  ::RtlZeroMemory(lpKeyValue,MAX_CHARS);                  GetPrivateProfileString(strSection[i],strKey[i][dwKeyNum[i]-1],NULL,lpKeyValue,MAX_CHARS,strFileName);                  lpIniKey[dwTotalKeyNum]=new INIKEY;                  lpIniKey[dwTotalKeyNum]->Section=strSection[i];                  lpIniKey[dwTotalKeyNum]->Key=strKey[i][dwKeyNum[i]-1];                  lpIniKey[dwTotalKeyNum]->Value.Format("%s",lpKeyValue);*/                   if(dwTotalKeyNum>=MAX_CHARS) return FALSE;                   dwTotalKeyNum++;                   if(lpReturnValue[j+1]==0) break;                   else continue;               }           }       }       strKey=new CString*[dwSectionNum];       if(strKey==NULL) return FALSE;       for(i=0;i<dwSectionNum;i++)           strKey[i]=new CString[dwKeyNum[i]];       //取所有strKey        int k=0;       int m=0;       for(i=0;i<dwSectionNum;i++)       {           GetPrivateProfileString(strSection[i],NULL,NULL,lpReturnValue,MAX_CHARS,strFileName);           k=0;           for(j=0;j<MAX_CHARS;j++)           {               if(lpReturnValue[j]!=0) strKey[i][k]+=lpReturnValue[j];               else if(lpReturnValue[j+1]==0) break;               else k++;           }       }       lpIniKey=new LPINIKEY[dwTotalKeyNum];       if(lpIniKey==NULL) return FALSE;       k=0;       for(i=0;i<dwSectionNum;i++)       {           for(j=0;j<dwKeyNum[i];j++)           {               ::RtlZeroMemory(lpKeyValue,MAX_CHARS);               GetPrivateProfileString(strSection[i],strKey[i][j],NULL,lpKeyValue,MAX_CHARS,strFileName);               lpIniKey[k]=new INIKEY;               lpIniKey[k]->Section=strSection[i];               lpIniKey[k]->Key=strKey[i][j];               lpIniKey[k]->Value.Format("%s",lpKeyValue);               k++;           }       }       strProfileName.Format("%s",strFileName);       return TRUE;   }      BOOL CINIMANGER::Release()   {       if(strProfileName=="") return FALSE;       if(strSection!=NULL)       {           delete [] strSection;           strSection=NULL;       }       if(dwKeyNum!=NULL)       {           delete [] dwKeyNum;           dwKeyNum=NULL;       }       if(strKey!=NULL)       {           for(DWORD i=0;i<dwSectionNum;i++)           {               if(strKey[i]!=NULL)               {                   delete [] strKey[i];                   strKey[i]=NULL;               }           }           delete [] strKey;       }       if(lpIniKey!=NULL)       {           for(DWORD i=0;i<dwTotalKeyNum;i++)           {               delete lpIniKey[i];               lpIniKey[i]=NULL;           }           delete [] lpIniKey;           lpIniKey=NULL;       }       dwSectionNum=0;       dwTotalKeyNum=0;       strProfileName="";       return TRUE;   }      BOOL CINIMANGER::Save()   {       if(strProfileName=="") return FALSE;       //检查INI文件        HANDLE hFile=CreateFile(strProfileName.GetBuffer(strProfileName.GetLength()),GENERIC_WRITE|GENERIC_READ,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);       CloseHandle(hFile);       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           WritePrivateProfileString(lpIniKey[i]->Section.GetBuffer(MAX_PATH),\                                       lpIniKey[i]->Key.GetBuffer(MAX_PATH),\                                       lpIniKey[i]->Value.GetBuffer(MAX_PATH),\                                       strProfileName.GetBuffer(MAX_PATH));       }       return TRUE;   }      CString CINIMANGER::GetKey(CString strSection,CString strKey,CString strDefault)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)               return lpIniKey[i]->Value;       }       return strDefault;   }      int CINIMANGER::GetKey(CString strSection,CString strKey,int iDefault)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               int iReturn=atoi(lpIniKey[i]->Value.GetBuffer(MAX_PATH));               return iReturn;           }       }       return iDefault;   }      void CINIMANGER::SetKey(CString strSection,CString strKey,CString strValue)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               lpIniKey[i]->Value=strValue;               return;           }       }       lpIniKey[dwTotalKeyNum]=new INIKEY;       lpIniKey[dwTotalKeyNum]->Section=strSection;       lpIniKey[dwTotalKeyNum]->Key=strKey;       lpIniKey[dwTotalKeyNum]->Value=strValue;       dwTotalKeyNum++;       for(DWORD j=0;j<dwSectionNum;j++)       {           if(strSection==this->strSection[j])           {               dwKeyNum[j]++;               return;           }       }       dwSectionNum++;       dwKeyNum[dwSectionNum-1]++;       return;   }      void CINIMANGER::SetKey(CString strSection,CString strKey,int iValue)   {       for(DWORD i=0;i<dwTotalKeyNum;i++)       {           if(strSection==lpIniKey[i]->Section&&strKey==lpIniKey[i]->Key)           {               lpIniKey[i]->Value.Format("%d",iValue);               return;           }       }       lpIniKey[dwTotalKeyNum]=new INIKEY;       lpIniKey[dwTotalKeyNum]->Section=strSection;       lpIniKey[dwTotalKeyNum]->Key=strKey;       lpIniKey[dwTotalKeyNum]->Value=iValue;       dwTotalKeyNum++;       for(DWORD j=0;j<dwSectionNum;j++)       {           if(strSection==this->strSection[j])           {               dwKeyNum[j]++;               return;           }       }       dwSectionNum++;       dwKeyNum[dwSectionNum-1]++;       return;   }      int CINIMANGER::GetSectionNum()   {       return dwSectionNum;   }      int CINIMANGER::GetTotalKeyNum()   {       return dwTotalKeyNum;   }      int CINIMANGER::GetSectionKeyNum(CString strSection)   {       for(DWORD i=0;i<dwSectionNum;i++)       {           if(this->strSection[i]==strSection) return dwKeyNum[i];       }       return 0;   }      CString CINIMANGER::GetProfileName()   {       return strProfileName;   }      int CINIMANGER::EnumSection(CString* strSection,int nMaxNum)   {       if((int)dwSectionNum>nMaxNum) return -1;       for(DWORD i=0;i<dwSectionNum;i++)           strSection[i]=this->strSection[i];       return dwSectionNum;   }      int CINIMANGER::EnumKey(CString strSection,CString* strKey,int nMaxNum)   {       for(DWORD i=0;i<dwSectionNum;i++)       {           if(this->strSection[i]==strSection)           {               if((int)dwKeyNum[i]>nMaxNum) return FALSE;               for(DWORD j=0;j<dwKeyNum[i];j++)                   strKey[j]=this->strKey[i][j];               return dwKeyNum[i];           }       }       return -1;   }      int CINIMANGER::EnumAllValue(CString* strValue,int nMaxNum)   {       if((int)dwTotalKeyNum>nMaxNum) return -1;       for(DWORD i=0;i<dwTotalKeyNum;i++)           strValue[i]=lpIniKey[i]->Value;       return dwTotalKeyNum;   }   


 

 

 

原创粉丝点击