OLD | NEW |
1 #include <memory> | 1 #include <memory> |
2 #include <stdexcept> | 2 #include <stdexcept> |
3 | 3 |
4 #include <Windows.h> | 4 #include <Windows.h> |
5 #include <ShlObj.h> | 5 #include <ShlObj.h> |
6 | 6 |
7 #include "Utils.h" | 7 #include "Utils.h" |
8 | 8 |
9 namespace | 9 namespace |
10 { | 10 { |
11 std::wstring appDataPath; | 11 std::wstring appDataPath; |
12 | 12 |
13 bool IsWindowsVistaOrLater() | 13 bool IsWindowsVistaOrLater() |
14 { | 14 { |
15 OSVERSIONINFOEX osvi; | 15 OSVERSIONINFOEX osvi; |
16 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); | 16 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); |
17 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); | 17 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
18 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); | 18 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); |
19 return osvi.dwMajorVersion >= 6; | 19 return osvi.dwMajorVersion >= 6; |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
| 23 std::string ToUtf8String(std::wstring str) |
| 24 { |
| 25 size_t length = str.size(); |
| 26 if (length == 0) |
| 27 return std::string(); |
| 28 |
| 29 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length,
0, 0, 0, 0); |
| 30 if (utf8StringLength == 0) |
| 31 throw std::runtime_error("Failed to determine the required buffer size"); |
| 32 |
| 33 std::string utf8String(utf8StringLength, '\0'); |
| 34 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Strin
gLength, 0, 0); |
| 35 return utf8String; |
| 36 } |
| 37 |
23 std::wstring GetAppDataPath() | 38 std::wstring GetAppDataPath() |
24 { | 39 { |
25 if (appDataPath.empty()) | 40 if (appDataPath.empty()) |
26 { | 41 { |
27 if (IsWindowsVistaOrLater()) | 42 if (IsWindowsVistaOrLater()) |
28 { | 43 { |
29 WCHAR* pathBuffer; | 44 WCHAR* pathBuffer; |
30 if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, 0, &pathBuffe
r))) | 45 if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, 0, &pathBuffe
r))) |
31 throw std::runtime_error("Unable to find app data directory"); | 46 throw std::runtime_error("Unable to find app data directory"); |
32 appDataPath.assign(pathBuffer); | 47 appDataPath.assign(pathBuffer); |
33 CoTaskMemFree(pathBuffer); | 48 CoTaskMemFree(pathBuffer); |
34 } | 49 } |
35 else | 50 else |
36 { | 51 { |
37 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); | 52 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); |
38 if (!SHGetSpecialFolderPath(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, true
)) | 53 if (!SHGetSpecialFolderPath(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, true
)) |
39 throw std::runtime_error("Unable to find app data directory"); | 54 throw std::runtime_error("Unable to find app data directory"); |
40 appDataPath.assign(pathBuffer.get()); | 55 appDataPath.assign(pathBuffer.get()); |
41 } | 56 } |
42 appDataPath += L"\\Adblock Plus for IE"; | 57 appDataPath += L"\\Adblock Plus for IE"; |
43 | 58 |
44 // Ignore errors here, this isn't a critical operation | 59 // Ignore errors here, this isn't a critical operation |
45 ::CreateDirectoryW(appDataPath.c_str(), NULL); | 60 ::CreateDirectoryW(appDataPath.c_str(), NULL); |
46 } | 61 } |
47 return appDataPath; | 62 return appDataPath; |
48 } | 63 } |
OLD | NEW |