1
//=============================================================================
3
// File : KviKvsParameterProcessor.cpp
4
// Creation date : Sun 17 Apr 2005 16:47:09 by Szymon Stefanek
6
// This file is part of the KVIrc IRC client distribution
7
// Copyright (C) 2005-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 "KviKvsParameterProcessor.h"
26
#include "KviKvsVariantList.h"
27
#include "KviKvsRunTimeContext.h"
28
#include "KviKvsVariantList.h"
29
#include "KviKvsArray.h"
30
#include "KviKvsArrayCast.h"
31
#include "KviKvsHash.h"
32
#include "KviKvsObject.h"
33
#include "KviQString.h"
34
#include "KviLocale.h"
35
#include "KviPointerList.h"
37
#include <QStringList>
39
namespace KviKvsParameterProcessor
41
void setDefaultValue(KviKvsParameterProcessor::ParameterFormat * pFmtArray)
43
switch(pFmtArray->uType)
46
case KVS_PT_NONEMPTYSTRING:
47
*((QString *)(pFmtArray->pContainer)) = QString();
50
*((kvs_int_t *)(pFmtArray->pContainer)) = 0;
53
*((kvs_uint_t *)(pFmtArray->pContainer)) = 0;
56
*((kvs_real_t *)(pFmtArray->pContainer)) = 0.0;
59
*((bool *)(pFmtArray->pContainer)) = false;
62
*((KviKvsHash **)(pFmtArray->pContainer)) = 0;
65
*((KviKvsArray **)(pFmtArray->pContainer)) = 0;
67
case KVS_PT_ARRAYCAST:
68
((KviKvsArrayCast *)(pFmtArray->pContainer))->clear();
71
*((KviKvsVariant **)(pFmtArray->pContainer)) = 0;
74
case KVS_PT_NONEMPTYCSTRING:
75
*((QByteArray *)(pFmtArray->pContainer)) = 0;
77
case KVS_PT_STRINGLIST:
78
((QStringList *)(pFmtArray->pContainer))->clear();
80
case KVS_PT_VARIANTLIST:
81
((KviKvsVariantList *)(pFmtArray->pContainer))->clear();
82
((KviKvsVariantList *)(pFmtArray->pContainer))->setAutoDelete(false);
85
*((kvs_hobject_t *)(pFmtArray->pContainer)) = (kvs_hobject_t)0;
91
qDebug("Internal error in KviKvsParameterProcessor::setDefaultValue(): unknown parameter type %d",pFmtArray->uType);
96
bool handleParameterTypeError(KviKvsRunTimeContext * pContext,KviKvsParameterProcessor::ParameterFormat * pFmtArray,KviKvsVariant * v,const char * szExpectedType)
98
if(pFmtArray->uFlags & KVS_PF_OPTIONAL)
102
setDefaultValue(pFmtArray);
103
return true; // empty optional
107
QString szError = QString(__tr2qs_ctx("Invalid data type for parameter \"%1\"","kvs")).arg(pFmtArray->szName);
112
QString tmp = v->string();
115
KviQString::appendFormatted(szError,__tr2qs_ctx("found empty string where type '%s' was expected","kvs"),&tmp,szExpectedType);
117
if(tmp.length() > 15)
122
KviQString::appendFormatted(szError,__tr2qs_ctx("found string value \"%Q\" where type '%s' was expected","kvs"),&tmp,szExpectedType);
127
KviQString::appendFormatted(szError,__tr2qs_ctx("found type %Q where type '%s' was expected","kvs"),&tmp,szExpectedType);
129
pContext->error(szError);
133
bool process(KviKvsVariantList * pVariantList,KviKvsRunTimeContext * pContext,KviKvsParameterProcessor::ParameterFormat * pFmtArray)
135
KviKvsVariant * v = pVariantList->first();
137
while(pFmtArray->szName)
141
// parameter not present
142
// it MUST be optional
143
if(!(pFmtArray->uFlags & KVS_PF_OPTIONAL))
146
QString szError = QString(__tr2qs_ctx("Missing non-optional parameter \"%1\"","kvs")).arg(pFmtArray->szName);
147
pContext->error(szError);
150
// ok, missing but optional (all the following are implicitly optional too)
151
// set to default values
153
setDefaultValue(pFmtArray);
155
} while(pFmtArray->szName);
158
// here we do only "light" casts: hard ones must be done explicitly by the user
159
switch(pFmtArray->uType)
162
v->asString(*((QString *)(pFmtArray->pContainer)));
163
if(pFmtArray->uFlags & KVS_PF_APPENDREMAINING)
165
v = pVariantList->next();
168
*((QString *)(pFmtArray->pContainer)) += QChar(' ');
169
v->appendAsString(*((QString *)(pFmtArray->pContainer)));
170
v = pVariantList->next();
175
case KVS_PT_STRINGLIST:
177
((QStringList *)(pFmtArray->pContainer))->clear();
180
((QStringList *)(pFmtArray->pContainer))->append(pSz);
181
v = pVariantList->next();
185
((QStringList *)(pFmtArray->pContainer))->append(pSz);
186
v = pVariantList->next();
191
case KVS_PT_VARIANTLIST:
193
((KviKvsVariantList *)(pFmtArray->pContainer))->clear();
194
((KviKvsVariantList *)(pFmtArray->pContainer))->setAutoDelete(false);
195
((KviKvsVariantList *)(pFmtArray->pContainer))->append(v);
196
v = pVariantList->next();
199
((KviKvsVariantList *)(pFmtArray->pContainer))->append(v);
200
v = pVariantList->next();
205
case KVS_PT_NONEMPTYSTRING:
207
v->asString(*((QString *)(pFmtArray->pContainer)));
208
bool bDoReturn = false;
209
if(pFmtArray->uFlags & KVS_PF_APPENDREMAINING)
211
v = pVariantList->next();
214
*((QString *)(pFmtArray->pContainer)) += QChar(' ');
215
v->appendAsString(*((QString *)(pFmtArray->pContainer)));
216
v = pVariantList->next();
220
if(((QString *)(pFmtArray->pContainer))->isEmpty())
222
QString szError = QString(__tr2qs_ctx("Invalid data type for parameter \"%1\": found empty string while a non empty one was expected","kvs")).arg(pFmtArray->szName);
223
pContext->error(szError);
226
if(bDoReturn)return true;
233
if(pFmtArray->uFlags & KVS_PF_APPENDREMAINING)
235
v = pVariantList->next();
238
*((QByteArray *)(pFmtArray->pContainer)) += ' ';
239
v->appendAsString(tmp);
240
v = pVariantList->next();
242
*((QByteArray *)(pFmtArray->pContainer)) = tmp.toUtf8();
245
*((QByteArray *)(pFmtArray->pContainer)) = tmp.toUtf8();
248
case KVS_PT_NONEMPTYCSTRING:
252
bool bDoReturn = false;
253
if(pFmtArray->uFlags & KVS_PF_APPENDREMAINING)
255
v = pVariantList->next();
258
*((QByteArray *)(pFmtArray->pContainer)) += ' ';
259
v->appendAsString(tmp);
260
v = pVariantList->next();
262
*((QByteArray *)(pFmtArray->pContainer)) = tmp.toUtf8();
265
*((QByteArray *)(pFmtArray->pContainer)) = tmp.toUtf8();
266
if(((QByteArray *)(pFmtArray->pContainer))->isEmpty())
268
QString szError = QString(__tr2qs_ctx("Invalid data type for parameter \"%1\": found empty string while a non empty one was expected","kvs")).arg(pFmtArray->szName);
269
pContext->error(szError);
272
if(bDoReturn)return true;
276
if(!v->asInteger(*((kvs_int_t *)(pFmtArray->pContainer))))
278
if(!handleParameterTypeError(pContext,pFmtArray,v,"integer"))
285
if(!v->asInteger(iTmp))
287
if(!handleParameterTypeError(pContext,pFmtArray,v,"unsigned integer"))
292
QString szError = QString(__tr2qs_ctx("Invalid data type for parameter \"%1\": found signed integer \"%2\" where type 'unsigned integer' was expected","kvs")).arg(pFmtArray->szName).arg(iTmp);
293
pContext->error(szError);
296
*((kvs_uint_t *)(pFmtArray->pContainer)) = (kvs_uint_t)iTmp;
300
if(!v->asReal(*((kvs_real_t *)(pFmtArray->pContainer))))
302
if(!handleParameterTypeError(pContext,pFmtArray,v,"real"))
309
if(!handleParameterTypeError(pContext,pFmtArray,v,"hash"))
312
*((KviKvsHash **)(pFmtArray->pContainer)) = v->hash();
315
case KVS_PT_ARRAYCAST:
316
v->castToArray((KviKvsArrayCast *)(pFmtArray->pContainer));
321
if(!handleParameterTypeError(pContext,pFmtArray,v,"array"))
324
*((KviKvsArray **)(pFmtArray->pContainer)) = v->array();
328
// this never fails: anything is converted to a boolean
329
*((bool *)(pFmtArray->pContainer)) = v->asBoolean();
332
*((KviKvsVariant **)(pFmtArray->pContainer)) = v;
335
if(!v->asHObject(*((kvs_hobject_t *)(pFmtArray->pContainer))))
337
if(!handleParameterTypeError(pContext,pFmtArray,v,"hobject"))
345
qDebug("Internal error in KviKvsParameterProcessor::processAsParameters(): unknown parameter type %d",pFmtArray->uType);
350
v = pVariantList->next();