1
0

config.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "main.h"
  2. #include "./config.h"
  3. #include "./wasabi.h"
  4. #include <shlwapi.h>
  5. #include <strsafe.h>
  6. #define CONFIG_SUFFIX L"Plugins\\webDev"
  7. static LPCSTR Config_GetPath()
  8. {
  9. static LPSTR configPath = NULL;
  10. if (NULL == configPath)
  11. {
  12. LPCWSTR p = (NULL != WASABI_API_APP) ? WASABI_API_APP->path_getUserSettingsPath() : NULL;
  13. if (NULL != p)
  14. {
  15. WCHAR szBuffer[MAX_PATH * 2] = {0};
  16. if (0 != PathCombine(szBuffer, p, CONFIG_SUFFIX))
  17. {
  18. OMUTILITY->EnsurePathExist(szBuffer);
  19. PathAppend(szBuffer, L"config.ini");
  20. configPath = Plugin_WideCharToMultiByte(CP_UTF8, 0, szBuffer, -1, NULL, NULL);
  21. }
  22. }
  23. }
  24. return configPath;
  25. }
  26. DWORD Config_ReadStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize)
  27. {
  28. return GetPrivateProfileStringA(lpSectionName, lpKeyName, lpDefault, lpReturnedString, nSize, Config_GetPath());
  29. }
  30. UINT Config_ReadInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nDefault)
  31. {
  32. return GetPrivateProfileIntA(lpSectionName, lpKeyName, nDefault, Config_GetPath());
  33. }
  34. HRESULT Config_WriteStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpString)
  35. {
  36. LPCSTR configPath = Config_GetPath();
  37. if (NULL == configPath || '\0' == *configPath)
  38. return E_UNEXPECTED;
  39. if (0 != WritePrivateProfileStringA(lpSectionName, lpKeyName, lpString, configPath))
  40. return S_OK;
  41. DWORD errorCode = GetLastError();
  42. return HRESULT_FROM_WIN32(errorCode);
  43. }
  44. HRESULT Config_WriteInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nValue)
  45. {
  46. char szBuffer[32];
  47. HRESULT hr = StringCchPrintfA(szBuffer, ARRAYSIZE(szBuffer), "%d", nValue);
  48. if (FAILED(hr)) return hr;
  49. return Config_WriteStr(lpSectionName, lpKeyName, szBuffer);
  50. }
  51. HRESULT Config_WriteSection(LPCSTR lpSectionName, LPCSTR lpData)
  52. {
  53. LPCSTR configPath = Config_GetPath();
  54. if (NULL == configPath || '\0' == *configPath)
  55. return E_UNEXPECTED;
  56. if (0 == WritePrivateProfileSectionA(lpSectionName, lpData, configPath))
  57. return S_OK;
  58. DWORD errorCode = GetLastError();
  59. return HRESULT_FROM_WIN32(errorCode);
  60. }