1
//=============================================================================
3
// File : KviKvsTreeNodeSpecialCommandSwitch.cpp
4
// Creation date : Fri 02 Jan 2004 14:09:00 by Szymon Stefanek
6
// This file is part of the KVIrc IRC client distribution
7
// Copyright (C) 2004-2010 Szymon Stefanek <pragma at kvirc dot net>
9
// This program is FREE software. You can redistribute it and/or
10
// modify it under the terms of the GNU General Public License
11
// as published by the Free Software Foundation; either version 2
12
// of the License, or (at your opinion) any later version.
14
// This program is distributed in the HOPE that it will be USEFUL,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
// See the GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program. If not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
//=============================================================================
25
#include "KviKvsTreeNodeSpecialCommandSwitch.h"
26
#include "KviKvsTreeNodeExpression.h"
27
#include "KviKvsTreeNodeInstruction.h"
28
#include "KviKvsRunTimeContext.h"
29
#include "KviLocale.h"
33
KviKvsTreeNodeSpecialCommandSwitchLabel::KviKvsTreeNodeSpecialCommandSwitchLabel(const QChar * pLocation)
34
: KviKvsTreeNode(pLocation)
38
m_bHasTerminatingBreak = false;
41
KviKvsTreeNodeSpecialCommandSwitchLabel::~KviKvsTreeNodeSpecialCommandSwitchLabel()
43
if(m_pParameter)delete m_pParameter;
44
if(m_pInstruction)delete m_pInstruction;
47
void KviKvsTreeNodeSpecialCommandSwitchLabel::setParameter(KviKvsTreeNodeData * pParameter)
49
if(m_pParameter)delete m_pParameter;
50
m_pParameter = pParameter;
51
if(m_pParameter)m_pParameter->setParent(this);
54
void KviKvsTreeNodeSpecialCommandSwitchLabel::setInstruction(KviKvsTreeNodeInstruction * pInstruction)
56
if(m_pInstruction)delete m_pInstruction;
57
m_pInstruction = pInstruction;
58
if(m_pInstruction)m_pInstruction->setParent(this);
62
KviKvsTreeNodeSpecialCommandSwitchLabelCase::KviKvsTreeNodeSpecialCommandSwitchLabelCase(const QChar * pLocation)
63
: KviKvsTreeNodeSpecialCommandSwitchLabel(pLocation)
67
KviKvsTreeNodeSpecialCommandSwitchLabelCase::~KviKvsTreeNodeSpecialCommandSwitchLabelCase()
71
void KviKvsTreeNodeSpecialCommandSwitchLabelCase::contextDescription(QString &szBuffer)
73
szBuffer = "Label \"case\" For Special Command \"switch\"";
76
void KviKvsTreeNodeSpecialCommandSwitchLabelCase::dump(const char * prefix)
78
qDebug("%s SpecialCommandSwitchLabelCase",prefix);
81
if(m_pParameter)m_pParameter->dump(tmp.toUtf8().data());
82
if(m_pInstruction)m_pInstruction->dump(tmp.toUtf8().data());
85
bool KviKvsTreeNodeSpecialCommandSwitchLabelCase::execute(KviKvsRunTimeContext * c,KviKvsVariant * pRealParameter, bool * bPassThrough)
90
if(!m_pParameter->evaluateReadOnly(c,&v))return false;
93
if(pRealParameter->asNumber(num))
96
if(!v.asNumber(num2))return true; // a number and a non number can't match
101
if(num.integer() != num2.integer())return true;
103
if(((double)(num.integer())) != num2.real())return true;
108
if(num.real() != ((double)(num2.integer())))return true;
110
if(num.real() != num2.real())return true;
114
// string comparision, case insensitive
119
pRealParameter->asString(val);
121
if(reg.toLower() != val.toLower())return true;
125
*bPassThrough = true;
129
if(!m_pInstruction->execute(c))return false; // might be a break too
131
if(m_bHasTerminatingBreak)
133
c->setBreakPending();
140
KviKvsTreeNodeSpecialCommandSwitchLabelMatch::KviKvsTreeNodeSpecialCommandSwitchLabelMatch(const QChar * pLocation)
141
: KviKvsTreeNodeSpecialCommandSwitchLabel(pLocation)
145
KviKvsTreeNodeSpecialCommandSwitchLabelMatch::~KviKvsTreeNodeSpecialCommandSwitchLabelMatch()
149
void KviKvsTreeNodeSpecialCommandSwitchLabelMatch::contextDescription(QString &szBuffer)
151
szBuffer = "Label \"match\" For Special Command \"switch\"";
154
void KviKvsTreeNodeSpecialCommandSwitchLabelMatch::dump(const char * prefix)
156
qDebug("%s SpecialCommandSwitchLabelMatch",prefix);
157
QString tmp = prefix;
159
if(m_pParameter)m_pParameter->dump(tmp.toUtf8().data());
160
if(m_pInstruction)m_pInstruction->dump(tmp.toUtf8().data());
163
bool KviKvsTreeNodeSpecialCommandSwitchLabelMatch::execute(KviKvsRunTimeContext * c,KviKvsVariant * pRealParameter, bool * bPassThrough)
168
if(!m_pParameter->evaluateReadOnly(c,&v))return false;
173
//QRegExp rx(reg,false,true);
174
QRegExp rx(reg,Qt::CaseInsensitive,QRegExp::Wildcard);
176
pRealParameter->asString(val);
178
if(!rx.exactMatch(val))return true; // no match
181
*bPassThrough = true;
185
if(!m_pInstruction->execute(c))return false; // might be a break too
187
if(m_bHasTerminatingBreak)
189
c->setBreakPending();
196
KviKvsTreeNodeSpecialCommandSwitchLabelRegexp::KviKvsTreeNodeSpecialCommandSwitchLabelRegexp(const QChar * pLocation)
197
: KviKvsTreeNodeSpecialCommandSwitchLabel(pLocation)
201
KviKvsTreeNodeSpecialCommandSwitchLabelRegexp::~KviKvsTreeNodeSpecialCommandSwitchLabelRegexp()
205
void KviKvsTreeNodeSpecialCommandSwitchLabelRegexp::contextDescription(QString &szBuffer)
207
szBuffer = "Label \"regexp\" For Special Command \"switch\"";
210
void KviKvsTreeNodeSpecialCommandSwitchLabelRegexp::dump(const char * prefix)
212
qDebug("%s SpecialCommandSwitchLabelRegexp",prefix);
213
QString tmp = prefix;
215
if(m_pParameter)m_pParameter->dump(tmp.toUtf8().data());
216
if(m_pInstruction)m_pInstruction->dump(tmp.toUtf8().data());
219
bool KviKvsTreeNodeSpecialCommandSwitchLabelRegexp::execute(KviKvsRunTimeContext * c,KviKvsVariant * pRealParameter, bool * bPassThrough)
224
if(!m_pParameter->evaluateReadOnly(c,&v))return false;
229
// QRegExp rx(reg,false,false);
230
QRegExp rx(reg,Qt::CaseInsensitive,QRegExp::RegExp);
232
pRealParameter->asString(val);
234
if(!rx.exactMatch(val))return true; // no match
237
*bPassThrough = true;
241
if(!m_pInstruction->execute(c))return false; // might be a break too
243
if(m_bHasTerminatingBreak)
245
c->setBreakPending();
252
KviKvsTreeNodeSpecialCommandSwitchLabelDefault::KviKvsTreeNodeSpecialCommandSwitchLabelDefault(const QChar * pLocation)
253
: KviKvsTreeNodeSpecialCommandSwitchLabel(pLocation)
257
KviKvsTreeNodeSpecialCommandSwitchLabelDefault::~KviKvsTreeNodeSpecialCommandSwitchLabelDefault()
261
void KviKvsTreeNodeSpecialCommandSwitchLabelDefault::contextDescription(QString &szBuffer)
263
szBuffer = "Label \"default\" For Special Command \"switch\"";
266
void KviKvsTreeNodeSpecialCommandSwitchLabelDefault::dump(const char * prefix)
268
qDebug("%s SpecialCommandSwitchLabelDefault",prefix);
269
QString tmp = prefix;
271
if(m_pInstruction)m_pInstruction->dump(tmp.toUtf8().data());
274
bool KviKvsTreeNodeSpecialCommandSwitchLabelDefault::execute(KviKvsRunTimeContext * c,KviKvsVariant *, bool * bPassThrough)
276
*bPassThrough = true;
280
if(!m_pInstruction->execute(c))return false; // might be a break too
282
if(m_bHasTerminatingBreak)
284
c->setBreakPending();
291
KviKvsTreeNodeSpecialCommandSwitch::KviKvsTreeNodeSpecialCommandSwitch(const QChar * pLocation,KviKvsTreeNodeExpression * e)
292
: KviKvsTreeNodeSpecialCommand(pLocation,"switch")
295
m_pExpression->setParent(this);
296
m_pLabels = new KviPointerList<KviKvsTreeNodeSpecialCommandSwitchLabel>;
297
m_pLabels->setAutoDelete(true);
300
KviKvsTreeNodeSpecialCommandSwitch::~KviKvsTreeNodeSpecialCommandSwitch()
302
delete m_pExpression;
306
void KviKvsTreeNodeSpecialCommandSwitch::addLabel(KviKvsTreeNodeSpecialCommandSwitchLabel * l)
308
m_pLabels->append(l);
312
void KviKvsTreeNodeSpecialCommandSwitch::contextDescription(QString &szBuffer)
314
szBuffer = "Special Command \"switch\"";
317
void KviKvsTreeNodeSpecialCommandSwitch::dump(const char * prefix)
319
qDebug("%s SpecialCommandSwitch",prefix);
320
QString tmp = prefix;
322
m_pExpression->dump(tmp.toUtf8().data());
323
for(KviKvsTreeNodeSpecialCommandSwitchLabel * l = m_pLabels->first();l;l = m_pLabels->next())
324
l->dump(tmp.toUtf8().data());
327
bool KviKvsTreeNodeSpecialCommandSwitch::execute(KviKvsRunTimeContext * c)
330
if(!m_pExpression->evaluateReadOnly(c,&v))return false;
332
KviKvsSwitchList swl;
335
if(!(m_pSwitches->evaluate(c,&swl)))return false;
338
bool bUsePassThrough = swl.find('p',"--passthrough");
339
bool bPassThrough = false;
340
for(KviKvsTreeNodeSpecialCommandSwitchLabel * l = m_pLabels->first();l;l = m_pLabels->next())
342
if(!l->execute(c,&v, &bPassThrough))
344
if(c->error())return false;
346
if(c->breakPending())
354
bPassThrough = false;