summaryrefslogtreecommitdiff
path: root/plugins/TrafficCounter/src/misc.cpp
blob: 398feaab963d3ca4c52ffbe461b8d68d9ecdd4ce (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Traffic Counter plugin for Miranda IM 
Copyright 2007-2011 Mironych.

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; either version 2
of the License, or (at your option) any later version.

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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

/* Ôóíêöèÿ ðàçáèðàåò ñòðîêó è âîçâðàùàåò ñïèñîê òåãîâ è ñîîòâåòñòâóþùèõ èì ñòðîê.
Àðãóìåíòû:
InputString - ñòðîêà äëÿ ðàçáîðà;
RowItemsList - ñïèñîê íàéäåííûõ ýëåìåíòîâ.
Âîçâðàùàåìîå çíà÷åíèå - êîëè÷åñòâî ýëåìåíòîâ â ñïèñêàõ. */

WORD GetRowItems(wchar_t *InputString, RowItemInfo **RowItemsList)
{
	wchar_t *begin, *end;
	WORD c = 0;

	// Èùåì ñëåâà îòêðûâàþùóþ ñêîáêó.
	begin = wcschr(InputString, '{');
	// Åñëè ñêîáêà íàéäåíà...
	if (begin) {
		// Âûäåëÿåì ïàìÿòü ïîä óêàçàòåëè
		*RowItemsList = (RowItemInfo*)mir_alloc(sizeof(RowItemInfo));
	}
	else return 0;

	do {
		// Ñðàçó âñëåä çà íåé èùåì çàêðûâàþùóþ.
		end = wcschr(begin, '}');

		// Âûäåëÿåì ïàìÿòü ïîä óêàçàòåëè
		*RowItemsList = (RowItemInfo*)mir_realloc(*RowItemsList, sizeof(RowItemInfo) * (c + 1));

		// Ðàçáèðàåì òåã.
		swscanf(begin + 1, L"%c%hd",
			&((*RowItemsList)[c].Alignment),
			&((*RowItemsList)[c].Interval));

		// Èùåì äàëåå îòêðûâàþùóþ ñêîáêó - ýòî êîíåö ñòðîêè, ñîîòâåòñòâóþùåé òåãó.
		begin = wcschr(end, '{');

		if (begin) {
			// Âûäåëÿåì ïàìÿòü ïîä ñòðîêó.
			(*RowItemsList)[c].String = (wchar_t*)mir_alloc(sizeof(wchar_t) * (begin - end));
			// Êîïèðóåì ñòðîêó.
			wcsncpy((*RowItemsList)[c].String, end + 1, begin - end - 1);
			(*RowItemsList)[c].String[begin - end - 1] = 0;
		}
		else {
			// Âûäåëÿåì ïàìÿòü ïîä ñòðîêó.
			(*RowItemsList)[c].String = (wchar_t*)mir_alloc(sizeof(wchar_t) * mir_tstrlen(end));
			// Êîïèðóåì ñòðîêó.
			wcsncpy((*RowItemsList)[c].String, end + 1, mir_tstrlen(end));
		}

		c++;
	} while (begin);

	return c;
}

/* Ôóíêöèÿ âîçâðàùàåò êîëè÷åñòâî äíåé â óêàçàííîì ìåñÿöå óêàçàííîãî ãîäà. */
BYTE DaysInMonth(BYTE Month, WORD Year)
{
	switch (Month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12: return 31;
	case 4:
	case 6:
	case 9:
	case 11: return 30;
	case 2: return 28 + (BYTE)!((Year % 4) && ((Year % 100) || !(Year % 400)));
	}
	return 0;
}

// Ôóíêöèÿ îïðåäåëÿåò äåíü íåäåëè ïî äàòå
// 7 - ÂÑ, 1 - ÏÍ è ò. ä.
BYTE DayOfWeek(BYTE Day, BYTE Month, WORD Year)
{
	WORD a, y, m;

	a = (14 - Month) / 12;
	y = Year - a;
	m = Month + 12 * a - 2;

	a = (7000 + (Day + y + (y >> 2) - y / 100 + y / 400 + (31 * m) / 12)) % 7;
	if (!a) a = 7;

	return a;
}

/*
Àðãóìåíòû:
Value - êîëè÷åñòâî áàéò;
Unit - åäèíèöû èçìåðåíèÿ (0 - áàéòû, 1 - êèëîáàéòû, 2 - ìåãàáàéòû, 3 - àâòîìàòè÷åñêè);
Buffer - àäðåñ ñòðîêè äëÿ çàïèñè ðåçóëüòàòà;
Size - ðàçìåð áóôåðà.
Âîçâðàùàåìîå çíà÷åíèå: òðåáóåìûé ðàçìåð áóôåðà.
*/
size_t GetFormattedTraffic(DWORD Value, BYTE Unit, wchar_t *Buffer, size_t Size)
{
	wchar_t Str1[32], szUnit[4] = { ' ', 0 };
	DWORD Divider;
	NUMBERFMT nf = { 0, 1, 3, L",", L" ", 0 };
	wchar_t *Res; // Ïðîìåæóòî÷íûé ðåçóëüòàò.

	switch (Unit) {
	case 0: //bytes
		Divider = 1;
		nf.NumDigits = 0;
		szUnit[0] = 0;
		break;
	case 1: // KB
		Divider = 0x400;
		nf.NumDigits = 2;
		break;
	case 2: // MB
		Divider = 0x100000;
		nf.NumDigits = 2;
		break;
	case 3: // Adaptive
		nf.NumDigits = 2;
		if (Value < 0x100000) { Divider = 0x400; szUnit[1] = 'K'; szUnit[2] = 'B'; }
		else { Divider = 0x100000; szUnit[1] = 'M'; szUnit[2] = 'B'; }
		break;
	default:
		return 0;
	}

	mir_sntprintf(Str1, L"%d.%d", Value / Divider, Value % Divider);
	size_t l = GetNumberFormat(LOCALE_USER_DEFAULT, 0, Str1, &nf, NULL, 0);
	if (!l) return 0;
	l += mir_tstrlen(szUnit) + 1;
	Res = (wchar_t*)malloc(l * sizeof(wchar_t));
	if (!Res) return 0;
	GetNumberFormat(LOCALE_USER_DEFAULT, 0, Str1, &nf, Res, (int)l);
	mir_tstrcat(Res, szUnit);

	if (Size && Buffer) {
		mir_tstrcpy(Buffer, Res);
		l = mir_tstrlen(Buffer);
	}
	else l = mir_tstrlen(Res) + 1;

	free(Res);
	return l;
}

/* Ïðåîáðàçîâàíèå èíòåðâàëà âðåìåíè â åãî ñòðîêîâîå ïðåäñòàâëåíèå
Àðãóìåíòû:
Duration: èíòåðâàë âðåìåíè â ñåêóíäàõ;
Format: ñòðîêà ôîðìàòà;
Buffer: àäðåñ áóôåðà, êóäà ôóíêöèÿ ïîìåùàåò ðåçóëüòàò.
Size - ðàçìåð áóôåðà. */

size_t GetDurationFormatM(DWORD Duration, wchar_t *Format, wchar_t *Buffer, size_t Size)
{
	size_t Length;
	DWORD q;
	WORD TokenIndex, FormatIndex;
	wchar_t Token[256],  // Àêêóìóëÿòîð.
		*Res; // Ïðîìåæóòî÷íûé ðåçóëüòàò.

	Res = (wchar_t*)malloc(sizeof(wchar_t)); // Âûäåëÿåì ÷óòü-÷óòü ïàìÿòè ïîä ðåçóëüòàò, íî ýòî òîëüêî íà÷àëî.
	//SecureZeroMemory(Res, sizeof(wchar_t));
	Res[0] = 0;

	for (FormatIndex = 0; Format[FormatIndex];) {
		// Èùåì òîêåíû. Ñ÷èòàåòñÿ, ÷òî òîêåí - òîëüêî áóêâû.
		TokenIndex = 0;
		q = iswalpha(Format[FormatIndex]);
		// Êîïèðóåì ñèìâîëû â àêêóìóëÿòîð äî ñìåíû ôëàãà.
		do {
			Token[TokenIndex++] = Format[FormatIndex++];
		} while (q == iswalpha(Format[FormatIndex]));
		Token[TokenIndex] = 0;

		// ×òî ïîëó÷èëè â àêêóìóëÿòîðå?
		if (!mir_tstrcmp(Token, L"d")) {
			q = Duration / (60 * 60 * 24);
			mir_sntprintf(Token, L"%d", q);
			Duration -= q * 60 * 60 * 24;
		}
		else if (!mir_tstrcmp(Token, L"h")) {
			q = Duration / (60 * 60);
			mir_sntprintf(Token, L"%d", q);
			Duration -= q * 60 * 60;
		}
		else if (!mir_tstrcmp(Token, L"hh")) {
			q = Duration / (60 * 60);
			mir_sntprintf(Token, L"%02d", q);
			Duration -= q * 60 * 60;
		}
		else if (!mir_tstrcmp(Token, L"m")) {
			q = Duration / 60;
			mir_sntprintf(Token, L"%d", q);
			Duration -= q * 60;
		}
		else if (!mir_tstrcmp(Token, L"mm")) {
			q = Duration / 60;
			mir_sntprintf(Token, L"%02d", q);
			Duration -= q * 60;
		}
		else if (!mir_tstrcmp(Token, L"s")) {
			q = Duration;
			mir_sntprintf(Token, L"%d", q);
			Duration -= q;
		}
		else if (!mir_tstrcmp(Token, L"ss")) {
			q = Duration;
			mir_sntprintf(Token, L"%02d", q);
			Duration -= q;
		}

		// Äîáàâèì ïàìÿòè, åñëè íóæíî.
		Length = mir_tstrlen(Res) + mir_tstrlen(Token) + 1;
		Res = (wchar_t*)realloc(Res, Length * sizeof(wchar_t));
		mir_tstrcat(Res, Token);
	}

	if (Size && Buffer) {
		wcsncpy(Buffer, Res, Size);
		Length = mir_tstrlen(Buffer);
	}
	else Length = mir_tstrlen(Res) + 1;

	free(Res);
	return Length;
}

/* Ðåçóëüòàò:
-1 - st1 < st2
0 - st1 = st2
+1 - st1 > st2
*/
signed short int TimeCompare(SYSTEMTIME st1, SYSTEMTIME st2)
{
	signed short int a, b, c, d;

	a = st1.wYear - st2.wYear;
	b = st1.wMonth - st2.wMonth;
	c = st1.wDay - st2.wDay;
	d = st1.wHour - st2.wHour;

	if (a < 0) return -1;
	if (a > 0) return +1;

	if (b < 0) return -1;
	if (b > 0) return +1;

	if (c < 0) return -1;
	if (c > 0) return +1;

	if (d < 0) return -1;
	if (d > 0) return +1;
	return 0;
}