-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cpp
More file actions
43 lines (36 loc) · 1.18 KB
/
Common.cpp
File metadata and controls
43 lines (36 loc) · 1.18 KB
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
#include "Common.h"
HBITMAP createPlaceholderBitmap(bool error) {
HBITMAP hPlaceholderBitmap = CreateBitmap(256, 256, 1, 32, NULL);
HDC hdc = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hPlaceholderBitmap);
HBRUSH hBrush = CreateSolidBrush(RGB(error ? 0xff : 0xcc, 0xcc, 0xcc));
RECT rect = {0, 0, 256, 256};
FillRect(hMemDC, &rect, hBrush);
SelectObject(hMemDC, hOldBitmap);
DeleteObject(hBrush);
DeleteDC(hMemDC);
ReleaseDC(NULL, hdc);
return hPlaceholderBitmap;
}
std::string urlEncode(const std::wstring& url) {
int utf8Length = WideCharToMultiByte(CP_UTF8, 0, url.c_str(), -1, 0, 0, 0, 0);
std::string utf8(utf8Length - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, url.c_str(), -1, &utf8[0], utf8Length, 0, 0);
static const char hex[] = "0123456789ABCDEF";
std::string encoded;
for (size_t i = 0; i < utf8.size(); ++i) {
unsigned char c = utf8[i];
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '_' || c == '.' || c == '~') {
encoded += c;
} else {
encoded += '%';
encoded += hex[c >> 4];
encoded += hex[c & 0x0F];
}
}
return encoded;
}