CProfile : 读写私有INI配置文件

前注: 这是自己平时写的一些小代码,未必有用及符合设计原则,若有不妥之处,还请大家指教。

说明:

虽然INI这种配置文件早已不被微软所推荐,但至少在VC6下用C++编写一些小程序时,用INI还是有其长处的:简单的INI要比简单的XML更加直观(当然在复杂情况则相反);Windows提供了专门的API来读写INI配置,只需一条语句即可。

不过在最近写的几个小程序中,也发现了私有配置文件读写API的一些问题:

  1. 要获取配置文件的全局路径,并在每次调用API传递;

  2. Windows没有提供写整形数据的API;

  3. 经常把GetPrivateProfileXXX写成GetPrifileXXX。

所以,自己封装了一个类,其实就是一个非常简单的封装,函数名简短了些,函数参数少了些。额,我常常干这事,为了代码的一些微不足道(很多别人应该是这么以为的)的美观或调用上的简化做一些极简单的封装。

要点如下:

  1. 提供了两个成员变量来保存配置文件路径和段名,使向外公布的读写接口不必带有这两个参数。至少对我的大多数小程序来说,这两个数据都是唯一的。

  2. 提供了一个默认配置文件路径:在当前模块所在目录下,与当前模块同名,以INI为后缀名。

  3. 提供了一个写INT形数据的接口,使用的方法是网上找的。先转换为字符串,再写。

  4. 提供的GetString直接返回CString对象,不用调用方去分配内存了。

头文件:

class CProfile  {public:    static DWORD GetDefaultProfile(LPTSTR lpBuffer, UINT uSize);    CProfile(); CProfile(LPCTSTR strFile);  virtual ~CProfile();public: BOOL GetStruct(LPCTSTR key, LPVOID lpStruct, UINT uSize, LPCTSTR section = NULL);   DWORD GetSectionNames(LPTSTR lpReturnedString, DWORD nSize);    DWORD GetSection(LPTSTR lpReturnedString, DWORD nSize, LPCTSTR section = NULL); CString GetString(LPCTSTR key, LPCTSTR strDefault = NULL, LPCTSTR section = NULL);  INT GetInt(LPCTSTR key, INT nDefault = 0, LPCTSTR section = NULL);      BOOL WriteStruct(LPCTSTR key, LPVOID lpStruct, UINT uSize, LPCTSTR section = NULL); BOOL WriteInt(LPCTSTR key, INT iValue, LPCTSTR section = NULL); BOOL WriteString(LPCTSTR key, LPCTSTR lpString, LPCTSTR section = NULL);    BOOL WriteSection(LPCTSTR lpString, LPCTSTR section = NULL);    void SetCurrentSection(LPCTSTR lpSection);  void SetCurrentFile(LPCTSTR lpFile);    LPCTSTR GetCurrentSection();    LPCTSTR GetCurrentFile();protected: TCHAR m_strSection[MAX_SECTIONNAME_SIZE];   TCHAR m_strFile[MAX_PATH];};

代码:

CProfile : 读写私有INI配置文件CProfile : 读写私有INI配置文件代码#defineCProfile_PrepareSection

if(NULL==section) section=m_strSection;

ASSERT(NULL
!=section)



//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////



CProfile::CProfile()

{

GetDefaultProfile(m_strFile, MAX_PATH);

memset(m_strSection,
0,50sizeof(TCHAR));

}



CProfile::CProfile(LPCTSTR lpFile)

{

SetCurrentFile(lpFile);

memset(m_strSection,
0,50
sizeof(TCHAR));

}



CProfile::
~CProfile()

{

}



DWORD CProfile::GetDefaultProfile(LPTSTR lpBuffer, UINT uSize)

{

DWORD iPathLen
=::GetModuleFileName(NULL, lpBuffer, uSize);



if(0==iPathLen)return0;



while('.'!=lpBuffer[--iPathLen]&&iPathLen>=0);



if(0==iPathLen)return0;



iPathLen
=min(uSize-5, iPathLen);



wsprintf(lpBuffer
+iPathLen,".ini");



returniPathLen+5;

}



INT CProfile::GetInt(LPCTSTR key, INT nDefault, LPCTSTR section)

{

CProfile_PrepareSection;



return::GetPrivateProfileInt(section, key, nDefault, m_strFile);

}



CString CProfile::GetString(LPCTSTR key, LPCTSTR lpDefault, LPCTSTR section)

{

staticTCHAR Buffer[MAX_PROFILESTRING_SIZE];



memset(Buffer,
0, MAX_PROFILESTRING_SIZEsizeof(TCHAR));



CProfile_PrepareSection;



::GetPrivateProfileString(section, key, lpDefault, Buffer, MAX_PROFILESTRING_SIZE, m_strFile);



returnCString(Buffer);

}



DWORD CProfile::GetSection(LPTSTR lpReturnedString, DWORD nSize, LPCTSTR section)

{

CProfile_PrepareSection;



return::GetPrivateProfileSection(section, lpReturnedString, nSize, m_strFile);

}



DWORD CProfile::GetSectionNames(LPTSTR lpReturnedString, DWORD nSize)

{

return::GetPrivateProfileSectionNames(lpReturnedString, nSize, m_strFile);

}



BOOL CProfile::GetStruct(LPCTSTR key, LPVOID lpStruct, UINT uSize, LPCTSTR section)

{

CProfile_PrepareSection;



return::GetPrivateProfileStruct(section, key, lpStruct, uSize, m_strFile);

}



BOOL CProfile::WriteSection(LPCTSTR lpString, LPCTSTR section)

{

CProfile_PrepareSection;



return::WritePrivateProfileSection(section, lpString, m_strFile);

}



BOOL CProfile::WriteString(LPCTSTR key, LPCTSTR lpString, LPCTSTR section)

{

CProfile_PrepareSection;



return::WritePrivateProfileString(section, key, lpString, m_strFile);

}



BOOL CProfile::WriteInt(LPCTSTR key, INT iValue, LPCTSTR section)

{

CProfile_PrepareSection;



TCHAR BUFFER[
8];



memset(BUFFER,
0,4
sizeof(TCHAR));



wsprintf(BUFFER,
"%i", iValue);



return::WritePrivateProfileString(section, key, BUFFER, m_strFile);

}



BOOL CProfile::WriteStruct(LPCTSTR key, LPVOID lpStruct, UINT uSize, LPCTSTR section)

{

CProfile_PrepareSection;



return::WritePrivateProfileStruct(section, key, lpStruct, uSize, m_strFile);

}



voidCProfile::SetCurrentFile(LPCTSTR lpFile)

{

StrCpyN(m_strFile, lpFile, MAX_PATH);

}



voidCProfile::SetCurrentSection(LPCTSTR lpSection)

{

StrCpyN(m_strSection, lpSection, MAX_SECTIONNAME_SIZE);

}



LPCTSTR CProfile::GetCurrentSection()

{

returnm_strSection;

}

LPCTSTR CProfile::GetCurrentFile()

{

returnm_strFile;

}



原文链接: https://www.cnblogs.com/yedaoq/archive/2010/09/01/1815149.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/14589

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午2:16
下一篇 2023年2月7日 下午2:17

相关推荐