config.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "main.h"
  2. #include "./config.h"
  3. #include <strsafe.h>
  4. #define CONFIG_SUFFIX L"Plugins\\ml"
  5. #define CONFIG_FILE L"ml_devices.ini"
  6. static const char *
  7. Config_GetPath(BOOL ensureExist)
  8. {
  9. static const char *configPath = NULL;
  10. if (NULL == configPath)
  11. {
  12. const wchar_t *userPath;
  13. wchar_t buffer[MAX_PATH * 2] = {0};
  14. if (NULL == WASABI_API_APP)
  15. return NULL;
  16. userPath = WASABI_API_APP->path_getUserSettingsPath();
  17. if (NULL == userPath)
  18. return NULL;
  19. if (0 != PathCombine(buffer, userPath, CONFIG_SUFFIX))
  20. {
  21. if ((FALSE == ensureExist || SUCCEEDED(Plugin_EnsurePathExist(buffer))) &&
  22. FALSE != PathAppend(buffer,CONFIG_FILE))
  23. {
  24. configPath = String_ToAnsi(CP_UTF8, 0, buffer, -1, NULL, NULL);
  25. }
  26. }
  27. }
  28. return configPath;
  29. }
  30. unsigned long
  31. Config_ReadString(const char *section, const char *key, const char *defaultValue, char *returnedString, unsigned long size)
  32. {
  33. return GetPrivateProfileStringA(section, key, defaultValue, returnedString, size, Config_GetPath(FALSE));
  34. }
  35. unsigned int
  36. Config_ReadInt(const char *section, const char *key, int defaultValue)
  37. {
  38. return GetPrivateProfileIntA(section, key, defaultValue, Config_GetPath(FALSE));
  39. }
  40. BOOL
  41. Config_ReadBool(const char *section, const char *key, BOOL defaultValue)
  42. {
  43. char buffer[32] = {0};
  44. int length = Config_ReadString(section, key, NULL, buffer, ARRAYSIZE(buffer));
  45. if (0 == length)
  46. return defaultValue;
  47. if (1 == length)
  48. {
  49. switch(*buffer)
  50. {
  51. case '0':
  52. case 'n':
  53. case 'f':
  54. return FALSE;
  55. case '1':
  56. case 'y':
  57. case 't':
  58. return TRUE;
  59. }
  60. }
  61. else
  62. {
  63. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "yes", -1, buffer, length) ||
  64. CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "true", -1, buffer, length))
  65. {
  66. return TRUE;
  67. }
  68. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "no", -1, buffer, length) ||
  69. CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "false", -1, buffer, length))
  70. {
  71. return FALSE;
  72. }
  73. }
  74. if (FALSE != StrToIntExA(buffer, STIF_SUPPORT_HEX, &length))
  75. return (0 != length);
  76. return defaultValue;
  77. }
  78. BOOL
  79. Config_WriteString(const char *section, const char *key, const char *value)
  80. {
  81. const char *configPath = Config_GetPath(TRUE);
  82. if (NULL == configPath || '\0' == *configPath)
  83. return FALSE;
  84. return (0 != WritePrivateProfileStringA(section, key, value, configPath));
  85. }
  86. BOOL
  87. Config_WriteInt(const char *section, const char *key, int value)
  88. {
  89. char buffer[32] = {0};
  90. if (FAILED(StringCchPrintfA(buffer, ARRAYSIZE(buffer), "%d", value)))
  91. return FALSE;
  92. return Config_WriteString(section, key, buffer);
  93. }
  94. BOOL
  95. Config_WriteBool(const char *section, const char *key, BOOL value)
  96. {
  97. return Config_WriteString(section, key, (FALSE != value) ? "yes" : "no");
  98. }