IniReader.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // IniFile.cpp: implementation of the CIniReader class.
  2. #include "stdafx.h"
  3. #include "IniReader.h"
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char THIS_FILE[]=__FILE__;
  7. #define new DEBUG_NEW
  8. #endif
  9. // 用于返回指定段名的键值;
  10. CString CIniReader::GetKeyValueText(CString strKey,CString strSection)
  11. {
  12. char ac_Result[255];
  13. // Get the info from the .ini file
  14. m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
  15. "",ac_Result, 255, (LPCTSTR)m_strFileName);
  16. CString strResult(ac_Result);
  17. return strResult;
  18. }
  19. // 用于返回指定段名的键值;
  20. int CIniReader::GetKeyValueInt(CString strKey,CString strSection)
  21. {
  22. char ac_Result[255];
  23. // Get the info from the .ini file
  24. m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
  25. "",ac_Result, 255, (LPCTSTR)m_strFileName);
  26. int nRet = atoi(ac_Result);
  27. return nRet;
  28. }
  29. // 用于添加或设置指定段名的键值;
  30. long CIniReader::SetKey(CString strValue, CString strKey, CString strSection)
  31. {
  32. m_lRetValue = WritePrivateProfileString (strSection, strKey,
  33. strValue, m_strFileName);
  34. return m_lRetValue;
  35. }
  36. // 用于判断一个段名是否存在;
  37. BOOL CIniReader::SectionExists(CString strSection)
  38. {
  39. char ac_Result[100];
  40. CString csAux;
  41. // Get the info from the .ini file
  42. m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
  43. "",ac_Result, 90, (LPCTSTR)m_strFileName);
  44. // Return if we could retrieve any info from that section
  45. return (m_lRetValue > 0);
  46. }
  47. // 用于返回ini文件中的所有段名;
  48. CStringList* CIniReader::GetSectionNames() //returns collection of section names
  49. {
  50. char ac_Result[2000];
  51. m_sectionList->RemoveAll();
  52. m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
  53. CString strSectionName;
  54. for(int i=0; i<m_lRetValue; i++)
  55. {
  56. if(ac_Result[i] != '\0') {
  57. strSectionName = strSectionName + ac_Result[i];
  58. } else {
  59. if(strSectionName != "") {
  60. m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
  61. }
  62. strSectionName = "";
  63. }
  64. }
  65. return m_sectionList;
  66. }
  67. // 用于返回ini文件所有段名的数据内容 ;
  68. CStringList* CIniReader::GetSectionData(CString strSection)
  69. {
  70. char ac_Result[2000]; //change size depending on needs
  71. m_sectionDataList->RemoveAll();
  72. m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
  73. CString strSectionData;
  74. for(int i=0; i<m_lRetValue; i++)
  75. {
  76. if(ac_Result[i] != '\0') {
  77. strSectionData = strSectionData + ac_Result[i];
  78. } else {
  79. if(strSectionData != "") {
  80. m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
  81. }
  82. strSectionData = "";
  83. }
  84. }
  85. return m_sectionDataList;
  86. }
  87. // 设置Ini文件名;
  88. void CIniReader::SetINIFileName(CString strINIFile)
  89. {
  90. m_strFileName = strINIFile;
  91. }
  92. int CIniReader::GetSecCount()
  93. {
  94. return m_sectionList->GetSize();
  95. }
  96. int CIniReader::GetKeyCount(/*CString strSec*/)
  97. {
  98. return m_sectionDataList->GetSize();
  99. }