OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 84 } |
85 | 85 |
86 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& values) | 86 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& values) |
87 { | 87 { |
88 std::vector<std::wstring> result; | 88 std::vector<std::wstring> result; |
89 result.reserve(values.size()); | 89 result.reserve(values.size()); |
90 transform(values.begin(), values.end(), back_inserter(result), ToUtf16String); | 90 transform(values.begin(), values.end(), back_inserter(result), ToUtf16String); |
91 return result; | 91 return result; |
92 } | 92 } |
93 | 93 |
| 94 namespace |
| 95 { |
| 96 std::wstring GetModulePath(HINSTANCE hInstance) |
| 97 { |
| 98 std::vector<WCHAR> path(MAX_PATH); |
| 99 int length = GetModuleFileNameW(hInstance, &path[0], static_cast<DWORD>(path
.size())); |
| 100 |
| 101 while (length == path.size()) |
| 102 { |
| 103 // Buffer too small, double buffer size |
| 104 path.resize(path.size() * 2); |
| 105 length = GetModuleFileNameW(hInstance, &path[0], static_cast<DWORD>(path.s
ize())); |
| 106 } |
| 107 |
| 108 try |
| 109 { |
| 110 if (length == 0) |
| 111 throw std::runtime_error("Failed determining module path"); |
| 112 |
| 113 std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.re
nd(), L'\\'); |
| 114 if (it == path.rend()) |
| 115 throw std::runtime_error("Unexpected plugin path, no backslash found"); |
| 116 |
| 117 return std::wstring(path.begin(), it.base()); |
| 118 } |
| 119 catch (const std::exception&) |
| 120 { |
| 121 return std::wstring(); |
| 122 } |
| 123 } |
| 124 } |
| 125 |
94 std::wstring GetDllDir() | 126 std::wstring GetDllDir() |
95 { | 127 { |
96 std::vector<WCHAR> path(MAX_PATH); | 128 return GetModulePath((HINSTANCE)&__ImageBase); |
97 int length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast
<DWORD>(path.size())); | 129 } |
98 | 130 |
99 while (length == path.size()) | 131 std::wstring GetExeDir() |
100 { | 132 { |
101 // Buffer too small, double buffer size | 133 return GetModulePath(nullptr); |
102 path.resize(path.size() * 2); | |
103 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast<D
WORD>(path.size())); | |
104 } | |
105 | |
106 try | |
107 { | |
108 if (length == 0) | |
109 throw std::runtime_error("Failed determining module path"); | |
110 | |
111 std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.rend
(), L'\\'); | |
112 if (it == path.rend()) | |
113 throw std::runtime_error("Unexpected plugin path, no backslash found"); | |
114 | |
115 return std::wstring(path.begin(), it.base()); | |
116 } | |
117 catch (const std::exception&) | |
118 { | |
119 return std::wstring(); | |
120 } | |
121 } | 134 } |
122 | 135 |
123 std::wstring GetAppDataPath() | 136 std::wstring GetAppDataPath() |
124 { | 137 { |
125 if (appDataPath.empty()) | 138 if (appDataPath.empty()) |
126 { | 139 { |
127 if (IsWindowsVistaOrLater()) | 140 if (IsWindowsVistaOrLater()) |
128 { | 141 { |
129 WCHAR* pathBuffer; | 142 WCHAR* pathBuffer; |
130 if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, 0, &pathBuffe
r))) | 143 if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, 0, &pathBuffe
r))) |
131 throw std::runtime_error("Unable to find app data directory"); | 144 throw std::runtime_error("Unable to find app data directory"); |
132 appDataPath.assign(pathBuffer); | 145 appDataPath.assign(pathBuffer); |
133 CoTaskMemFree(pathBuffer); | 146 CoTaskMemFree(pathBuffer); |
134 } | 147 } |
135 else | 148 else |
136 { | 149 { |
137 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); | 150 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); |
138 if (!SHGetSpecialFolderPathW(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, tru
e)) | 151 if (!SHGetSpecialFolderPathW(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, tru
e)) |
139 throw std::runtime_error("Unable to find app data directory"); | 152 throw std::runtime_error("Unable to find app data directory"); |
140 appDataPath.assign(pathBuffer.get()); | 153 appDataPath.assign(pathBuffer.get()); |
141 } | 154 } |
142 appDataPath += L"\\Adblock Plus for IE"; | 155 appDataPath += L"\\Adblock Plus for IE"; |
143 | 156 |
144 // Ignore errors here, this isn't a critical operation | 157 // Ignore errors here, this isn't a critical operation |
145 ::CreateDirectoryW(appDataPath.c_str(), NULL); | 158 ::CreateDirectoryW(appDataPath.c_str(), NULL); |
146 } | 159 } |
147 return appDataPath; | 160 return appDataPath; |
148 } | 161 } |
149 | 162 |
150 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st
d::wstring replacement) | 163 void ReplaceString(std::wstring& input, const std::wstring& placeholder, const s
td::wstring& replacement) |
151 { | 164 { |
152 size_t replaceStart = input.find(placeholder); | 165 size_t replaceStart = input.find(placeholder); |
153 if (replaceStart != std::string::npos) | 166 if (replaceStart != std::string::npos) |
154 { | 167 { |
155 input.replace(replaceStart, placeholder.length(), replacement); | 168 input.replace(replaceStart, placeholder.length(), replacement); |
156 } | 169 } |
157 } | 170 } |
OLD | NEW |