123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // IniFile.cpp: implementation of the CIniReader class.
- #include "stdafx.h"
- #include "IniReader.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- // 用于返回指定段名的键值;
- CString CIniReader::GetKeyValueText(CString strKey,CString strSection)
- {
- char ac_Result[255];
- // Get the info from the .ini file
- m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
- "",ac_Result, 255, (LPCTSTR)m_strFileName);
- CString strResult(ac_Result);
- return strResult;
- }
- // 用于返回指定段名的键值;
- int CIniReader::GetKeyValueInt(CString strKey,CString strSection)
- {
- char ac_Result[255];
- // Get the info from the .ini file
- m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
- "",ac_Result, 255, (LPCTSTR)m_strFileName);
- int nRet = atoi(ac_Result);
- return nRet;
- }
- // 用于添加或设置指定段名的键值;
- long CIniReader::SetKey(CString strValue, CString strKey, CString strSection)
- {
- m_lRetValue = WritePrivateProfileString (strSection, strKey,
- strValue, m_strFileName);
- return m_lRetValue;
- }
- // 用于判断一个段名是否存在;
- BOOL CIniReader::SectionExists(CString strSection)
- {
- char ac_Result[100];
- CString csAux;
- // Get the info from the .ini file
- m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
- "",ac_Result, 90, (LPCTSTR)m_strFileName);
- // Return if we could retrieve any info from that section
- return (m_lRetValue > 0);
- }
- // 用于返回ini文件中的所有段名;
- CStringList* CIniReader::GetSectionNames() //returns collection of section names
- {
- char ac_Result[2000];
- m_sectionList->RemoveAll();
-
- m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
-
- CString strSectionName;
- for(int i=0; i<m_lRetValue; i++)
- {
- if(ac_Result[i] != '\0') {
- strSectionName = strSectionName + ac_Result[i];
- } else {
- if(strSectionName != "") {
- m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
- }
- strSectionName = "";
- }
- }
- return m_sectionList;
- }
- // 用于返回ini文件所有段名的数据内容 ;
- CStringList* CIniReader::GetSectionData(CString strSection)
- {
- char ac_Result[2000]; //change size depending on needs
- m_sectionDataList->RemoveAll();
- m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
- CString strSectionData;
- for(int i=0; i<m_lRetValue; i++)
- {
- if(ac_Result[i] != '\0') {
- strSectionData = strSectionData + ac_Result[i];
- } else {
- if(strSectionData != "") {
- m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
- }
- strSectionData = "";
- }
- }
- return m_sectionDataList;
- }
- // 设置Ini文件名;
- void CIniReader::SetINIFileName(CString strINIFile)
- {
- m_strFileName = strINIFile;
- }
- int CIniReader::GetSecCount()
- {
- return m_sectionList->GetSize();
- }
- int CIniReader::GetKeyCount(/*CString strSec*/)
- {
- return m_sectionDataList->GetSize();
- }
|