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

    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 "variables.h"

// this is for get and put(s)
static CRITICAL_SECTION csVarRegister;
static VARIABLEREGISTER *vr = NULL;
static int vrCount = 0;

static int addToVariablesRegister(TCHAR *szName, TCHAR *szText)
{
	if ((szName == NULL) || (szText == NULL) || (_tcslen(szName) <= 0))
		return -1;

	mir_cslock lck(csVarRegister);
	for (int i = 0; i < vrCount; i++) {
		if ((!_tcscmp(vr[i].szName, szName))) {
			mir_free(vr[i].szText);
			vr[i].szText = mir_tstrdup(szText);
			return 0;
		}
	}
	vr = (VARIABLEREGISTER*)mir_realloc(vr, (vrCount + 1)*sizeof(VARIABLEREGISTER));
	if (vr == NULL)
		return -1;

	vr[vrCount].szName = mir_tstrdup(szName);
	vr[vrCount].szText = mir_tstrdup(szText);
	vr[vrCount].dwOwnerThread = GetCurrentThreadId();
	vrCount += 1;
	return 0;
}

static TCHAR *searchVariableRegister(TCHAR *szName)
{
	if ((szName == NULL) || (_tcslen(szName) <= 0))
		return NULL;

	mir_cslock lck(csVarRegister);
	for (int i = 0; i < vrCount; i++)
		if ((!_tcscmp(vr[i].szName, szName)))
			return mir_tstrdup(vr[i].szText);

	return NULL;
}

int clearVariableRegister(bool bAll)
{
	int count = 0;
	mir_cslock lck(csVarRegister);
	for (int i = 0; i < vrCount; i++) {
		if (!bAll && vr[i].dwOwnerThread != GetCurrentThreadId())
			continue;

		mir_free(vr[i].szName);
		mir_free(vr[i].szText);
		if (vrCount > 1) {
			memcpy(&vr[i], &vr[vrCount-1], sizeof(VARIABLEREGISTER));
			vr = (VARIABLEREGISTER*)mir_realloc(vr, (vrCount-1)*sizeof(VARIABLEREGISTER));
			if (vr == NULL)
				return -1;

			vrCount--;
		}
		else {
			mir_free(vr);
			vr = NULL;
			vrCount = 0;
		}
		count += 1;
	}

	return count;
}

static TCHAR *parsePut(ARGUMENTSINFO *ai)
{
	FORMATINFO fi;

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

	//	ai->flags |= AIF_DONTPARSE;
	if (addToVariablesRegister(ai->targv[1], ai->targv[2]))
		return NULL;

	memcpy(&fi, ai->fi, sizeof(fi));
	fi.tszFormat = ai->targv[2];
	fi.flags |= FIF_TCHAR;
	return formatString(&fi);
}

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

	if (addToVariablesRegister(ai->targv[1], ai->targv[2]))
		return NULL;

	return mir_tstrdup(_T(""));
}

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

	return searchVariableRegister(ai->targv[1]);
}

void registerVariablesTokens()
{
	registerIntToken(GET, parseGet, TRF_FUNCTION, LPGEN("Variables")"\t(x)\t"LPGEN("variable set by put(s) with name x"));
	registerIntToken(PUT, parsePut, TRF_FUNCTION, LPGEN("Variables")"\t(x,y)\t"LPGEN("x, and stores y as variable named x"));//TRF_UNPARSEDARGS);
	registerIntToken(PUTS, parsePuts, TRF_FUNCTION, LPGEN("Variables")"\t(x,y)\t"LPGEN("only stores y as variables x"));//TRF_UNPARSEDARGS);
	InitializeCriticalSection(&csVarRegister);
}

void unregisterVariablesTokens()
{
	clearVariableRegister(true);
	DeleteCriticalSection(&csVarRegister);
}