IniFile.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //***********************************************************************
  2. // (c) xqsoft,版权所有不得拷贝(2003)
  3. // IniFile.cpp
  4. // 功能:修改后的INI文件类,支持读写删除。
  5. // 记录到INI格式文件
  6. //***********************************************************************
  7. #include "stdafx.h"
  8. #include "IniFile.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. /////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. /////////////////////////////////////////////////////////////////////
  17. //default constructor
  18. CIniFile::CIniFile()
  19. {
  20. GetModuleFileName(NULL,m_szFilePath,5000/*MAX_PATH*/);
  21. int iLen = _tcslen(m_szFilePath);
  22. m_szFilePath[iLen-1]=_T('i');
  23. m_szFilePath[iLen-2]=_T('n');
  24. m_szFilePath[iLen-3]=_T('i');
  25. }
  26. //default destructor
  27. CIniFile::~CIniFile()
  28. {
  29. }
  30. //sets path of ini file to read and write from
  31. void CIniFile::SetPath(LPCTSTR szPath)
  32. {
  33. _stprintf(m_szFilePath,_T("%s"),szPath);
  34. }
  35. BOOL CIniFile::GetVarStr(LPCTSTR strSection,LPCTSTR strVarName,CString & strValue)
  36. {
  37. TCHAR strReturnValue[5000/*MAX_PATH*/];
  38. DWORD dr = GetPrivateProfileString(strSection,strVarName,NULL,strReturnValue,5000/*MAX_PATH*/,m_szFilePath);
  39. strValue = strReturnValue;
  40. if(dr == NULL)return FALSE;
  41. return TRUE;
  42. }
  43. BOOL CIniFile::GetVarStr(LPCTSTR strSection,LPCTSTR strVarName,LPTSTR strReturnValue)
  44. {
  45. DWORD dr = GetPrivateProfileString(strSection,strVarName,NULL,strReturnValue,5000/*MAX_PATH*/,m_szFilePath);
  46. if(dr == NULL)return FALSE;
  47. return TRUE;
  48. }
  49. BOOL CIniFile::GetVarInt(LPCTSTR strSection,LPCTSTR strVarName,int & iValue)
  50. {
  51. DWORD dr;
  52. TCHAR strReturnValue[5000/*MAX_PATH*/];
  53. dr = GetPrivateProfileString(strSection,strVarName,NULL,strReturnValue,5000/*MAX_PATH*/,m_szFilePath);
  54. #if _UNICODE
  55. iValue = _wtoi(strReturnValue);
  56. #else
  57. iValue = atoi(strReturnValue);
  58. #endif
  59. if(dr == NULL)return FALSE;
  60. return TRUE;
  61. }
  62. BOOL CIniFile::SetVarStr(LPCTSTR strSection,LPCTSTR strVarName,LPCTSTR strValue)
  63. {
  64. DWORD dr = WritePrivateProfileString(strSection,strVarName,strValue,m_szFilePath);
  65. if(dr == NULL)return FALSE;
  66. return TRUE;
  67. }
  68. BOOL CIniFile::SetVarInt(LPCTSTR strSection,LPCTSTR strVarName,const int iValue)
  69. {
  70. TCHAR strValue[5000/*MAX_PATH*/];
  71. _stprintf(strValue,_T("%d"),iValue);
  72. DWORD dr = WritePrivateProfileString(strSection,strVarName,strValue,m_szFilePath);
  73. if(dr == NULL)return FALSE;
  74. return TRUE;
  75. }