summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/utils.cpp
blob: 5f990fa3374909cd64147317c87878dedb6f8c24 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright (c) 2005 Victor Pavlychko (nullbyte@sotline.net.ua)
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

#include <msapi/comptr.h>

Bitmap* LoadImageFromResource(HINSTANCE hInst, int resourceId, const wchar_t *pwszType)
{
	if (HRSRC hrsrc = FindResourceW(hInst, MAKEINTRESOURCE(resourceId), pwszType)) {
		if (DWORD dwSize = SizeofResource(hInst, hrsrc)) {
			if (HGLOBAL hRes = LoadResource(hInst, hrsrc)) {
				void *pImage = LockResource(hRes);

				if (HGLOBAL hGlobal = ::GlobalAlloc(GHND, dwSize)) {
					void *pBuffer = ::GlobalLock(hGlobal);
					if (pBuffer) {
						memcpy(pBuffer, pImage, dwSize);

						CComPtr<IStream> pStream;
						HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
						if (SUCCEEDED(hr))
							return new Gdiplus::Bitmap(pStream);
					}

					GlobalFree(hGlobal); // free memory only if the function fails
				}
			}
		}
	}

	return nullptr;
}

/////////////////////////////////////////////////////////////////////////////////////////

uint32_t toggleBit(uint32_t dw, uint32_t bit)
{
	if (dw & bit)
		return dw & ~bit;
	return dw | bit;
}

bool CheckFilter(wchar_t *buf, wchar_t *filter)
{
	//	MessageBox(0, buf, filter, MB_OK);
	int l1 = (int)mir_wstrlen(buf);
	int l2 = (int)mir_wstrlen(filter);
	for (int i = 0; i < l1 - l2 + 1; i++)
		if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, buf + i, l2, filter, l2) == CSTR_EQUAL)
			return true;
	return false;
}

int GetFontHeight(const LOGFONTA &lf)
{
	return 2 * abs(lf.lfHeight) * 74 / g_iPixelY;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct
{
	wchar_t *pStart, *pEnd;
	size_t cbStart, cbEnd;
}
static bbcodes[] = 
{
	{ L"[b]",      nullptr },
	{ L"[/b]",     nullptr },
	{ L"[i]",      nullptr },
	{ L"[/i]",     nullptr },
	{ L"[u]",      nullptr },
	{ L"[/u]",     nullptr },
	{ L"[s]",      nullptr },
	{ L"[/s]",     nullptr },

	{ L"[color=", L"]"     },
	{ L"[/color]", nullptr },

	{ L"[c0]",     nullptr },
	{ L"[c1]",     nullptr },
	{ L"[c2]",     nullptr },
	{ L"[c3]",     nullptr },
	{ L"[c4]",     nullptr },
	{ L"[c5]",     nullptr },
	{ L"[c6]",     nullptr },

	{ L"[$hicon=", L"$]"   },

	{ L"[url]", L"[/url]"  },
	{ L"[url=", L"]",      },
	{ L"[img]", L"[/img]"  },
	{ L"[img=", L"]"       },
};

void RemoveBbcodes(CMStringW &wszText)
{
	if (wszText.IsEmpty())
		return;

	if (bbcodes[0].cbStart == 0)
		for (auto &it : bbcodes) {
			it.cbStart = wcslen(it.pStart);
			if (it.pEnd)
				it.cbEnd = wcslen(it.pEnd);
		}

	for (int idx = wszText.Find('[', 0); idx != -1; idx = wszText.Find('[', idx)) {
		bool bFound = false;
		for (auto &it : bbcodes) {
			if (wcsncmp(wszText.c_str() + idx, it.pStart, it.cbStart))
				continue;

			wszText.Delete(idx, (int)it.cbStart);

			if (it.pEnd) {
				int idx2 = wszText.Find(it.pEnd, idx);
				if (idx2 != -1) {
					wszText.Delete(idx, idx2 - idx);
					wszText.Delete(idx, (int)it.cbEnd);
				}
			}

			bFound = true;
			break;
		}

		// just an occasional square bracket? skip it
		if (!bFound)
			idx++;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

static int countNoWhitespace(const wchar_t *str)
{
	int c;
	for (c = 0; *str != '\n' && *str != '\r' && *str != '\t' && *str != ' ' && *str != '\0'; str++, c++);
	return c;
}

static int DetectUrl(const wchar_t *text)
{
	int i;
	for (i = 0; text[i] != '\0'; i++)
		if (!((text[i] >= '0' && text[i] <= '9') || iswalpha(text[i])))
			break;

	if (i <= 0 || wcsncmp(text + i, L"://", 3))
		return 0;

	i += countNoWhitespace(text + i);
	for (; i > 0; i--)
		if ((text[i - 1] >= '0' && text[i - 1] <= '9') || iswalpha(text[i - 1]) || text[i - 1] == '/')
			break;

	return i;
}

void UrlAutodetect(CMStringW &str)
{
	int level = 0;

	for (auto *p = str.c_str(); *p; p++) {
		if (!wcsncmp(p, L"[img=", 5) || !wcsncmp(p, L"[img]", 5) || !wcsncmp(p, L"[url]", 5) || !wcsncmp(p, L"[url=", 5)) {
			p += 4;
			level++;
		}
		if (!wcsncmp(p, L"[/img]", 6) || !wcsncmp(p, L"[/url]", 6)) {
			p += 5;
			level--;
		}
		else if (int len = DetectUrl(p)) {
			if (level == 0) {
				int pos = p - str.c_str();
				CMStringW url = str.Mid(pos, len);
				str.Insert(pos + len, L"[/url]");
				str.Insert(pos, L"[url]");
				p = str.c_str() + pos + len + 11;
			}
		}
	}
}