summaryrefslogtreecommitdiff
path: root/plugins/TrafficCounter/src/misc.cpp
blob: afbc60e84114559b5accd406977035cb398cf28b (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
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 "commonheaders.h"

/* Ôóíêöèÿ ðàçáèðàåò ñòðîêó è âîçâðàùàåò ñïèñîê òåãîâ è ñîîòâåòñòâóþùèõ èì ñòðîê.
Àðãóìåíòû:
InputString - ñòðîêà äëÿ ðàçáîðà;
RowItemsList - ñïèñîê íàéäåííûõ ýëåìåíòîâ.
Âîçâðàùàåìîå çíà÷åíèå - êîëè÷åñòâî ýëåìåíòîâ â ñïèñêàõ. */
WORD GetRowItems(TCHAR *InputString, RowItemInfo **RowItemsList)
{
	TCHAR *begin, *end;
	WORD c = 0;

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

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

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

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

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

		if (begin)
		{
			// Âûäåëÿåì ïàìÿòü ïîä ñòðîêó.
			(*RowItemsList)[c].String = (TCHAR*)mir_alloc(sizeof(TCHAR) * (begin - end));
			// Êîïèðóåì ñòðîêó.
			_tcsncpy((*RowItemsList)[c].String, end + 1, begin - end - 1);
			(*RowItemsList)[c].String[begin - end - 1] = 0;
		}
		else
		{
			// Âûäåëÿåì ïàìÿòü ïîä ñòðîêó.
			(*RowItemsList)[c].String = (TCHAR*)mir_alloc(sizeof(TCHAR) * _tcslen(end));
			// Êîïèðóåì ñòðîêó.
			_tcsncpy((*RowItemsList)[c].String, end + 1, _tcslen(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 - ðàçìåð áóôåðà.
Âîçâðàùàåìîå çíà÷åíèå: òðåáóåìûé ðàçìåð áóôåðà.
*/
WORD GetFormattedTraffic(DWORD Value, BYTE Unit, TCHAR *Buffer, WORD Size)
{
	TCHAR Str1[32], szUnit[4] = {' ', 0};
	DWORD Divider;
	NUMBERFMT nf = {0, 1, 3, _T(","), _T(" "), 0};
	TCHAR *Res; // Ïðîìåæóòî÷íûé ðåçóëüòàò.
	WORD l;

	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;
	}

	mir_sntprintf(Str1, SIZEOF(Str1), _T("%d.%d"), Value / Divider, Value % Divider);
	l = GetNumberFormat(LOCALE_USER_DEFAULT, 0, Str1, &nf, NULL, 0);
	if (!l) return 0;
	l += _tcslen(szUnit) + 1;
	Res = (TCHAR*)malloc(l * sizeof(TCHAR));
	if (!Res) return 0;
	GetNumberFormat(LOCALE_USER_DEFAULT, 0, Str1, &nf, Res, l);
	_tcscat(Res, szUnit);

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

	free(Res);
	return l;
}

/* Ïðåîáðàçîâàíèå èíòåðâàëà âðåìåíè â åãî ñòðîêîâîå ïðåäñòàâëåíèå
Àðãóìåíòû:
Duration: èíòåðâàë âðåìåíè â ñåêóíäàõ;
Format: ñòðîêà ôîðìàòà;
Buffer: àäðåñ áóôåðà, êóäà ôóíêöèÿ ïîìåùàåò ðåçóëüòàò.
Size - ðàçìåð áóôåðà. */
WORD GetDurationFormatM(DWORD Duration, TCHAR *Format, TCHAR *Buffer, WORD Size)
{
	DWORD q;
	WORD TokenIndex, FormatIndex, Length;
	TCHAR Token[256],  // Àêêóìóëÿòîð.
		*Res; // Ïðîìåæóòî÷íûé ðåçóëüòàò.

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

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

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

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

	if (Size && Buffer)
	{
		_tcsncpy(Buffer, Res, Size);
		Length = _tcslen(Buffer);
	}
	else
	{
		Length = _tcslen(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;
}