1
0

config.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "main.h"
  2. #include "./config.h"
  3. #include <shlwapi.h>
  4. #include <strsafe.h>
  5. #define DEFAULT_SECTION TEXT("gen_ml_config")
  6. #define STRCOMP_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
  7. C_Config::~C_Config()
  8. {
  9. if (m_inifile) CoTaskMemFree(m_inifile);
  10. }
  11. C_Config::C_Config(LPCTSTR pszIniFile)
  12. {
  13. if (S_OK != SHStrDup(pszIniFile, &m_inifile))
  14. m_inifile = NULL;
  15. }
  16. void C_Config::WriteIntEx(LPCTSTR pszSection, LPCTSTR pszKey, int nValue)
  17. {
  18. TCHAR buf[32] = {0};
  19. StringCchPrintf(buf, ARRAYSIZE(buf), TEXT("%d"), nValue);
  20. WriteStringEx(pszSection, pszKey, buf);
  21. }
  22. int C_Config::ReadIntEx(LPCTSTR pszSection, LPCTSTR pszKey, int nDefault)
  23. {
  24. return GetPrivateProfileInt(pszSection, pszKey, nDefault, m_inifile);
  25. }
  26. void C_Config::WriteStringEx(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszValue)
  27. {
  28. WritePrivateProfileString(pszSection, pszKey, pszValue, m_inifile);
  29. }
  30. LPTSTR C_Config::ReadCbStringEx(LPTSTR pszBuffer, INT cbBuffer, LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault)
  31. {
  32. return ReadCchStringEx(pszBuffer, cbBuffer/sizeof(TCHAR), pszSection, pszKey, pszDefault);
  33. }
  34. LPTSTR C_Config::ReadCchStringEx(LPTSTR pszBuffer, INT cchBuffer, LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault)
  35. {
  36. const static TCHAR foobuf[] = TEXT("___________gen_ml_lameness___________");
  37. pszBuffer[0] = TEXT('\0');
  38. GetStringEx(pszSection, pszKey, foobuf, pszBuffer, cchBuffer);
  39. pszBuffer[cchBuffer -1] = TEXT('\0');
  40. if (CSTR_EQUAL == CompareString(STRCOMP_INVARIANT, 0, foobuf, -1, pszBuffer, -1))
  41. {
  42. if (S_OK != StringCchCopyEx(pszBuffer, cchBuffer, pszDefault, NULL, NULL, STRSAFE_IGNORE_NULLS))
  43. return NULL;
  44. }
  45. return pszBuffer;
  46. }
  47. LPTSTR C_Config::ReadCchQuotedString(LPTSTR pszBuffer, INT cchBuffer, LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault)
  48. {
  49. LPTSTR p = ReadCchStringEx(pszBuffer, cchBuffer, pszSection, pszKey, pszDefault);
  50. if (p) PathUnquoteSpaces(pszBuffer);
  51. return p;
  52. }
  53. LPTSTR C_Config::ReadCbQuotedString(LPTSTR pszBuffer, INT cbBuffer, LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault)
  54. {
  55. return ReadCchQuotedString(pszBuffer, cbBuffer/sizeof(TCHAR), pszSection, pszKey, pszDefault);
  56. }
  57. void C_Config::WriteQuotedString(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszValue)
  58. {
  59. if (!pszValue) return;
  60. INT cch = lstrlen(pszValue);
  61. if (cch < MAX_PATH)
  62. {
  63. TCHAR szBuffer[MAX_PATH] = {0};
  64. if (S_OK == StringCchCopy(szBuffer, ARRAYSIZE(szBuffer), pszValue))
  65. {
  66. PathQuoteSpaces(szBuffer);
  67. WriteStringEx(pszSection, pszKey, szBuffer);
  68. return;
  69. }
  70. }
  71. else
  72. {
  73. LPTSTR pszBuffer = (LPTSTR)calloc((cch + 4), sizeof(TCHAR));
  74. if (pszBuffer)
  75. {
  76. if (S_OK == StringCchCopy(pszBuffer, cch + 4, pszValue))
  77. {
  78. PathQuoteSpaces(pszBuffer);
  79. WriteStringEx(pszSection, pszKey, pszBuffer);
  80. free(pszBuffer);
  81. return;
  82. }
  83. free(pszBuffer);
  84. }
  85. }
  86. WriteStringEx(pszSection, pszKey, pszValue);
  87. }
  88. DWORD C_Config::GetStringEx(LPCTSTR pszSection, LPCTSTR pszKey, LPCTSTR pszDefault, LPTSTR pszReturnedValue, DWORD nSize)
  89. {
  90. return GetPrivateProfileString(pszSection, pszKey, pszDefault, pszReturnedValue, nSize, m_inifile);
  91. }
  92. void C_Config::WriteInt(LPCTSTR pszKey, int nValue)
  93. {
  94. WriteIntEx(DEFAULT_SECTION, pszKey, nValue);
  95. }
  96. int C_Config::ReadInt(LPCTSTR pszKey, int nDefault)
  97. {
  98. return ReadIntEx(DEFAULT_SECTION, pszKey, nDefault);
  99. }
  100. void C_Config::WriteString(LPCTSTR pszKey, LPCTSTR pszValue)
  101. {
  102. WriteStringEx(DEFAULT_SECTION, pszKey, pszValue);
  103. }
  104. LPCTSTR C_Config::ReadString(LPCTSTR pszKey, LPCTSTR pszDefault)
  105. {
  106. static TCHAR szBuffer[4096];
  107. return ReadCchStringEx(szBuffer, ARRAYSIZE(szBuffer), DEFAULT_SECTION, pszKey, pszDefault);
  108. }
  109. DWORD C_Config::GetString(LPCTSTR pszKey, LPCTSTR pszDefault, LPTSTR pszReturnedValue, DWORD nSize)
  110. {
  111. return GetStringEx(DEFAULT_SECTION, pszKey, pszDefault, pszReturnedValue, nSize);
  112. }