-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathWeaselUtility.cpp
51 lines (46 loc) · 1.46 KB
/
WeaselUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "stdafx.h"
#include <filesystem>
#include <string>
#include <WeaselUtility.h>
fs::path WeaselUserDataPath() {
WCHAR _path[MAX_PATH] = {0};
const WCHAR KEY[] = L"Software\\Rime\\Weasel";
HKEY hKey;
LSTATUS ret = RegOpenKey(HKEY_CURRENT_USER, KEY, &hKey);
if (ret == ERROR_SUCCESS) {
DWORD len = sizeof(_path);
DWORD type = 0;
DWORD data = 0;
ret =
RegQueryValueEx(hKey, L"RimeUserDir", NULL, &type, (LPBYTE)_path, &len);
RegCloseKey(hKey);
if (ret == ERROR_SUCCESS && type == REG_SZ && _path[0]) {
return fs::path(_path);
}
}
// default location
ExpandEnvironmentStringsW(L"%AppData%\\Rime", _path, _countof(_path));
return fs::path(_path);
}
fs::path WeaselSharedDataPath() {
wchar_t _path[MAX_PATH] = {0};
GetModuleFileNameW(NULL, _path, _countof(_path));
return fs::path(_path).remove_filename().append("data");
}
std::string GetCustomResource(const char* name, const char* type) {
const HINSTANCE module = 0; // main executable
HRSRC hRes = FindResourceA(module, name, type);
if (hRes) {
HGLOBAL hData = LoadResource(module, hRes);
if (hData) {
const char* data = (const char*)::LockResource(hData);
size_t size = ::SizeofResource(module, hRes);
if (data && size) {
if (data[size - 1] == '\0') // null-terminated string
size--;
return std::string(data, size);
}
}
}
return std::string();
}