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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef UTILS_H | 18 #ifndef UTILS_H |
19 #define UTILS_H | 19 #define UTILS_H |
20 | 20 |
21 #include <algorithm> | 21 #include <algorithm> |
22 #include <locale> | 22 #include <locale> |
23 #include <functional> | 23 #include <functional> |
24 #include <string> | 24 #include <string> |
25 #include <vector> | 25 #include <vector> |
| 26 #include <wtypes.h> |
26 | 27 |
27 #define WM_ALREADY_UP_TO_DATE WM_APP+1 | 28 #define WM_ALREADY_UP_TO_DATE WM_APP+1 |
28 #define WM_UPDATE_CHECK_ERROR WM_APP+2 | 29 #define WM_UPDATE_CHECK_ERROR WM_APP+2 |
29 #define WM_DOWNLOADING_UPDATE WM_APP+3 | 30 #define WM_DOWNLOADING_UPDATE WM_APP+3 |
30 | 31 |
31 // | 32 // |
32 // Application Package Authority. | 33 // Application Package Authority. |
33 // | 34 // |
34 | 35 |
35 #define SECURITY_APP_PACKAGE_AUTHORITY {0,0,0,0,0,15} | 36 #define SECURITY_APP_PACKAGE_AUTHORITY {0,0,0,0,0,15} |
36 | 37 |
37 #define SECURITY_APP_PACKAGE_BASE_RID (0x00000002L) | 38 #define SECURITY_APP_PACKAGE_BASE_RID (0x00000002L) |
38 #define SECURITY_BUILTIN_APP_PACKAGE_RID_COUNT (2L) | 39 #define SECURITY_BUILTIN_APP_PACKAGE_RID_COUNT (2L) |
39 #define SECURITY_APP_PACKAGE_RID_COUNT (8L) | 40 #define SECURITY_APP_PACKAGE_RID_COUNT (8L) |
40 #define SECURITY_CAPABILITY_BASE_RID (0x00000003L) | 41 #define SECURITY_CAPABILITY_BASE_RID (0x00000003L) |
41 #define SECURITY_BUILTIN_CAPABILITY_RID_COUNT (2L) | 42 #define SECURITY_BUILTIN_CAPABILITY_RID_COUNT (2L) |
42 #define SECURITY_CAPABILITY_RID_COUNT (5L) | 43 #define SECURITY_CAPABILITY_RID_COUNT (5L) |
43 | 44 |
44 // | 45 // |
45 // Built-in Packages. | 46 // Built-in Packages. |
46 // | 47 // |
47 | 48 |
48 #define SECURITY_BUILTIN_PACKAGE_ANY_PACKAGE (0x00000001L) | 49 #define SECURITY_BUILTIN_PACKAGE_ANY_PACKAGE (0x00000001L) |
49 | 50 |
50 | 51 |
51 bool IsWindowsVistaOrLater(); | 52 bool IsWindowsVistaOrLater(); |
52 bool IsWindows8OrLater(); | 53 bool IsWindows8OrLater(); |
53 | 54 |
| 55 std::wstring ToWstring(const BSTR b); |
54 std::string ToUtf8String(const std::wstring& str); | 56 std::string ToUtf8String(const std::wstring& str); |
55 std::wstring ToUtf16String(const std::string& str); | 57 std::wstring ToUtf16String(const std::string& str); |
56 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& value); | 58 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& value); |
57 std::wstring GetDllDir(); | 59 std::wstring GetDllDir(); |
58 std::wstring GetExeDir(); | 60 std::wstring GetExeDir(); |
59 std::wstring GetAppDataPath(); | 61 std::wstring GetAppDataPath(); |
60 void ReplaceString(std::wstring& input, const std::wstring& placeholder, const s
td::wstring& replacement); | 62 void ReplaceString(std::wstring& input, const std::wstring& placeholder, const s
td::wstring& replacement); |
61 | 63 |
62 /** | 64 /** |
63 * Returns the beginning of the URL which includes the scheme and hierarchical | 65 * Returns the beginning of the URL which includes the scheme and hierarchical |
64 * part according to http://en.wikipedia.org/wiki/URI_scheme. | 66 * part according to http://en.wikipedia.org/wiki/URI_scheme. |
65 */ | 67 */ |
66 std::wstring GetSchemeAndHierarchicalPart(const std::wstring& url); | 68 std::wstring GetSchemeAndHierarchicalPart(const std::wstring& url); |
67 | 69 |
68 std::wstring GetQueryString(const std::wstring& url); | 70 std::wstring GetQueryString(const std::wstring& url); |
69 | 71 |
| 72 /* |
| 73 * The method used below for trimming strings is taken from here: |
| 74 * http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstr
ing |
| 75 */ |
| 76 template<class C> |
| 77 bool isNotWhitespace(C x) |
| 78 { |
| 79 return !std::isspace(x, std::locale::classic()); |
| 80 }; |
| 81 |
| 82 template<class T> |
| 83 void TrimStringInPlaceLeft(T& text) |
| 84 { |
| 85 text.erase(text.begin(), std::find_if(text.begin(), text.end(), isNotWhitespac
e<T::value_type>)); |
| 86 } |
| 87 |
| 88 template<class T> |
| 89 void TrimStringInPlaceRight(T& text) |
| 90 { |
| 91 text.erase(std::find_if(text.rbegin(), text.rend(), isNotWhitespace<T::value_t
ype>).base(), text.end()); |
| 92 } |
| 93 |
| 94 template<class T> |
| 95 void TrimStringInPlace(T& text) |
| 96 { |
| 97 TrimStringInPlaceRight(text); |
| 98 TrimStringInPlaceLeft(text); |
| 99 } |
| 100 |
| 101 template<class T> |
| 102 T TrimStringLeft(T text) |
| 103 { |
| 104 TrimStringInPlaceLeft(text); |
| 105 return text; |
| 106 } |
| 107 |
| 108 template<class T> |
| 109 T TrimStringRight(T text) |
| 110 { |
| 111 TrimStringInPlaceRight(text); |
| 112 return text; |
| 113 } |
| 114 |
70 template<class T> | 115 template<class T> |
71 T TrimString(T text) | 116 T TrimString(T text) |
72 { | 117 { |
73 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring | 118 TrimStringInPlaceRight(text); |
74 T trimmed(text); | 119 TrimStringInPlaceLeft(text); |
75 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_
type>, std::placeholders::_1, std::locale::classic()); | 120 return text; |
76 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(isspace))); | |
77 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace
)).base(), trimmed.end()); | |
78 return trimmed; | |
79 } | 121 } |
80 | 122 |
81 #endif // UTILS_H | 123 #endif // UTILS_H |
OLD | NEW |