C++封装ini操作

转自:http://blog.csdn.net/lewutian/article/details/6787048#

C++封装ini操作

// IniFile.h   
#ifndef __INIFILE_H__   
#define __INIFILE_H__   
  
class CIniFile  
{  
public:  
    CIniFile();  
    CIniFile(LPCTSTR szFileName);  
    virtual ~CIniFile();  
      
public:  
    // Attributes      
    void SetFileName(LPCTSTR szFileName);  
      
public:  
    // Operations   
    BOOL SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue);  
    BOOL SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue);  
  
    DWORD GetProfileSectionNames(CStringArray& strArray); // 返回section数量   
  
    int GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);  
    DWORD GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue);  
  
    BOOL DeleteSection(LPCTSTR lpszSectionName);  
    BOOL DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);  
      
private:  
    CString  m_szFileName; // .//Config.ini, 如果该文件不存在,则exe第一次试图Write时将创建该文件   
  
    UINT m_unMaxSection; // 最多支持的section数(256)   
    UINT m_unSectionNameMaxSize; // section名称长度,这里设为32(Null-terminated)   
  
    void Init();  
};  
  
#endif   
  
// IniFile.cpp   
#include "IniFile.h"   
  
void CIniFile::Init()  
{  
    m_unMaxSection = 512;  
    m_unSectionNameMaxSize = 33; // 32位UID串   
}  
  
CIniFile::CIniFile()  
{  
    Init();  
}  
  
CIniFile::CIniFile(LPCTSTR szFileName)  
{  
    // (1) 绝对路径,需检验路径是否存在   
    // (2) 以"./"开头,则需检验后续路径是否存在   
    // (3) 以"../"开头,则涉及相对路径的解析   
      
    Init();  
  
    // 相对路径   
    m_szFileName.Format(".//%s", szFileName);  
}  
  
CIniFile::~CIniFile()    
{  
      
}  
  
void CIniFile::SetFileName(LPCTSTR szFileName)  
{  
    m_szFileName.Format(".//%s", szFileName);  
}  
  
DWORD CIniFile::GetProfileSectionNames(CStringArray &strArray)  
{  
    int nAllSectionNamesMaxSize = m_unMaxSection*m_unSectionNameMaxSize+1;  
    char *pszSectionNames = new char[nAllSectionNamesMaxSize];  
    DWORD dwCopied = 0;  
    dwCopied = ::GetPrivateProfileSectionNames(pszSectionNames, nAllSectionNamesMaxSize, m_szFileName);  
      
    strArray.RemoveAll();  
  
    char *pSection = pszSectionNames;  
    do   
    {  
        CString szSection(pSection);  
        if (szSection.GetLength() < 1)  
        {  
            delete[] pszSectionNames;  
            return dwCopied;  
        }  
        strArray.Add(szSection);  
  
        pSection = pSection + szSection.GetLength() + 1; // next section name   
    } while (pSection && pSection<pszSectionNames+nAllSectionNamesMaxSize);  
  
    delete[] pszSectionNames;  
    return dwCopied;  
}  
  
DWORD CIniFile::GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue)  
{  
    DWORD dwCopied = 0;  
    dwCopied = ::GetPrivateProfileString(lpszSectionName, lpszKeyName, "",   
        szKeyValue.GetBuffer(MAX_PATH), MAX_PATH, m_szFileName);  
    szKeyValue.ReleaseBuffer();  
  
    return dwCopied;  
}  
  
int CIniFile::GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)  
{  
    int nKeyValue = ::GetPrivateProfileInt(lpszSectionName, lpszKeyName, 0, m_szFileName);  
      
    return nKeyValue;  
}  
  
BOOL CIniFile::SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue)  
{  
    return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, lpszKeyValue, m_szFileName);  
}  
  
BOOL CIniFile::SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue)  
{  
    CString szKeyValue;  
    szKeyValue.Format("%d", nKeyValue);  
  
    return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, szKeyValue, m_szFileName);  
}  
  
BOOL CIniFile::DeleteSection(LPCTSTR lpszSectionName)  
{  
    return ::WritePrivateProfileSection(lpszSectionName, NULL, m_szFileName);  
}  
  
BOOL CIniFile::DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)  
{  
    return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, NULL, m_szFileName);  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值