summaryrefslogtreecommitdiff
path: root/plugins/Variables/src/parse_math.cpp
blob: 14e2ec6f46407cdfb526b1acf7a9be66c38a1f53 (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
/*
    Variables Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2006 P. Boon

    This program is mir_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 "variables.h"

static TCHAR *parseAdd(ARGUMENTSINFO *ai)
{
	if (ai->argc < 3)
		return NULL;

	int result = 0;
	for (unsigned int i=1;i<ai->argc;i++)
		result += ttoi(ai->targv[i]);

	return itot(result);
}

static TCHAR *parseDiv(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return NULL;

	int val1 = ttoi(ai->targv[1]);
	int val2 = ttoi(ai->targv[2]);
	if (val2 == 0)
		return NULL;

	return itot(val1/val2);
}

static TCHAR *parseHex(ARGUMENTSINFO *ai)
{
	unsigned int i;
	TCHAR szVal[34];

	if (ai->argc != 3)
		return NULL;

	int val = ttoi(ai->targv[1]);
	int padding = ttoi(ai->targv[2]);
	mir_sntprintf(szVal, SIZEOF(szVal), _T("%x"), val);
	unsigned int zeros = max(padding - (signed int)_tcslen(szVal), 0);
	TCHAR *res = (TCHAR*)mir_alloc((zeros + _tcslen(szVal) + 3)*sizeof(TCHAR));
	if (res == NULL)
		return NULL;

	ZeroMemory(res, (zeros + _tcslen(szVal) + 3)*sizeof(TCHAR));
	_tcscpy(res, _T("0x"));
	for (i=0; i < zeros; i++)
		*(res+2+i) = '0';

	_tcscat(res, szVal);
	return res;
}

static TCHAR *parseMod(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return NULL;

	int val1 = ttoi(ai->targv[1]);
	int val2 = ttoi(ai->targv[2]);
	if (val2 == 0)
		return NULL;

	return itot(val1 % val2);
}

static TCHAR *parseMul(ARGUMENTSINFO *ai)
{
	if (ai->argc < 3)
		return NULL;

	int result = ttoi(ai->targv[1]);
	for (unsigned i=2; i < ai->argc; i++)
		result *= ttoi(ai->targv[i]);

	return itot(result);
}

static TCHAR *parseMuldiv(ARGUMENTSINFO *ai)
{
	if (ai->argc != 4)
		return NULL;

	if (ttoi(ai->targv[3]) == 0)
		return NULL;

	return itot((ttoi(ai->targv[1])*ttoi(ai->targv[2]))/ttoi(ai->targv[3]));
}

static TCHAR *parseMin(ARGUMENTSINFO *ai)
{
	if (ai->argc < 2)
		return NULL;

	int minVal = ttoi(ai->targv[1]);
	for (unsigned i=2; i < ai->argc; i++)
		minVal = min(ttoi(ai->targv[i]), minVal);

	return itot(minVal);
}

static TCHAR *parseMax(ARGUMENTSINFO *ai)
{
	if (ai->argc < 2)
		return NULL;

	int maxVal = ttoi(ai->targv[1]);
	for (unsigned i=2; i < ai->argc; i++)
		maxVal = max(ttoi(ai->targv[i]), maxVal);

	return itot(maxVal);
}

static TCHAR *parseNum(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return NULL;

	int val = ttoi(ai->targv[1]);
	int padding = ttoi(ai->targv[2]);
	TCHAR *szVal = itot(val);
	if (szVal == NULL)
		return NULL;

	unsigned zeros = max(padding - (signed int)_tcslen(szVal), 0);
	TCHAR *res = (TCHAR*)mir_alloc((zeros + _tcslen(szVal) + 1)*sizeof(TCHAR));
	if (res == NULL)
		return NULL;

	ZeroMemory(res, (zeros + _tcslen(szVal) + 1)*sizeof(TCHAR));
	TCHAR *cur = res;
	for (unsigned i=0; i < zeros; i++)
		*cur++ = '0';

	_tcscat(res, szVal);
	mir_free(szVal);

	return res;
}

static TCHAR *parseRand(ARGUMENTSINFO *ai)
{
	return itot(rand());
}

static TCHAR *parseSub(ARGUMENTSINFO *ai)
{
	if (ai->argc < 3)
		return NULL;

	int result = ttoi(ai->targv[1]);
	for (unsigned i=2;i<ai->argc;i++)
		result -= ttoi(ai->targv[i]);

	return itot(result);
}

int registerMathTokens()
{
	registerIntToken(_T(ADD), parseAdd, TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y ,...)\t"LPGEN("x + y + ..."));
	registerIntToken(_T(DIV), parseDiv,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y)\t"LPGEN("x divided by y"));
	registerIntToken(_T(HEX), parseHex,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y)\t"LPGEN("converts decimal value x to hex value and padds to length y"));
	registerIntToken(_T(MOD), parseMod, TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y)\t"LPGEN("x modulo y (remainder of x divided by y)"));
	registerIntToken(_T(MUL), parseMul,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y)\t"LPGEN("x times y"));
	registerIntToken(_T(MULDIV), parseMuldiv, TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y,z)\t"LPGEN("x times y divided by z"));
	registerIntToken(_T(MIN), parseMin,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y,...)\t"LPGEN("minimum value of (decimal) arguments"));
	registerIntToken(_T(MAX), parseMax,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y,...)\t"LPGEN("maximum value of (decimal) arguments"));
	registerIntToken(_T(NUM), parseNum,	TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y)\t"LPGEN("pads decimal value x to length y with zeros"));
	registerIntToken(_T(RAND), parseRand, TRF_FUNCTION, LPGEN("Mathematical Functions")"\t()\t"LPGEN("random number"));
	registerIntToken(_T(SUB), parseSub, TRF_FUNCTION, LPGEN("Mathematical Functions")"\t(x,y,...)\t"LPGEN("x - y - ..."));
	srand((unsigned int)GetTickCount());

	return 0;
}