1
//=============================================================================
3
// File : kvi_kvs_treenode_specialcommandforeach.cpp
4
// Creation date : Fri 07 Nov 2003 11:48:11 by Szymon Stefanek
6
// This file is part of the KVIrc IRC client distribution
7
// Copyright (C) 2003-2008 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 "kvi_kvs_treenode_specialcommandforeach.h"
26
#include "kvi_kvs_treenode_data.h"
27
#include "kvi_kvs_treenode_datalist.h"
28
#include "kvi_kvs_treenode_instruction.h"
29
#include "kvi_kvs_treenode_switchlist.h"
30
#include "kvi_kvs_runtimecontext.h"
31
#include "kvi_locale.h"
33
KviKvsTreeNodeSpecialCommandForeach::KviKvsTreeNodeSpecialCommandForeach(const QChar * pLocation,KviKvsTreeNodeData * pVar,KviKvsTreeNodeDataList * pArgs,KviKvsTreeNodeInstruction * pLoop)
34
: KviKvsTreeNodeSpecialCommand(pLocation,"foreach")
36
m_pIterationVariable = pVar;
37
m_pIterationVariable->setParent(this);
38
m_pIterationData = pArgs;
39
m_pIterationData->setParent(this);
41
m_pLoop->setParent(this);
44
KviKvsTreeNodeSpecialCommandForeach::~KviKvsTreeNodeSpecialCommandForeach()
46
delete m_pIterationVariable;
47
delete m_pIterationData;
51
void KviKvsTreeNodeSpecialCommandForeach::contextDescription(QString &szBuffer)
53
szBuffer = "Special Command \"foreach\"";
56
void KviKvsTreeNodeSpecialCommandForeach::dump(const char * prefix)
58
qDebug("%s SpecialCommandForeach",prefix);
61
m_pIterationVariable->dump(tmp.toUtf8().data());
62
m_pIterationData->dump(tmp.toUtf8().data());
63
m_pLoop->dump(tmp.toUtf8().data());
66
bool KviKvsTreeNodeSpecialCommandForeach::execute(KviKvsRunTimeContext * c)
69
l.setAutoDelete(true);
70
if(!m_pIterationData->evaluate(c,&l))
76
if(!(m_pSwitches->evaluate(c,&swl)))
80
bool bIncludeEmptyScalars = swl.find('a',"all") != 0;
82
for(KviKvsVariant * pArg = l.first();pArg;pArg = l.next())
86
case KviKvsVariantData::Array:
88
unsigned int uCnt = pArg->array()->size();
92
// we evaluate this each time (as it may actually be killed at each iteration)
93
// FIXME: maybe some kind of reference counting or a observer pattern might be a bit more efficient here
94
// (but might be far less efficient everywhere else...)
95
KviKvsRWEvaluationResult * v = m_pIterationVariable->evaluateReadWrite(c);
98
KviKvsVariant * pOne = pArg->array()->at(idx);
100
v->result()->copyFrom(*pOne);
102
v->result()->setNothing();
103
delete v; // we're done with it for this iteration
105
if(!m_pLoop->execute(c))
111
if(c->breakPending())
117
if(c->continuePending())
124
return false; // propagate the false return value
131
case KviKvsVariantData::Hash:
133
KviKvsHashIterator it(*(pArg->hash()->dict()));
134
while(KviKvsVariant * pOne = it.current())
136
// we evaluate this each time (as it may actually be killed at each iteration)
137
// FIXME: maybe some kind of reference counting or a observer pattern might be a bit more efficient here
138
// (but might be far less efficient everywhere else...)
139
KviKvsRWEvaluationResult * v = m_pIterationVariable->evaluateReadWrite(c);
142
v->result()->copyFrom(*pOne);
143
delete v; // we're done with it for this iteration
145
if(!m_pLoop->execute(c))
151
if(c->breakPending())
157
if(c->continuePending())
164
return false; // propagate the false return value
172
if(bIncludeEmptyScalars || (!pArg->isNothing()))
174
// we evaluate this each time (as it may actually be killed at each iteration)
175
// FIXME: maybe some kind of reference counting or a observer pattern might be a bit more efficient here
176
// (but might be far less efficient everywhere else...)
177
KviKvsRWEvaluationResult * v = m_pIterationVariable->evaluateReadWrite(c);
180
v->result()->copyFrom(*pArg);
181
delete v; // we're done with it for this iteration
183
if(!m_pLoop->execute(c))
189
if(c->breakPending())
195
if(c->continuePending())
201
return false; // propagate the false return value