~ubuntu-branches/ubuntu/edgy/koffice/edgy-updates

« back to all changes in this revision

Viewing changes to kspread/kspread_interpreter.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040509113300-xi5t1z4yxe7n03x7
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "kspread_interpreter.h"
2
 
#include "kspread_util.h"
3
 
#include "kspread_doc.h"
4
 
#include "kspread_map.h"
5
 
#include "kspread_cell.h"
6
 
#include "kspread_table.h"
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 1998-2002 The KSpread Team
 
3
                           www.koffice.org/kspread
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License as published by the Free Software Foundation; either
 
8
   version 2 of the License.
 
9
 
 
10
   This library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public License
 
16
   along with this library; see the file COPYING.LIB.  If not, write to
 
17
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
#include <stdlib.h>
 
22
#include <math.h>
 
23
#include <float.h>
 
24
 
 
25
#include <kdebug.h>
7
26
 
8
27
#include <koscript_parser.h>
9
28
#include <koscript_util.h>
10
29
#include <koscript_func.h>
11
30
#include <koscript_synext.h>
12
31
 
13
 
#include <stdlib.h>
14
 
#include <math.h>
15
 
#include <float.h>
16
 
#include <klocale.h>
17
 
#include <kapp.h>
18
 
#include <qdatetime.h>
 
32
#include <kspread_functions.h>
 
33
#include <kspread_util.h>
 
34
#include <kspread_sheet.h>
 
35
#include <kspread_doc.h>
19
36
 
20
 
#include <kdebug.h>
21
37
 
22
38
/***************************************************************
23
39
 *
31
47
class KSParseNodeExtraPoint : public KSParseNodeExtra
32
48
{
33
49
public:
34
 
  KSParseNodeExtraPoint( const QString& s, KSpreadMap* m, KSpreadTable* t ) : m_point( s, m, t ) { }
 
50
  KSParseNodeExtraPoint( const QString& s, KSpreadMap* m, KSpreadSheet* t ) : m_point( s, m, t )
 
51
  {
 
52
  }
35
53
 
36
54
  KSpreadPoint* point() { return &m_point; }
37
55
 
45
63
class KSParseNodeExtraRange : public KSParseNodeExtra
46
64
{
47
65
public:
48
 
  KSParseNodeExtraRange( const QString& s, KSpreadMap* m, KSpreadTable* t ) : m_range( s, m, t ) { }
 
66
  KSParseNodeExtraRange( const QString& s, KSpreadMap* m, KSpreadSheet* t )
 
67
    : m_range( s, m, t )
 
68
  {
 
69
  }
49
70
 
50
71
  KSpreadRange* range() { return &m_range; }
51
72
 
62
83
/**
63
84
 * Creates dependencies from the parse tree of a formula.
64
85
 */
65
 
void makeDepends( KSContext& context, KSParseNode* node, KSpreadMap* m, KSpreadTable* t, QList<KSpreadDepend>& depends )
 
86
void makeDepends( KSContext& context, KSParseNode* node, KSpreadMap* m, KSpreadSheet* t, QPtrList<KSpreadDependency>& depends )
66
87
{
67
88
  KSParseNodeExtra* extra = node->extra();
68
89
  if ( !extra )
70
91
    if ( node->getType() == t_cell )
71
92
    {
72
93
      KSParseNodeExtraPoint* extra = new KSParseNodeExtraPoint( node->getStringLiteral(), m, t );
73
 
      kdDebug(36002) << "-------- Got dep " << util_cellName( extra->point()->pos.x(), extra->point()->pos.y() ) << endl;
74
 
      KSpreadDepend* d = new KSpreadDepend;
75
 
      d->m_iColumn = extra->point()->pos.x();
76
 
      d->m_iRow = extra->point()->pos.y();
77
 
      d->m_iColumn2 = -1;
78
 
      d->m_iRow2 = -1;
79
 
      d->m_pTable = extra->point()->table;
80
 
      if (!d->m_pTable)
 
94
      kdDebug(36001) << "-------- Got dep " << KSpreadCell::name( extra->point()->pos.x(), extra->point()->pos.y() ) << endl;
 
95
      KSpreadDependency* d = new KSpreadDependency(extra->point()->pos.x(), extra->point()->pos.y(),
 
96
                                               extra->point()->table);
 
97
      if (!d->Table())
81
98
      {
82
99
        QString tmp( i18n("The expression %1 is not valid") );
83
100
        tmp = tmp.arg( node->getStringLiteral() );
84
101
        context.setException( new KSException( "InvalidTableExpression", tmp ) );
 
102
        delete d;
 
103
        delete extra;
85
104
        return;
86
105
      }
87
106
      depends.append( d );
90
109
    else if ( node->getType() == t_range )
91
110
    {
92
111
      KSParseNodeExtraRange* extra = new KSParseNodeExtraRange( node->getStringLiteral(), m, t );
93
 
      KSpreadDepend* d = new KSpreadDepend;
94
 
      d->m_iColumn = extra->range()->range.left();
95
 
      d->m_iRow = extra->range()->range.top();
96
 
      d->m_iColumn2 = extra->range()->range.right();
97
 
      d->m_iRow2 = extra->range()->range.bottom();
98
 
      d->m_pTable = extra->range()->table;
99
 
      if (!d->m_pTable)
 
112
      KSpreadDependency* d = new KSpreadDependency(extra->range()->range.left(),
 
113
                                                   extra->range()->range.top(),
 
114
                                                   extra->range()->range.right(),
 
115
                                                   extra->range()->range.bottom(),
 
116
                                                   extra->range()->table);
 
117
      if (!d->Table())
100
118
      {
101
119
        QString tmp( i18n("The expression %1 is not valid") );
102
120
        tmp = tmp.arg( node->getStringLiteral() );
103
121
        context.setException( new KSException( "InvalidTableExpression", tmp ) );
 
122
        delete d;
 
123
        delete extra;
104
124
        return;
105
125
      }
106
126
      depends.append( d );
120
140
    makeDepends( context, node->branch5(), m, t, depends );
121
141
}
122
142
 
123
 
/*********************************************************************
124
 
 *
125
 
 * Helper function to avoid problems with rounding floating point
126
 
 * values. Idea for this kind of solution taken from Openoffice.
127
 
 *
128
 
 *********************************************************************/
129
 
 
130
 
static bool approx_equal (double a, double b)
131
 
{
132
 
  if ( a == b )
133
 
    return TRUE;
134
 
  double x = a - b;
135
 
  return (x < 0.0 ? -x : x)  <  ((a < 0.0 ? -a : a) * DBL_EPSILON);
136
 
}
137
 
 
138
 
/*********************************************************************
139
 
 *
140
 
 * Module with global functions like "sin", "cos", "sum" etc.
141
 
 *
142
 
 *********************************************************************/
143
 
 
144
 
static bool kspreadfunc_sin( KSContext& context )
145
 
{
146
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
147
 
 
148
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "sin", true ) )
149
 
    return false;
150
 
  double val=0.0;
151
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
152
 
    {
153
 
      if( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
154
 
        return false;
155
 
    }
156
 
  val=args[0]->doubleValue();
157
 
 
158
 
  context.setValue( new KSValue( sin( val ) ) );
159
 
 
160
 
  return true;
161
 
}
162
 
 
163
 
 
164
 
 
165
 
 
166
 
static bool kspreadfunc_cos( KSContext& context )
167
 
{
168
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
169
 
 
170
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "cos", true ) )
171
 
    return false;
172
 
  double val=0.0;
173
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
174
 
    {
175
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
176
 
        return false;
177
 
    }
178
 
  else
179
 
    val=args[0]->doubleValue();
180
 
 
181
 
  context.setValue( new KSValue( cos( val ) ) );
182
 
 
183
 
  return true;
184
 
}
185
 
 
186
 
static bool kspreadfunc_sqrt( KSContext& context )
187
 
{
188
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
189
 
 
190
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "sqrt", true ) )
191
 
    return false;
192
 
  double val=0.0;
193
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
194
 
    {
195
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
196
 
        return false;
197
 
    }
198
 
  else
199
 
    val=args[0]->doubleValue();
200
 
 
201
 
  context.setValue( new KSValue( sqrt( val ) ) );
202
 
 
203
 
  return true;
204
 
}
205
 
 
206
 
static bool kspreadfunc_sqrtn( KSContext& context )
207
 
{
208
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
209
 
 
210
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "SQRTn", true ) )
211
 
    return false;
212
 
 
213
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
214
 
    return false;
215
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
216
 
    return false;
217
 
 
218
 
  context.setValue( new KSValue( exp( log(args[0]->doubleValue())/args[1]->intValue() ) ) );
219
 
 
220
 
  return true;
221
 
}
222
 
 
223
 
static bool kspreadfunc_cur( KSContext& context )
224
 
{
225
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
226
 
 
227
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "CUR", true ) )
228
 
    return false;
229
 
 
230
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
231
 
    return false;
232
 
 
233
 
  context.setValue( new KSValue( exp( log(args[0]->doubleValue())/3)) );
234
 
 
235
 
  return true;
236
 
}
237
 
 
238
 
 
239
 
static bool kspreadfunc_fabs( KSContext& context )
240
 
{
241
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
242
 
 
243
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "fabs", true ) )
244
 
    return false;
245
 
 
246
 
  double val=0.0;
247
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
248
 
    {
249
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
250
 
        return false;
251
 
    }
252
 
  else
253
 
    val=args[0]->doubleValue();
254
 
 
255
 
  context.setValue( new KSValue( fabs(val ) ) );
256
 
 
257
 
  return true;
258
 
}
259
 
 
260
 
static bool kspreadfunc_tan( KSContext& context )
261
 
{
262
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
263
 
 
264
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "tan", true ) )
265
 
    return false;
266
 
 
267
 
  double val=0.0;
268
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
269
 
    {
270
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
271
 
        return false;
272
 
    }
273
 
  else
274
 
    val=args[0]->doubleValue();
275
 
  context.setValue( new KSValue( tan(val) ) );
276
 
 
277
 
  return true;
278
 
}
279
 
 
280
 
static bool kspreadfunc_exp( KSContext& context )
281
 
{
282
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
283
 
 
284
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "exp",true ) )
285
 
    return false;
286
 
 
287
 
  double val=0.0;
288
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
289
 
    {
290
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
291
 
        return false;
292
 
    }
293
 
  else
294
 
    val=args[0]->doubleValue();
295
 
 
296
 
  context.setValue( new KSValue( exp( val ) ) );
297
 
 
298
 
  return true;
299
 
}
300
 
 
301
 
static bool kspreadfunc_ceil( KSContext& context )
302
 
{
303
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
304
 
 
305
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ceil", true ) )
306
 
    return false;
307
 
 
308
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
309
 
    return false;
310
 
  if (approx_equal(floor(args[0]->doubleValue()),args[0]->doubleValue()))
311
 
    context.setValue( new KSValue(args[0]->doubleValue()));
312
 
  else
313
 
    context.setValue( new KSValue( ceil( args[0]->doubleValue() ) ) );
314
 
 
315
 
  return true;
316
 
}
317
 
 
318
 
static bool kspreadfunc_floor( KSContext& context )
319
 
{
320
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
321
 
 
322
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "floor", true ) )
323
 
    return false;
324
 
 
325
 
  double val=0.0;
326
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
327
 
    {
328
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
329
 
        return false;
330
 
    }
331
 
  else
332
 
    val=args[0]->doubleValue();
333
 
 
334
 
  context.setValue( new KSValue( floor( val ) ) );
335
 
 
336
 
  return true;
337
 
}
338
 
 
339
 
static bool kspreadfunc_atan( KSContext& context )
340
 
{
341
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
342
 
 
343
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "atan", true ) )
344
 
    return false;
345
 
 
346
 
  double val=0.0;
347
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
348
 
    {
349
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
350
 
        return false;
351
 
    }
352
 
  else
353
 
    val=args[0]->doubleValue();
354
 
 
355
 
  context.setValue( new KSValue( atan( val ) ) );
356
 
 
357
 
  return true;
358
 
}
359
 
 
360
 
static bool kspreadfunc_ln( KSContext& context )
361
 
{
362
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
363
 
 
364
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ln", true ) )
365
 
    return false;
366
 
 
367
 
  double val=0.0;
368
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
369
 
    {
370
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
371
 
        return false;
372
 
    }
373
 
  else
374
 
    val=args[0]->doubleValue();
375
 
 
376
 
  context.setValue( new KSValue( log( val ) ) );
377
 
 
378
 
  return true;
379
 
}
380
 
 
381
 
static bool kspreadfunc_logn( KSContext& context )
382
 
{
383
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
384
 
 
385
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "LOGn", true ) )
386
 
    return false;
387
 
 
388
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
389
 
    return false;
390
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
391
 
    return false;
392
 
 
393
 
  context.setValue( new KSValue( log( args[0]->doubleValue() ) /log((double)args[1]->intValue() )) );
394
 
 
395
 
  return true;
396
 
}
397
 
 
398
 
 
399
 
static bool kspreadfunc_asin( KSContext& context )
400
 
{
401
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
402
 
 
403
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "asin", true ) )
404
 
    return false;
405
 
 
406
 
  double val=0.0;
407
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
408
 
    {
409
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
410
 
        return false;
411
 
    }
412
 
  else
413
 
    val=args[0]->doubleValue();
414
 
 
415
 
  context.setValue( new KSValue( asin( val ) ) );
416
 
 
417
 
  return true;
418
 
}
419
 
 
420
 
static bool kspreadfunc_acos( KSContext& context )
421
 
{
422
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
423
 
 
424
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "acos", true ) )
425
 
    return false;
426
 
 
427
 
  double val=0.0;
428
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
429
 
    {
430
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
431
 
        return false;
432
 
    }
433
 
  else
434
 
    val=args[0]->doubleValue();
435
 
 
436
 
 
437
 
  context.setValue( new KSValue( acos( val ) ) );
438
 
 
439
 
  return true;
440
 
}
441
 
 
442
 
static bool kspreadfunc_log( KSContext& context )
443
 
{
444
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
445
 
 
446
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "log", true ) )
447
 
    return false;
448
 
 
449
 
  double val=0.0;
450
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
451
 
    {
452
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
453
 
        return false;
454
 
    }
455
 
  else
456
 
    val=args[0]->doubleValue();
457
 
 
458
 
  context.setValue( new KSValue( log10( val ) ) );
459
 
 
460
 
  return true;
461
 
}
462
 
 
463
 
static bool kspreadfunc_asinh( KSContext& context )
464
 
{
465
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
466
 
 
467
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "asinh", true ) )
468
 
    return false;
469
 
 
470
 
  double val=0.0;
471
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
472
 
    {
473
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
474
 
        return false;
475
 
    }
476
 
  else
477
 
    val=args[0]->doubleValue();
478
 
 
479
 
  context.setValue( new KSValue( asinh( val ) ) );
480
 
 
481
 
  return true;
482
 
}
483
 
 
484
 
static bool kspreadfunc_acosh( KSContext& context )
485
 
{
486
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
487
 
 
488
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "acosh", true ) )
489
 
    return false;
490
 
 
491
 
  double val=0.0;
492
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
493
 
    {
494
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
495
 
        return false;
496
 
    }
497
 
  else
498
 
    val=args[0]->doubleValue();
499
 
 
500
 
  context.setValue( new KSValue( acosh( val ) ) );
501
 
 
502
 
  return true;
503
 
}
504
 
 
505
 
static bool kspreadfunc_atanh( KSContext& context )
506
 
{
507
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
508
 
 
509
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "atanh", true ) )
510
 
    return false;
511
 
 
512
 
  double val=0.0;
513
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
514
 
    {
515
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
516
 
        return false;
517
 
    }
518
 
  else
519
 
    val=args[0]->doubleValue();
520
 
 
521
 
 
522
 
  context.setValue( new KSValue( atanh( val ) ) );
523
 
 
524
 
  return true;
525
 
}
526
 
 
527
 
static bool kspreadfunc_tanh( KSContext& context )
528
 
{
529
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
530
 
 
531
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "tanh", true ) )
532
 
    return false;
533
 
 
534
 
  double val=0.0;
535
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
536
 
    {
537
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
538
 
        return false;
539
 
    }
540
 
  else
541
 
    val=args[0]->doubleValue();
542
 
 
543
 
  context.setValue( new KSValue( tanh( val ) ) );
544
 
 
545
 
  return true;
546
 
}
547
 
 
548
 
static bool kspreadfunc_sinh( KSContext& context )
549
 
{
550
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
551
 
 
552
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "sinh", true ) )
553
 
    return false;
554
 
 
555
 
  double val=0.0;
556
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
557
 
    {
558
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
559
 
        return false;
560
 
    }
561
 
  else
562
 
    val=args[0]->doubleValue();
563
 
 
564
 
  context.setValue( new KSValue( sinh( val ) ) );
565
 
 
566
 
  return true;
567
 
}
568
 
 
569
 
static bool kspreadfunc_cosh( KSContext& context )
570
 
{
571
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
572
 
 
573
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "cosh", true ) )
574
 
    return false;
575
 
 
576
 
  double val=0.0;
577
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
578
 
    {
579
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
580
 
        return false;
581
 
    }
582
 
  else
583
 
    val=args[0]->doubleValue();
584
 
 
585
 
  context.setValue( new KSValue( cosh( val ) ) );
586
 
 
587
 
  return true;
588
 
}
589
 
 
590
 
static bool kspreadfunc_degree( KSContext& context )
591
 
{
592
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
593
 
 
594
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "degree", true ) )
595
 
    return false;
596
 
 
597
 
  double val=0.0;
598
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
599
 
    {
600
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
601
 
        return false;
602
 
    }
603
 
  else
604
 
    val=args[0]->doubleValue();
605
 
 
606
 
  context.setValue( new KSValue( (val*180)/M_PI  ));
607
 
 
608
 
  return true;
609
 
}
610
 
 
611
 
static bool kspreadfunc_radian( KSContext& context )
612
 
{
613
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
614
 
 
615
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "radian", true ) )
616
 
    return false;
617
 
 
618
 
  double val=0.0;
619
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
620
 
    {
621
 
      if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ))
622
 
        return false;
623
 
    }
624
 
  else
625
 
    val=args[0]->doubleValue();
626
 
 
627
 
 
628
 
  context.setValue( new KSValue( (val*M_PI )/180  ));
629
 
 
630
 
  return true;
631
 
}
632
 
 
633
 
 
634
 
static bool kspreadfunc_sum_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
635
 
{
636
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
637
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
638
 
 
639
 
  for( ; it != end; ++it )
640
 
  {
641
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
642
 
    {
643
 
      if ( !kspreadfunc_sum_helper( context, (*it)->listValue(), result ) )
644
 
        return false;
645
 
    }
646
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
647
 
      {
648
 
      result += (*it)->doubleValue();
649
 
      }
650
 
    /*else if ( !KSUtil::checkType( context, *it, KSValue::Empty, true ) )
651
 
      return false;*/
652
 
  }
653
 
 
654
 
  return true;
655
 
}
656
 
 
657
 
static bool kspreadfunc_sum( KSContext& context )
658
 
{
659
 
  double result = 0.0;
660
 
  bool b = kspreadfunc_sum_helper( context, context.value()->listValue(), result );
661
 
 
662
 
  if ( b )
663
 
    context.setValue( new KSValue( result ) );
664
 
 
665
 
  return b;
666
 
}
667
 
 
668
 
static bool kspreadfunc_sumsq_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
669
 
{
670
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
671
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
672
 
 
673
 
  for( ; it != end; ++it )
674
 
  {
675
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
676
 
    {
677
 
      if ( !kspreadfunc_sum_helper( context, (*it)->listValue(), result ) )
678
 
        return false;
679
 
    }
680
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
681
 
      {
682
 
      result += ((*it)->doubleValue()*(*it)->doubleValue());
683
 
      }
684
 
    else if ( !KSUtil::checkType( context, *it, KSValue::Empty, true ) )
685
 
      return false;
686
 
  }
687
 
 
688
 
  return true;
689
 
}
690
 
 
691
 
static bool kspreadfunc_sumsq( KSContext& context )
692
 
{
693
 
  double result = 0.0;
694
 
  bool b = kspreadfunc_sumsq_helper( context, context.value()->listValue(), result );
695
 
 
696
 
  if ( b )
697
 
    context.setValue( new KSValue( result ) );
698
 
 
699
 
  return b;
700
 
}
701
 
 
702
 
 
703
 
static bool kspreadfunc_max_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& inter)
704
 
{
705
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
706
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
707
 
 
708
 
  for( ; it != end; ++it )
709
 
  {
710
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
711
 
    {
712
 
 
713
 
      if ( !kspreadfunc_max_helper( context, (*it)->listValue(), result,inter ) )
714
 
        return false;
715
 
    }
716
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
717
 
      {
718
 
      if(inter == 0)
719
 
      {
720
 
        result=(*it)->doubleValue();
721
 
        inter=1;
722
 
      }
723
 
      if(result <  (*it)->doubleValue())
724
 
        result =(*it)->doubleValue();
725
 
    }
726
 
  }
727
 
 
728
 
  return true;
729
 
}
730
 
 
731
 
static bool kspreadfunc_max( KSContext& context )
732
 
{
733
 
  double result = 0.0;
734
 
 
735
 
  //init first element
736
 
  int inter=0;
737
 
 
738
 
  bool b = kspreadfunc_max_helper( context, context.value()->listValue(), result ,inter );
739
 
 
740
 
  if ( b )
741
 
    context.setValue( new KSValue( result ) );
742
 
 
743
 
  return b;
744
 
}
745
 
 
746
 
static bool kspreadfunc_min_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& inter)
747
 
{
748
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
749
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
750
 
 
751
 
  for( ; it != end; ++it )
752
 
  {
753
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
754
 
    {
755
 
 
756
 
      if ( !kspreadfunc_min_helper( context, (*it)->listValue(), result,inter ) )
757
 
        return false;
758
 
    }
759
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
760
 
      {
761
 
      if(inter == 0)
762
 
      {
763
 
        result=(*it)->doubleValue();
764
 
        inter=1;
765
 
      }
766
 
      if(result >  (*it)->doubleValue())
767
 
        result =(*it)->doubleValue();
768
 
    }
769
 
  }
770
 
 
771
 
  return true;
772
 
}
773
 
 
774
 
static bool kspreadfunc_min( KSContext& context )
775
 
{
776
 
  double result = 0.0;
777
 
 
778
 
  //init first element
779
 
  int inter=0;
780
 
 
781
 
  bool b = kspreadfunc_min_helper( context, context.value()->listValue(), result ,inter );
782
 
 
783
 
  if ( b )
784
 
    context.setValue( new KSValue( result ) );
785
 
 
786
 
  return b;
787
 
}
788
 
 
789
 
static bool kspreadfunc_average_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,int& number)
790
 
{
791
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
792
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
793
 
 
794
 
  for( ; it != end; ++it )
795
 
  {
796
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
797
 
    {
798
 
      if ( !kspreadfunc_average_helper( context, (*it)->listValue(), result ,number) )
799
 
        return false;
800
 
    }
801
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
802
 
      {
803
 
      result += (*it)->doubleValue();
804
 
      number++;
805
 
      }
806
 
  }
807
 
 
808
 
  return true;
809
 
}
810
 
 
811
 
static bool kspreadfunc_average( KSContext& context )
812
 
{
813
 
  double result = 0.0;
814
 
 
815
 
  int number=0;
816
 
  bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
817
 
 
818
 
  if ( number == 0 )
819
 
    {
820
 
    context.setValue( new KSValue( i18n("#DIV/0") ) );
821
 
    return true;
822
 
    }
823
 
 
824
 
  if ( b )
825
 
    context.setValue( new KSValue( result / (double)number ) );
826
 
 
827
 
  return b;
828
 
}
829
 
 
830
 
static bool kspreadfunc_variance_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result, double avera)
831
 
{
832
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
833
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
834
 
 
835
 
  for( ; it != end; ++it )
836
 
  {
837
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
838
 
    {
839
 
      if ( !kspreadfunc_variance_helper( context, (*it)->listValue(), result ,avera) )
840
 
        return false;
841
 
    }
842
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
843
 
      {
844
 
      result += ( (*it)->doubleValue() - avera ) * ( (*it)->doubleValue() - avera );
845
 
      }
846
 
  }
847
 
 
848
 
  return true;
849
 
}
850
 
 
851
 
static bool kspreadfunc_variance( KSContext& context )
852
 
{
853
 
  double result = 0.0;
854
 
  double avera = 0.0;
855
 
  int number = 0;
856
 
  bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
857
 
 
858
 
  if ( number == 0 )
859
 
      return false;
860
 
 
861
 
  if ( b )
862
 
  {
863
 
    avera = result / (double)number;
864
 
    result = 0.0;
865
 
    bool b = kspreadfunc_variance_helper( context, context.value()->listValue(), result, avera );
866
 
    if(b)
867
 
      context.setValue( new KSValue(result / (double)number ) );
868
 
  }
869
 
 
870
 
  return b;
871
 
}
872
 
 
873
 
static bool kspreadfunc_stddev_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result,double& avera)
874
 
{
875
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
876
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
877
 
 
878
 
  for( ; it != end; ++it )
879
 
  {
880
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
881
 
    {
882
 
      if ( !kspreadfunc_stddev_helper( context, (*it)->listValue(), result ,avera) )
883
 
        return false;
884
 
 
885
 
    }
886
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
887
 
    {
888
 
      result += (((*it)->doubleValue()-avera)*((*it)->doubleValue()-avera));
889
 
    }
890
 
  }
891
 
 
892
 
  return true;
893
 
}
894
 
 
895
 
static bool kspreadfunc_stddev( KSContext& context )
896
 
{
897
 
  double result = 0.0;
898
 
  double avera = 0.0;
899
 
  int number=0;
900
 
  bool b = kspreadfunc_average_helper( context, context.value()->listValue(), result ,number);
901
 
 
902
 
  if ( number == 0 )
903
 
      return false;
904
 
 
905
 
  if ( b )
906
 
  {
907
 
    avera=result/number;
908
 
    result=0.0;
909
 
    bool b = kspreadfunc_stddev_helper( context, context.value()->listValue(), result,avera );
910
 
    if(b)
911
 
      context.setValue( new KSValue(sqrt(result/number )) );
912
 
  }
913
 
 
914
 
  return b;
915
 
}
916
 
 
917
 
static bool kspreadfunc_mult_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result )
918
 
{
919
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
920
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
921
 
 
922
 
  for( ; it != end; ++it )
923
 
  {
924
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
925
 
    {
926
 
      if ( !kspreadfunc_mult_helper( context, (*it)->listValue(), result ) )
927
 
        return false;
928
 
    }
929
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
930
 
      result *= (*it)->doubleValue();
931
 
  }
932
 
 
933
 
  return true;
934
 
}
935
 
 
936
 
static bool kspreadfunc_mult( KSContext& context )
937
 
{
938
 
  double result = 1.0;
939
 
  bool b = kspreadfunc_mult_helper( context, context.value()->listValue(), result );
940
 
 
941
 
  if ( b )
942
 
    context.setValue( new KSValue( result ) );
943
 
 
944
 
  return b;
945
 
}
946
 
 
947
 
 
948
 
static bool kspreadfunc_join_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& tmp )
949
 
{
950
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
951
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
952
 
  QString tmp2;
953
 
  for( ; it != end; ++it )
954
 
  {
955
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
956
 
    {
957
 
      if ( !kspreadfunc_join_helper( context, (*it)->listValue(), tmp ) )
958
 
        return false;
959
 
    }
960
 
    else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
961
 
      tmp+= (*it)->stringValue();
962
 
    else if( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
963
 
      tmp+= KGlobal::locale()->formatNumber((*it)->doubleValue());
964
 
    else
965
 
      return false;
966
 
  }
967
 
  return true;
968
 
}
969
 
 
970
 
static bool kspreadfunc_join( KSContext& context )
971
 
{
972
 
  QString tmp;
973
 
  bool b = kspreadfunc_join_helper( context, context.value()->listValue(), tmp );
974
 
 
975
 
  if ( b )
976
 
    context.setValue( new KSValue( tmp ) );
977
 
 
978
 
  return b;
979
 
}
980
 
 
981
 
static bool kspreadfunc_not( KSContext& context )
982
 
{
983
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
984
 
 
985
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "NOT", true ) || !KSUtil::checkArgumentsCount( context, 1, "not", true ) )
986
 
    return false;
987
 
 
988
 
  if ( !KSUtil::checkType( context, args[0], KSValue::BoolType, true ) )
989
 
    return false;
990
 
 
991
 
  bool toto = !args[0]->boolValue();
992
 
  context.setValue( new KSValue(toto));
993
 
  return true;
994
 
}
995
 
 
996
 
 
997
 
static bool kspreadfunc_or_helper( KSContext& context, QValueList<KSValue::Ptr>& args, bool& first )
998
 
{
999
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
1000
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
1001
 
 
1002
 
  for( ; it != end; ++it )
1003
 
  {
1004
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
1005
 
    {
1006
 
      if ( !kspreadfunc_or_helper( context, (*it)->listValue(), first ) )
1007
 
        return false;
1008
 
    }
1009
 
    else if ( KSUtil::checkType( context, *it, KSValue::BoolType, true ) )
1010
 
      first = (first || (*it)->boolValue());
1011
 
    else
1012
 
      return false;
1013
 
  }
1014
 
 
1015
 
  return true;
1016
 
}
1017
 
 
1018
 
static bool kspreadfunc_or( KSContext& context )
1019
 
{
1020
 
  bool first = false;
1021
 
  bool b = kspreadfunc_or_helper( context, context.value()->listValue(), first );
1022
 
 
1023
 
  if ( b )
1024
 
    context.setValue( new KSValue( first ) );
1025
 
 
1026
 
  return b;
1027
 
}
1028
 
 
1029
 
static bool kspreadfunc_nor( KSContext& context )
1030
 
{
1031
 
  bool first = false;
1032
 
  bool b = kspreadfunc_or_helper( context, context.value()->listValue(), first );
1033
 
 
1034
 
  if ( b )
1035
 
    context.setValue( new KSValue( !first ) );
1036
 
 
1037
 
  return b;
1038
 
}
1039
 
 
1040
 
 
1041
 
static bool kspreadfunc_and_helper( KSContext& context, QValueList<KSValue::Ptr>& args, bool& first )
1042
 
{
1043
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
1044
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
1045
 
 
1046
 
  for( ; it != end; ++it )
1047
 
  {
1048
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
1049
 
    {
1050
 
      if ( !kspreadfunc_and_helper( context, (*it)->listValue(), first ) )
1051
 
        return false;
1052
 
    }
1053
 
    else if ( KSUtil::checkType( context, *it, KSValue::BoolType, true ) )
1054
 
      first = first && (*it)->boolValue();
1055
 
    else
1056
 
      return false;
1057
 
  }
1058
 
 
1059
 
  return true;
1060
 
}
1061
 
 
1062
 
static bool kspreadfunc_and( KSContext& context )
1063
 
{
1064
 
  bool first = true;
1065
 
  bool b = kspreadfunc_and_helper( context, context.value()->listValue(), first );
1066
 
 
1067
 
  if ( b )
1068
 
    context.setValue( new KSValue( first ) );
1069
 
 
1070
 
  return b;
1071
 
}
1072
 
 
1073
 
static bool kspreadfunc_nand( KSContext& context )
1074
 
{
1075
 
  bool first = true;
1076
 
  bool b = kspreadfunc_and_helper( context, context.value()->listValue(), first );
1077
 
 
1078
 
  if ( b )
1079
 
    context.setValue( new KSValue( !first ) );
1080
 
 
1081
 
  return b;
1082
 
}
1083
 
 
1084
 
 
1085
 
static bool kspreadfunc_if( KSContext& context )
1086
 
{
1087
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1088
 
 
1089
 
    if ( !KSUtil::checkArgumentsCount( context, 3, "if", true ) || !KSUtil::checkArgumentsCount( context, 3, "IF", true ))
1090
 
      return false;
1091
 
 
1092
 
    if ( !KSUtil::checkType( context, args[0], KSValue::BoolType, true ) )
1093
 
      return false;
1094
 
 
1095
 
    if (  args[0]->boolValue() == true )
1096
 
      context.setValue( new KSValue( *(args[1]) ) );
1097
 
    else
1098
 
      context.setValue( new KSValue( *(args[2]) ) );
1099
 
 
1100
 
    return true;
1101
 
}
1102
 
 
1103
 
static bool kspreadfunc_left( KSContext& context )
1104
 
{
1105
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1106
 
 
1107
 
    if ( !KSUtil::checkArgumentsCount( context, 2, "left", true ) )
1108
 
      return false;
1109
 
 
1110
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1111
 
      return false;
1112
 
    int nb;
1113
 
    if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1114
 
      nb = (int) args[1]->doubleValue();
1115
 
    else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1116
 
      nb = args[1]->intValue();
1117
 
    else
1118
 
      return false;
1119
 
 
1120
 
    QString tmp = args[0]->stringValue().left(nb);
1121
 
    context.setValue( new KSValue( tmp ) );
1122
 
    return true;
1123
 
}
1124
 
 
1125
 
static bool kspreadfunc_right( KSContext& context )
1126
 
{
1127
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1128
 
 
1129
 
    if ( !KSUtil::checkArgumentsCount( context, 2, "right", true ) )
1130
 
      return false;
1131
 
 
1132
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1133
 
      return false;
1134
 
    int nb;
1135
 
    if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1136
 
      nb = (int) args[1]->doubleValue();
1137
 
    else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1138
 
      nb = args[1]->intValue();
1139
 
    else
1140
 
      return false;
1141
 
 
1142
 
    QString tmp = args[0]->stringValue().right(nb);
1143
 
    context.setValue( new KSValue(tmp));
1144
 
    return true;
1145
 
}
1146
 
 
1147
 
static bool kspreadfunc_upper( KSContext& context )
1148
 
{
1149
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1150
 
 
1151
 
    if ( !KSUtil::checkArgumentsCount( context, 1, "upper", true ) )
1152
 
      return false;
1153
 
 
1154
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1155
 
      return false;
1156
 
 
1157
 
    QString tmp = args[0]->stringValue().upper();
1158
 
    context.setValue( new KSValue( tmp ) );
1159
 
    return true;
1160
 
}
1161
 
 
1162
 
static bool kspreadfunc_lower( KSContext& context )
1163
 
{
1164
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1165
 
 
1166
 
    if ( !KSUtil::checkArgumentsCount( context, 1, "lower", true ) )
1167
 
      return false;
1168
 
 
1169
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1170
 
      return false;
1171
 
 
1172
 
    QString tmp = args[0]->stringValue().lower();
1173
 
    context.setValue( new KSValue( tmp ) );
1174
 
    return true;
1175
 
}
1176
 
 
1177
 
static bool kspreadfunc_find( KSContext& context )
1178
 
{
1179
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1180
 
 
1181
 
    if ( !KSUtil::checkArgumentsCount( context, 2, "find", true ) )
1182
 
      return false;
1183
 
 
1184
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1185
 
      return false;
1186
 
 
1187
 
    if ( !KSUtil::checkType( context, args[1], KSValue::StringType, true ) )
1188
 
      return false;
1189
 
 
1190
 
    QString string_find = args[0]->stringValue();
1191
 
    QString string = args[1]->stringValue();
1192
 
    bool exist=(string.find(string_find)!=-1)?true:false;
1193
 
    context.setValue( new KSValue( exist ) );
1194
 
    return true;
1195
 
}
1196
 
 
1197
 
 
1198
 
 
1199
 
static bool kspreadfunc_mid( KSContext& context )
1200
 
{
1201
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1202
 
 
1203
 
    uint len = 0xffffffff;
1204
 
    if ( KSUtil::checkArgumentsCount( context, 3, "mid", false ) )
1205
 
    {
1206
 
      if( KSUtil::checkType( context, args[2], KSValue::DoubleType, false ) )
1207
 
        len = (uint) args[2]->doubleValue();
1208
 
      else if( KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1209
 
        len = (uint) args[2]->intValue();
1210
 
      else
1211
 
        return false;
1212
 
    }
1213
 
    else if ( !KSUtil::checkArgumentsCount( context, 2, "mid", true ) )
1214
 
      return false;
1215
 
 
1216
 
    if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1217
 
      return false;
1218
 
    int pos;
1219
 
    if( KSUtil::checkType( context, args[1], KSValue::DoubleType, false ) )
1220
 
      pos = (int) args[1]->doubleValue();
1221
 
    else if( KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1222
 
      pos = args[1]->intValue();
1223
 
    else
1224
 
      return false;
1225
 
 
1226
 
    QString tmp = args[0]->stringValue().mid( pos, len );
1227
 
    context.setValue( new KSValue(tmp));
1228
 
    return true;
1229
 
}
1230
 
 
1231
 
static bool kspreadfunc_len( KSContext& context )
1232
 
{
1233
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1234
 
 
1235
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "len", true ) )
1236
 
    return false;
1237
 
 
1238
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1239
 
    return false;
1240
 
 
1241
 
  int nb=args[0]->stringValue().length();
1242
 
  context.setValue( new KSValue(nb));
1243
 
  return true;
1244
 
}
1245
 
 
1246
 
static bool kspreadfunc_EXACT( KSContext& context )
1247
 
{
1248
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1249
 
 
1250
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "EXACT", true ) )
1251
 
    return false;
1252
 
 
1253
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1254
 
    return false;
1255
 
  if ( !KSUtil::checkType( context, args[1], KSValue::StringType, true ) )
1256
 
    return false;
1257
 
  bool exact = args[1]->stringValue() == args[0]->stringValue();
1258
 
  context.setValue( new KSValue(exact) );
1259
 
  return true;
1260
 
}
1261
 
 
1262
 
 
1263
 
 
1264
 
static bool kspreadfunc_INT( KSContext& context )
1265
 
{
1266
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1267
 
 
1268
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "INT", true ) )
1269
 
    return false;
1270
 
  double val=0.0;
1271
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1272
 
    {
1273
 
    if(!KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
1274
 
      return false;
1275
 
    }
1276
 
  else
1277
 
    val=args[0]->doubleValue();
1278
 
 
1279
 
  context.setValue( new KSValue((int)val));
1280
 
  return true;
1281
 
}
1282
 
 
1283
 
static bool kspreadfunc_PI( KSContext& context )
1284
 
{
1285
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1286
 
 
1287
 
    if ( !KSUtil::checkArgumentsCount( context, 0, "PI", true ) )
1288
 
      return false;
1289
 
 
1290
 
    context.setValue( new KSValue(M_PI));
1291
 
    return true;
1292
 
}
1293
 
 
1294
 
static bool kspreadfunc_rand( KSContext& context )
1295
 
{
1296
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1297
 
 
1298
 
    if ( !KSUtil::checkArgumentsCount( context, 0, "rand", true ) )
1299
 
      return false;
1300
 
 
1301
 
    context.setValue( new KSValue((double) rand()/(RAND_MAX + 1.0)));
1302
 
    return true;
1303
 
}
1304
 
 
1305
 
static bool kspreadfunc_randbetween( KSContext& context )
1306
 
{
1307
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
1308
 
 
1309
 
    if ( !KSUtil::checkArgumentsCount( context, 2, "RANDBETWEEN", true ) )
1310
 
      return false;
1311
 
    if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1312
 
        return false;
1313
 
    if( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1314
 
        return false;
1315
 
    if(args[0]->intValue()>args[1]->intValue())
1316
 
        {
1317
 
        context.setValue( new KSValue(i18n("Err")));
1318
 
        return true;
1319
 
        }
1320
 
 
1321
 
    context.setValue( new KSValue((double)(((double)args[1]->intValue()-(double)args[0]->intValue())*rand()/RAND_MAX+((double)args[0]->intValue()))));
1322
 
 
1323
 
    return true;
1324
 
}
1325
 
 
1326
 
 
1327
 
static bool kspreadfunc_REPT( KSContext& context )
1328
 
{
1329
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1330
 
 
1331
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "REPT", true ) )
1332
 
    return false;
1333
 
 
1334
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1335
 
    return false;
1336
 
  if( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1337
 
    return false;
1338
 
 
1339
 
  int nb=(int) args[1]->doubleValue();
1340
 
  QString tmp=args[0]->stringValue();
1341
 
  QString tmp1;
1342
 
  for (int i=0 ;i<nb;i++)
1343
 
    tmp1+=tmp;
1344
 
  context.setValue( new KSValue(tmp1));
1345
 
  return true;
1346
 
}
1347
 
 
1348
 
static bool kspreadfunc_islogic( KSContext& context )
1349
 
{
1350
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1351
 
 
1352
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISLOGIC", true ) )
1353
 
    return false;
1354
 
 
1355
 
  bool logic = KSUtil::checkType( context, args[0], KSValue::BoolType, true );
1356
 
  context.setValue( new KSValue(logic));
1357
 
  return true;
1358
 
}
1359
 
 
1360
 
static bool kspreadfunc_istext( KSContext& context )
1361
 
{
1362
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1363
 
 
1364
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISTEXT", true ) )
1365
 
    return false;
1366
 
 
1367
 
  bool logic = KSUtil::checkType( context, args[0], KSValue::StringType, true );
1368
 
  context.setValue( new KSValue(logic));
1369
 
  return true;
1370
 
}
1371
 
 
1372
 
static bool kspreadfunc_isnottext( KSContext& context )
1373
 
{
1374
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1375
 
 
1376
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISNOTTEXT", true ) )
1377
 
    return false;
1378
 
 
1379
 
  bool logic = !KSUtil::checkType( context, args[0], KSValue::StringType, true );
1380
 
  context.setValue( new KSValue(logic));
1381
 
  return true;
1382
 
}
1383
 
 
1384
 
static bool kspreadfunc_isnum( KSContext& context )
1385
 
{
1386
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1387
 
 
1388
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISNUM", true ) )
1389
 
    return false;
1390
 
 
1391
 
  bool logic = KSUtil::checkType( context, args[0], KSValue::DoubleType, true );
1392
 
  context.setValue( new KSValue(logic));
1393
 
  return true;
1394
 
}
1395
 
 
1396
 
static bool kspreadfunc_istime( KSContext& context )
1397
 
{
1398
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1399
 
 
1400
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISTIME", true ) )
1401
 
    return false;
1402
 
 
1403
 
  bool logic = KSUtil::checkType( context, args[0], KSValue::TimeType, true );
1404
 
  context.setValue( new KSValue(logic));
1405
 
  return true;
1406
 
}
1407
 
 
1408
 
static bool kspreadfunc_isdate( KSContext& context )
1409
 
{
1410
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1411
 
 
1412
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "ISDATE", true ) )
1413
 
    return false;
1414
 
 
1415
 
  bool logic = KSUtil::checkType( context, args[0], KSValue::DateType, true );
1416
 
  context.setValue( new KSValue(logic));
1417
 
  return true;
1418
 
}
1419
 
 
1420
 
 
1421
 
static bool kspreadfunc_pow( KSContext& context )
1422
 
{
1423
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1424
 
 
1425
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "pow",true ) )
1426
 
    return false;
1427
 
 
1428
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1429
 
    return false;
1430
 
 
1431
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1432
 
    return false;
1433
 
 
1434
 
  context.setValue( new KSValue( pow( args[0]->doubleValue(),args[1]->doubleValue() ) ) );
1435
 
 
1436
 
  return true;
1437
 
}
1438
 
 
1439
 
static bool kspreadfunc_mod( KSContext& context )
1440
 
{
1441
 
  double result=0;
1442
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1443
 
 
1444
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "MOD",true ) )
1445
 
    return false;
1446
 
 
1447
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1448
 
    return false;
1449
 
 
1450
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1451
 
    return false;
1452
 
  if( (int)args[1]->doubleValue()!=0)
1453
 
  {
1454
 
        result=(int)args[0]->doubleValue() % (int)args[1]->doubleValue();
1455
 
        context.setValue( new KSValue(  result  ) );
1456
 
  }
1457
 
  else
1458
 
  {
1459
 
        context.setValue( new KSValue( i18n("#DIV/0") ) );
1460
 
 
1461
 
  }
1462
 
  return true;
1463
 
}
1464
 
 
1465
 
static bool kspreadfunc_hours( KSContext& context )
1466
 
{
1467
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1468
 
  if ( !KSUtil::checkArgumentsCount( context,1, "hours",true ) )
1469
 
    return false;
1470
 
  if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1471
 
    {
1472
 
      if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1473
 
        return false;
1474
 
      QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1475
 
      if(tmp.isValid())
1476
 
        context.setValue(new KSValue(tmp.hour()));
1477
 
      else
1478
 
        context.setValue(new KSValue(i18n("Err")));
1479
 
      return true;
1480
 
    }
1481
 
  else
1482
 
    {
1483
 
      QTime tmp=args[0]->timeValue();
1484
 
      context.setValue(new KSValue(tmp.hour()));
1485
 
    }
1486
 
   return true;
1487
 
}
1488
 
 
1489
 
static bool kspreadfunc_minutes( KSContext& context )
1490
 
{
1491
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1492
 
  if ( !KSUtil::checkArgumentsCount( context,1, "minutes",true ) )
1493
 
    return false;
1494
 
  if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1495
 
    {
1496
 
      if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1497
 
        return false;
1498
 
      QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1499
 
      if(tmp.isValid())
1500
 
        context.setValue(new KSValue(tmp.minute()));
1501
 
      else
1502
 
        context.setValue(new KSValue(i18n("Err")));
1503
 
      return true;
1504
 
    }
1505
 
  else
1506
 
    {
1507
 
      QTime tmp=args[0]->timeValue();
1508
 
      context.setValue(new KSValue(tmp.minute()));
1509
 
    }
1510
 
   return true;
1511
 
}
1512
 
 
1513
 
static bool kspreadfunc_seconds( KSContext& context )
1514
 
{
1515
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1516
 
  if ( !KSUtil::checkArgumentsCount( context,1, "seconds",true ) )
1517
 
    return false;
1518
 
  if (!KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
1519
 
    {
1520
 
      if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
1521
 
        return false;
1522
 
      QTime tmp=KGlobal::locale()->readTime(args[0]->stringValue());
1523
 
      if(tmp.isValid())
1524
 
        context.setValue(new KSValue(tmp.second()));
1525
 
      else
1526
 
        context.setValue(new KSValue(i18n("Err")));
1527
 
      return true;
1528
 
    }
1529
 
  else
1530
 
    {
1531
 
      QTime tmp=args[0]->timeValue();
1532
 
      context.setValue(new KSValue(tmp.second()));
1533
 
    }
1534
 
   return true;
1535
 
}
1536
 
 
1537
 
static bool kspreadfunc_date( KSContext& context )
1538
 
{
1539
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1540
 
 
1541
 
  if ( !KSUtil::checkArgumentsCount( context,3, "date",true ) )
1542
 
    return false;
1543
 
 
1544
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1545
 
    return false;
1546
 
 
1547
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1548
 
    return false;
1549
 
 
1550
 
  if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1551
 
    return false;
1552
 
 
1553
 
  QDate _date;
1554
 
  if( _date.setYMD(args[0]->intValue(), args[1]->intValue(), args[2]->intValue()) )
1555
 
    context.setValue( new KSValue(KGlobal::locale()->formatDate(_date)));
1556
 
  else
1557
 
    context.setValue( new KSValue(QString(i18n("Err"))) );
1558
 
 
1559
 
  return true;
1560
 
}
1561
 
 
1562
 
static bool kspreadfunc_day( KSContext& context )
1563
 
{
1564
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1565
 
  QString tmp;
1566
 
  if ( !KSUtil::checkArgumentsCount( context,1, "day",true ) )
1567
 
    return false;
1568
 
 
1569
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1570
 
    return false;
1571
 
 
1572
 
  if(KGlobal::locale()->weekDayName(args[0]->intValue()).isNull())
1573
 
        tmp=i18n("Err");
1574
 
  else
1575
 
        tmp= KGlobal::locale()->weekDayName(args[0]->intValue());
1576
 
 
1577
 
  //context.setValue( new KSValue(KGlobal::locale()->weekDayName(args[0]->intValue())));
1578
 
  context.setValue( new KSValue(tmp));
1579
 
  return true;
1580
 
}
1581
 
 
1582
 
static bool kspreadfunc_month( KSContext& context )
1583
 
{
1584
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1585
 
  QString tmp;
1586
 
  if ( !KSUtil::checkArgumentsCount( context,1, "month",true ) )
1587
 
    return false;
1588
 
 
1589
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1590
 
    return false;
1591
 
 
1592
 
  if(KGlobal::locale()->monthName(args[0]->intValue()).isNull())
1593
 
        tmp=i18n("Err");
1594
 
  else
1595
 
        tmp=KGlobal::locale()->monthName(args[0]->intValue());
1596
 
 
1597
 
  context.setValue( new KSValue(tmp));
1598
 
  //context.setValue( new KSValue(KGlobal::locale()->monthName(args[0]->intValue())));
1599
 
 
1600
 
  return true;
1601
 
}
1602
 
 
1603
 
static bool kspreadfunc_time( KSContext& context )
1604
 
{
1605
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1606
 
 
1607
 
  if ( !KSUtil::checkArgumentsCount( context,3, "time",true ) )
1608
 
    return false;
1609
 
 
1610
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1611
 
    return false;
1612
 
 
1613
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1614
 
    return false;
1615
 
 
1616
 
  if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1617
 
    return false;
1618
 
 
1619
 
  context.setValue( new KSValue(KGlobal::locale()->formatTime(
1620
 
    QTime(args[0]->intValue(),
1621
 
          args[1]->intValue(),args[2]->intValue()),true )));
1622
 
 
1623
 
  return true;
1624
 
}
1625
 
 
1626
 
static bool kspreadfunc_currentDate( KSContext& context )
1627
 
{
1628
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1629
 
 
1630
 
    if ( !KSUtil::checkArgumentsCount( context,0, "currentDate",true ) )
1631
 
      return false;
1632
 
 
1633
 
    context.setValue( new KSValue(KGlobal::locale()->formatDate(QDate::currentDate())));
1634
 
 
1635
 
    return true;
1636
 
}
1637
 
 
1638
 
static bool kspreadfunc_shortcurrentDate( KSContext& context )
1639
 
{
1640
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1641
 
 
1642
 
    if ( !KSUtil::checkArgumentsCount( context,0, "shortcurrentDate",true ) )
1643
 
      return false;
1644
 
 
1645
 
    context.setValue( new KSValue(KGlobal::locale()->formatDate(QDate::currentDate(),true)));
1646
 
 
1647
 
    return true;
1648
 
}
1649
 
 
1650
 
 
1651
 
static bool kspreadfunc_currentTime( KSContext& context )
1652
 
{
1653
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1654
 
 
1655
 
    if ( !KSUtil::checkArgumentsCount( context,0, "currentTime",true ) )
1656
 
      return false;
1657
 
 
1658
 
    context.setValue( new KSValue(KGlobal::locale()->formatTime(QTime::currentTime())));
1659
 
 
1660
 
    return true;
1661
 
}
1662
 
 
1663
 
static bool kspreadfunc_currentDateTime( KSContext& context )
1664
 
{
1665
 
    // QValueList<KSValue::Ptr>& args = context.value()->listValue();
1666
 
 
1667
 
    if ( !KSUtil::checkArgumentsCount( context,0, "currentDateTime",true ) )
1668
 
      return false;
1669
 
 
1670
 
    context.setValue( new KSValue(KGlobal::locale()->formatDateTime(QDateTime::currentDateTime())));
1671
 
 
1672
 
    return true;
1673
 
}
1674
 
 
1675
 
static bool kspreadfunc_dayOfYear( KSContext& context )
1676
 
{
1677
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1678
 
 
1679
 
  if ( !KSUtil::checkArgumentsCount( context,3, "dayOfYear",true ) )
1680
 
    return false;
1681
 
 
1682
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1683
 
    return false;
1684
 
 
1685
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1686
 
    return false;
1687
 
 
1688
 
  if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
1689
 
    return false;
1690
 
 
1691
 
  context.setValue( new KSValue(QDate(args[0]->intValue(),
1692
 
                                      args[1]->intValue(),args[2]->intValue()).dayOfYear() ));
1693
 
 
1694
 
  return true;
1695
 
}
1696
 
 
1697
 
static double fact( double val, double end )
1698
 
{
1699
 
    /* fact =i*(i-1)*(i-2)*...*1 */
1700
 
    if(val <0||end<0)
1701
 
        return (-1);
1702
 
    if(val==0)
1703
 
        return(1);
1704
 
    else if(val==end)
1705
 
        return(1);
1706
 
    /*val==end => you don't multiplie it */
1707
 
    else
1708
 
        return(val*fact((double)(val-1),end));
1709
 
 
1710
 
}
1711
 
 
1712
 
static bool kspreadfunc_fact( KSContext& context )
1713
 
{
1714
 
  double result;
1715
 
  QString tmp;
1716
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1717
 
 
1718
 
  if ( !KSUtil::checkArgumentsCount( context,1, "fact",true ) )
1719
 
    return false;
1720
 
 
1721
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1722
 
    return false;
1723
 
 
1724
 
  result=fact((double)args[0]->intValue(),0);
1725
 
  //In fact function val must be positive
1726
 
  tmp=i18n("Err");
1727
 
  if(result==-1)
1728
 
        context.setValue( new KSValue(tmp));
1729
 
  else
1730
 
        context.setValue( new KSValue(result ));
1731
 
 
1732
 
  return true;
1733
 
}
1734
 
 
1735
 
static bool kspreadfunc_arrang( KSContext& context )
1736
 
{ /* arrang : fact(n)/(fact(n-m) */
1737
 
  double result;
1738
 
  QString tmp;
1739
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1740
 
 
1741
 
  if ( !KSUtil::checkArgumentsCount( context,2, "PERMUT",true ) )
1742
 
    return false;
1743
 
 
1744
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1745
 
    return false;
1746
 
 
1747
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1748
 
    return false;
1749
 
 
1750
 
  tmp=i18n("Err");
1751
 
  if((double)args[0]->intValue()<(double)args[1]->intValue())
1752
 
          context.setValue( new KSValue(tmp ));
1753
 
 
1754
 
  else if((double)args[1]->intValue()<0)
1755
 
          context.setValue( new KSValue(tmp ));
1756
 
 
1757
 
  else
1758
 
        {
1759
 
        result=fact((double)args[0]->intValue(),
1760
 
        ((double)args[0]->intValue()-(double)args[1]->intValue()));
1761
 
        //In fact function val must be positive
1762
 
 
1763
 
        if(result==-1)
1764
 
                context.setValue( new KSValue(tmp));
1765
 
        else
1766
 
                context.setValue( new KSValue(result ));
1767
 
        }
1768
 
  return true;
1769
 
}
1770
 
 
1771
 
static bool kspreadfunc_combin( KSContext& context )
1772
 
{ /*combin : fact(n)/(fact(n-m)*fact(m)) */
1773
 
  double result;
1774
 
  QString tmp;
1775
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1776
 
 
1777
 
  if ( !KSUtil::checkArgumentsCount( context,2, "COMBIN",true ) )
1778
 
    return false;
1779
 
 
1780
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
1781
 
    return false;
1782
 
 
1783
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
1784
 
    return false;
1785
 
 
1786
 
  tmp=i18n("Err");
1787
 
  if((double)args[0]->intValue()<(double)args[1]->intValue())
1788
 
          context.setValue( new KSValue(tmp ));
1789
 
 
1790
 
  else if((double)args[1]->intValue()<0)
1791
 
          context.setValue( new KSValue(tmp ));
1792
 
 
1793
 
  else
1794
 
        {
1795
 
        result=(fact((double)args[0]->intValue(),
1796
 
        ((double)args[0]->intValue()-(double)args[1]->intValue()))
1797
 
        /fact((double)args[1]->intValue(),0));
1798
 
        //In fact function val must be positive
1799
 
 
1800
 
        if(result==-1)
1801
 
                context.setValue( new KSValue(tmp));
1802
 
        else
1803
 
                context.setValue( new KSValue(result ));
1804
 
        }
1805
 
  return true;
1806
 
}
1807
 
 
1808
 
static bool kspreadfunc_bino( KSContext& context )
1809
 
{
1810
 
  double result=0;
1811
 
  QString tmp;
1812
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1813
 
 
1814
 
  if ( !KSUtil::checkArgumentsCount( context,3, "BINO",true ) )
1815
 
    return false;
1816
 
 
1817
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1818
 
    return false;
1819
 
 
1820
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1821
 
    return false;
1822
 
 
1823
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1824
 
    return false;
1825
 
 
1826
 
  tmp=i18n("Err");
1827
 
  if(args[0]->doubleValue()<args[1]->doubleValue())
1828
 
    context.setValue( new KSValue(tmp ));
1829
 
 
1830
 
  else if(args[1]->doubleValue()<0)
1831
 
    context.setValue( new KSValue(tmp ));
1832
 
 
1833
 
  // 0<proba<1
1834
 
  else if((args[2]->doubleValue()<0)||(args[2]->doubleValue()>1))
1835
 
    context.setValue( new KSValue(tmp ));
1836
 
  else
1837
 
  {
1838
 
    result=(fact(args[0]->doubleValue(),
1839
 
                 (args[0]->doubleValue()-args[1]->doubleValue()))
1840
 
            /fact(args[1]->doubleValue(),0));
1841
 
    //In fact function val must be positive
1842
 
 
1843
 
    if(result==-1)
1844
 
      context.setValue( new KSValue(tmp));
1845
 
    else
1846
 
    {
1847
 
      result=result*pow(args[2]->doubleValue(),(int)args[1]->doubleValue())*
1848
 
        pow((1-args[2]->doubleValue()),((int)args[0]->doubleValue()-
1849
 
                                        ((int)args[1]->doubleValue())));
1850
 
      context.setValue( new KSValue(result ));
1851
 
    }
1852
 
  }
1853
 
  return true;
1854
 
 
1855
 
 
1856
 
}
1857
 
 
1858
 
 
1859
 
static bool kspreadfunc_bino_inv( KSContext& context )
1860
 
{
1861
 
  double result=0;
1862
 
  QString tmp;
1863
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1864
 
 
1865
 
  if ( !KSUtil::checkArgumentsCount( context,3, "INVBINO",true ) )
1866
 
    return false;
1867
 
 
1868
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1869
 
    return false;
1870
 
 
1871
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1872
 
    return false;
1873
 
 
1874
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1875
 
    return false;
1876
 
 
1877
 
  tmp=i18n("Err");
1878
 
  if(args[0]->doubleValue()<args[1]->doubleValue())
1879
 
          context.setValue( new KSValue(tmp ));
1880
 
 
1881
 
  else if(args[1]->doubleValue()<0)
1882
 
          context.setValue( new KSValue(tmp ));
1883
 
 
1884
 
  // 0<proba<1
1885
 
  else if((args[2]->doubleValue()<0)||(args[2]->doubleValue()>1))
1886
 
    context.setValue( new KSValue(tmp ));
1887
 
  else
1888
 
  {
1889
 
        result=(fact(args[0]->doubleValue(),
1890
 
        (args[0]->doubleValue()-args[1]->doubleValue()))
1891
 
        /fact(args[1]->doubleValue(),0));
1892
 
        //In fact function val must be positive
1893
 
 
1894
 
        if(result==-1)
1895
 
          context.setValue( new KSValue(tmp));
1896
 
        else
1897
 
        {
1898
 
          result=result*pow((1-args[2]->doubleValue()),((int)args[0]->doubleValue()-
1899
 
                                                        (int)args[1]->doubleValue()))*pow(args[2]->doubleValue(),(
1900
 
                                                          (int)args[1]->doubleValue()));
1901
 
          context.setValue( new KSValue(result ));
1902
 
        }
1903
 
  }
1904
 
  return true;
1905
 
 
1906
 
 
1907
 
}
1908
 
 
1909
 
static bool kspreadfunc_fv( KSContext& context )
1910
 
{
1911
 
/* Returns future value, given current value, interest rate and time */
1912
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1913
 
 
1914
 
  if ( !KSUtil::checkArgumentsCount( context, 3, "FV", true ) )
1915
 
    return false;
1916
 
 
1917
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1918
 
    return false;
1919
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1920
 
    return false;
1921
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1922
 
    return false;
1923
 
  double present = args[0]->doubleValue();
1924
 
  double interest = args[1]->doubleValue();
1925
 
  double periods = args[2]->doubleValue();
1926
 
 
1927
 
 
1928
 
  context.setValue( new KSValue( present * pow(1+interest, periods)));
1929
 
  return true;
1930
 
}
1931
 
 
1932
 
static bool kspreadfunc_compound( KSContext& context )
1933
 
{
1934
 
/* Returns value after compounded interest, given principal, rate, periods
1935
 
per year and year */
1936
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1937
 
 
1938
 
  if ( !KSUtil::checkArgumentsCount( context, 4, "compound", true ) )
1939
 
    return false;
1940
 
 
1941
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1942
 
    return false;
1943
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1944
 
    return false;
1945
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1946
 
    return false;
1947
 
  if ( !KSUtil::checkType( context, args[3], KSValue::DoubleType, true ) )
1948
 
    return false;
1949
 
  double principal = args[0]->doubleValue();
1950
 
  double interest = args[1]->doubleValue();
1951
 
  double periods = args[2]->doubleValue();
1952
 
  double years = args[3]->doubleValue();
1953
 
 
1954
 
  context.setValue( new KSValue( principal * pow(1+(interest/periods),
1955
 
periods*years)));
1956
 
 
1957
 
  return true;
1958
 
}
1959
 
 
1960
 
static bool kspreadfunc_continuous( KSContext& context )
1961
 
{
1962
 
    // If you still don't understand this, let me know!  ;-)  jsinger@leeta.net
1963
 
 
1964
 
/* Returns value after continuous compounding of interest, given prinicpal,
1965
 
rate and years */
1966
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1967
 
 
1968
 
  if ( !KSUtil::checkArgumentsCount( context, 3, "continuous", true ) )
1969
 
    return false;
1970
 
 
1971
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1972
 
    return false;
1973
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1974
 
    return false;
1975
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1976
 
    return false;
1977
 
  double principal = args[0]->doubleValue();
1978
 
  double interest = args[1]->doubleValue();
1979
 
  double years = args[2]->doubleValue();
1980
 
 
1981
 
 
1982
 
  context.setValue( new KSValue( principal * exp(interest * years)));
1983
 
  return true;
1984
 
}
1985
 
 
1986
 
static bool kspreadfunc_pv( KSContext& context )
1987
 
{
1988
 
/* Returns presnt value, given future value, interest rate and years */
1989
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
1990
 
 
1991
 
  if ( !KSUtil::checkArgumentsCount( context, 3, "PV", true ) )
1992
 
    return false;
1993
 
 
1994
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
1995
 
    return false;
1996
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
1997
 
    return false;
1998
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
1999
 
    return false;
2000
 
  double future = args[0]->doubleValue();
2001
 
  double interest = args[1]->doubleValue();
2002
 
  double periods = args[2]->doubleValue();
2003
 
 
2004
 
 
2005
 
  context.setValue( new KSValue( future / pow(1+interest, periods)));
2006
 
  return true;
2007
 
}
2008
 
 
2009
 
static bool kspreadfunc_pv_annuity( KSContext& context )
2010
 
{
2011
 
    /* Returns present value of an annuity or cash flow, given payment,
2012
 
       interest rate,
2013
 
       periods, initial amount and whether payments are made at the start (TRUE)
2014
 
       or end of a period */
2015
 
 
2016
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
2017
 
 
2018
 
    if ( !KSUtil::checkArgumentsCount( context, 3, "PV_annuity", true ) )
2019
 
        return false;
2020
 
 
2021
 
    if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2022
 
        return false;
2023
 
    if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2024
 
        return false;
2025
 
    if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2026
 
        return false;
2027
 
    double amount = args[0]->doubleValue();
2028
 
    double interest = args[1]->doubleValue();
2029
 
    double periods = args[2]->doubleValue();
2030
 
 
2031
 
    double result;
2032
 
    result = amount * (1 - 1/(pow( (1+interest), periods ))) / interest ;
2033
 
 
2034
 
  context.setValue( new KSValue( result ) );
2035
 
 
2036
 
  return true;
2037
 
}
2038
 
 
2039
 
 
2040
 
static bool kspreadfunc_fv_annuity( KSContext& context )
2041
 
{
2042
 
    /* Returns future value of an annuity or cash flow, given payment, interest
2043
 
       rate and periods */
2044
 
 
2045
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
2046
 
 
2047
 
    if ( !KSUtil::checkArgumentsCount( context, 3, "FV_annuity", true ) )
2048
 
        return false;
2049
 
 
2050
 
    if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2051
 
        return false;
2052
 
    if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2053
 
        return false;
2054
 
    if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2055
 
        return false;
2056
 
    double amount= args[0]->doubleValue();
2057
 
    double interest = args[1]->doubleValue();
2058
 
    double periods = args[2]->doubleValue();
2059
 
 
2060
 
    double result;
2061
 
 
2062
 
    result = amount * ((pow( (1+interest),periods))/interest - 1/interest)   ;
2063
 
 
2064
 
    context.setValue( new KSValue( result ) );
2065
 
 
2066
 
    return true;
2067
 
}
2068
 
 
2069
 
 
2070
 
static bool kspreadfunc_effective( KSContext& context )
2071
 
{
2072
 
/* Returns effective interest rate given nominal rate and periods per year */
2073
 
 
2074
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2075
 
 
2076
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "effective", true ) )
2077
 
    return false;
2078
 
 
2079
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2080
 
    return false;
2081
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2082
 
    return false;
2083
 
  double nominal = args[0]->doubleValue();
2084
 
  double periods = args[1]->doubleValue();
2085
 
 
2086
 
  context.setValue( new KSValue(  pow( 1 + (nominal/periods), periods )- 1 ) );
2087
 
 
2088
 
  return true;
2089
 
}
2090
 
 
2091
 
 
2092
 
static bool kspreadfunc_zero_coupon( KSContext& context )
2093
 
{
2094
 
/* Returns effective interest rate given nominal rate and periods per year */
2095
 
 
2096
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2097
 
 
2098
 
  if ( !KSUtil::checkArgumentsCount( context, 3, "zero_coupon", true ) )
2099
 
    return false;
2100
 
 
2101
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2102
 
    return false;
2103
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2104
 
    return false;
2105
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2106
 
    return false;
2107
 
  double face = args[0]->doubleValue();
2108
 
  double rate = args[1]->doubleValue();
2109
 
  double years = args[2]->doubleValue();
2110
 
 
2111
 
  context.setValue( new KSValue(  face / pow( (1 + rate), years )  ) );
2112
 
 
2113
 
  return true;
2114
 
}
2115
 
 
2116
 
 
2117
 
static bool kspreadfunc_level_coupon( KSContext& context )
2118
 
{
2119
 
/* Returns effective interest rate given nominal rate and periods per year */
2120
 
 
2121
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2122
 
 
2123
 
  if ( !KSUtil::checkArgumentsCount( context, 5, "level_coupon", true ) )
2124
 
    return false;
2125
 
 
2126
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2127
 
    return false;
2128
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2129
 
    return false;
2130
 
  if ( !KSUtil::checkType( context, args[2], KSValue::DoubleType, true ) )
2131
 
    return false;
2132
 
  if ( !KSUtil::checkType( context, args[3], KSValue::DoubleType, true ) )
2133
 
    return false;
2134
 
  if ( !KSUtil::checkType( context, args[4], KSValue::DoubleType, true ) )
2135
 
    return false;
2136
 
  double face = args[0]->doubleValue();
2137
 
  double coupon_rate = args[1]->doubleValue();
2138
 
  double coupon_year = args[2]->doubleValue();
2139
 
  double years = args[3]->doubleValue();
2140
 
  double market_rate = args[4]->doubleValue();
2141
 
 
2142
 
  double coupon = coupon_rate * face / coupon_year;
2143
 
  double interest =  market_rate/coupon_year;
2144
 
  double pv_annuity = (1 - 1/(pow( (1+interest), (years*coupon_year) ))) / interest ;
2145
 
  context.setValue( new KSValue( coupon * pv_annuity + (face/ pow( (1+interest), (years*coupon_year) ) ) ) );
2146
 
 
2147
 
  return true;
2148
 
}
2149
 
 
2150
 
 
2151
 
static bool kspreadfunc_nominal( KSContext& context )
2152
 
{
2153
 
/* Returns nominal interest rate given effective rate and periods per year */
2154
 
 
2155
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2156
 
 
2157
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "nominal", true ) )
2158
 
    return false;
2159
 
 
2160
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2161
 
    return false;
2162
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2163
 
    return false;
2164
 
  double effective = args[0]->doubleValue();
2165
 
  double periods = args[1]->doubleValue();
2166
 
 
2167
 
  if ( periods == 0.0 ) // Check null
2168
 
      return false;
2169
 
 
2170
 
  context.setValue( new KSValue( periods * (pow( (effective + 1), (1 / periods) ) -1) ) );
2171
 
 
2172
 
  return true;
2173
 
}
2174
 
 
2175
 
 
2176
 
static bool kspreadfunc_sign( KSContext& context )
2177
 
{
2178
 
  int value=0;
2179
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2180
 
 
2181
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "sign", true ) )
2182
 
    return false;
2183
 
  double val=0.0;
2184
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2185
 
    {
2186
 
      if ( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
2187
 
        return false;
2188
 
    }
2189
 
  val=args[0]->doubleValue();
2190
 
 
2191
 
  if(val>0)
2192
 
    value=1;
2193
 
  else if(val<0)
2194
 
    value=-1;
2195
 
  else if(val==0)
2196
 
    value=0;
2197
 
 
2198
 
  context.setValue( new KSValue( value ) );
2199
 
 
2200
 
  return true;
2201
 
}
2202
 
 
2203
 
static bool kspreadfunc_atan2( KSContext& context )
2204
 
{
2205
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2206
 
 
2207
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "atan2", true ) )
2208
 
    return false;
2209
 
 
2210
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2211
 
    return false;
2212
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2213
 
    return false;
2214
 
  context.setValue( new KSValue( atan2( args[1]->doubleValue(),args[0]->doubleValue() ) ) );
2215
 
 
2216
 
  return true;
2217
 
}
2218
 
 
2219
 
static bool kspreadfunc_inv( KSContext& context )
2220
 
{
2221
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2222
 
 
2223
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "INV",true ) )
2224
 
    return false;
2225
 
  double val=0.0;
2226
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2227
 
    {
2228
 
      if ( !KSUtil::checkType( context, args[0], KSValue::Empty, true ) )
2229
 
        return false;
2230
 
    }
2231
 
  val=args[0]->doubleValue();
2232
 
 
2233
 
 
2234
 
  context.setValue( new KSValue( val*(-1) ) );
2235
 
 
2236
 
  return true;
2237
 
}
2238
 
 
2239
 
static bool kspreadfunc_dec2hex( KSContext& context )
2240
 
{
2241
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2242
 
 
2243
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "DECHEX", true ) ||!KSUtil::checkArgumentsCount( context, 1, "DEC2HEX", true ))
2244
 
    return false;
2245
 
 
2246
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2247
 
    return false;
2248
 
 
2249
 
  QString tmp;
2250
 
  tmp=tmp.setNum( args[0]->intValue(),16);
2251
 
  context.setValue( new KSValue( tmp ));
2252
 
 
2253
 
  return true;
2254
 
}
2255
 
 
2256
 
static bool kspreadfunc_dec2oct( KSContext& context )
2257
 
{
2258
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2259
 
 
2260
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "DEC2OCT", true )  || !KSUtil::checkArgumentsCount( context, 1, "DECOCT", true ))
2261
 
    return false;
2262
 
 
2263
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2264
 
    return false;
2265
 
 
2266
 
  QString tmp;
2267
 
  tmp=tmp.setNum( args[0]->intValue(),8);
2268
 
  context.setValue( new KSValue( tmp ));
2269
 
 
2270
 
  return true;
2271
 
}
2272
 
 
2273
 
static bool kspreadfunc_dec2bin( KSContext& context )
2274
 
{
2275
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2276
 
 
2277
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "DEC2BIN", true )  || !KSUtil::checkArgumentsCount( context, 1, "DECBIN", true ))
2278
 
    return false;
2279
 
 
2280
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
2281
 
    return false;
2282
 
 
2283
 
  QString tmp;
2284
 
  tmp=tmp.setNum( args[0]->intValue(),2);
2285
 
  context.setValue( new KSValue( tmp ));
2286
 
 
2287
 
  return true;
2288
 
}
2289
 
 
2290
 
static bool kspreadfunc_bin2dec( KSContext& context )
2291
 
{
2292
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2293
 
 
2294
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2DEC", true ) )
2295
 
    return false;
2296
 
 
2297
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2298
 
    return false;
2299
 
 
2300
 
  QString tmp=args[0]->stringValue();
2301
 
  bool ok;
2302
 
  long val=tmp.toLong(&ok,2);
2303
 
  if(!ok)
2304
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2305
 
  else
2306
 
        context.setValue( new KSValue(val));
2307
 
 
2308
 
  return true;
2309
 
}
2310
 
 
2311
 
static bool kspreadfunc_bin2oct( KSContext& context )
2312
 
{
2313
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2314
 
 
2315
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2OCT", true ) )
2316
 
    return false;
2317
 
 
2318
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2319
 
    return false;
2320
 
 
2321
 
  QString tmp=args[0]->stringValue();
2322
 
  bool ok;
2323
 
  long val=tmp.toLong(&ok,2);
2324
 
  if(!ok)
2325
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2326
 
  else
2327
 
        {
2328
 
        tmp=tmp.setNum(val,8);
2329
 
        context.setValue( new KSValue(tmp));
2330
 
        }
2331
 
 
2332
 
  return true;
2333
 
}
2334
 
 
2335
 
static bool kspreadfunc_bin2hex( KSContext& context )
2336
 
{
2337
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2338
 
 
2339
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "BIN2HEX", true ) )
2340
 
    return false;
2341
 
 
2342
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2343
 
    return false;
2344
 
 
2345
 
  QString tmp=args[0]->stringValue();
2346
 
  bool ok;
2347
 
  long val=tmp.toLong(&ok,2);
2348
 
  if(!ok)
2349
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2350
 
  else
2351
 
        {
2352
 
        tmp=tmp.setNum(val,16);
2353
 
        context.setValue( new KSValue(tmp));
2354
 
        }
2355
 
 
2356
 
  return true;
2357
 
}
2358
 
 
2359
 
static bool kspreadfunc_oct2dec( KSContext& context )
2360
 
{
2361
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2362
 
 
2363
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2DEC", true ) )
2364
 
    return false;
2365
 
 
2366
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2367
 
    return false;
2368
 
 
2369
 
  QString tmp=args[0]->stringValue();
2370
 
  bool ok;
2371
 
  long val=tmp.toLong(&ok,8);
2372
 
  if(!ok)
2373
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2374
 
  else
2375
 
        context.setValue( new KSValue(val));
2376
 
 
2377
 
  return true;
2378
 
}
2379
 
 
2380
 
static bool kspreadfunc_oct2bin( KSContext& context )
2381
 
{
2382
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2383
 
 
2384
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2BIN", true ) )
2385
 
    return false;
2386
 
 
2387
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2388
 
    return false;
2389
 
 
2390
 
  QString tmp=args[0]->stringValue();
2391
 
  bool ok;
2392
 
  long val=tmp.toLong(&ok,8);
2393
 
  if(!ok)
2394
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2395
 
  else
2396
 
        {
2397
 
        tmp=tmp.setNum(val,2);
2398
 
        context.setValue( new KSValue(tmp));
2399
 
        }
2400
 
 
2401
 
  return true;
2402
 
}
2403
 
 
2404
 
static bool kspreadfunc_oct2hex( KSContext& context )
2405
 
{
2406
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2407
 
 
2408
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "OCT2HEX", true ) )
2409
 
    return false;
2410
 
 
2411
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2412
 
    return false;
2413
 
 
2414
 
  QString tmp=args[0]->stringValue();
2415
 
  bool ok;
2416
 
  long val=tmp.toLong(&ok,8);
2417
 
  if(!ok)
2418
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2419
 
  else
2420
 
        {
2421
 
        tmp=tmp.setNum(val,16);
2422
 
        context.setValue( new KSValue(tmp));
2423
 
        }
2424
 
 
2425
 
  return true;
2426
 
}
2427
 
 
2428
 
static bool kspreadfunc_hex2dec( KSContext& context )
2429
 
{
2430
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2431
 
 
2432
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2DEC", true ) )
2433
 
    return false;
2434
 
 
2435
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2436
 
    return false;
2437
 
 
2438
 
  QString tmp=args[0]->stringValue();
2439
 
  bool ok;
2440
 
  long val=tmp.toLong(&ok,16);
2441
 
  if(!ok)
2442
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2443
 
  else
2444
 
        context.setValue( new KSValue(val));
2445
 
 
2446
 
  return true;
2447
 
}
2448
 
 
2449
 
static bool kspreadfunc_hex2bin( KSContext& context )
2450
 
{
2451
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2452
 
 
2453
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2BIN", true ) )
2454
 
    return false;
2455
 
 
2456
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2457
 
    return false;
2458
 
 
2459
 
  QString tmp=args[0]->stringValue();
2460
 
  bool ok;
2461
 
  long val=tmp.toLong(&ok,16);
2462
 
  if(!ok)
2463
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2464
 
  else
2465
 
        {
2466
 
        tmp=tmp.setNum(val,2);
2467
 
        context.setValue( new KSValue(tmp));
2468
 
        }
2469
 
 
2470
 
  return true;
2471
 
}
2472
 
 
2473
 
static bool kspreadfunc_hex2oct( KSContext& context )
2474
 
{
2475
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2476
 
 
2477
 
  if ( !KSUtil::checkArgumentsCount( context, 1, "HEX2OCT", true ) )
2478
 
    return false;
2479
 
 
2480
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2481
 
    return false;
2482
 
 
2483
 
  QString tmp=args[0]->stringValue();
2484
 
  bool ok;
2485
 
  long val=tmp.toLong(&ok,16);
2486
 
  if(!ok)
2487
 
        context.setValue( new KSValue( QString(i18n("Err") )));
2488
 
  else
2489
 
        {
2490
 
        tmp=tmp.setNum(val,8);
2491
 
        context.setValue( new KSValue(tmp));
2492
 
        }
2493
 
 
2494
 
  return true;
2495
 
}
2496
 
 
2497
 
 
2498
 
static bool kspreadfunc_rounddown( KSContext& context )
2499
 
{
2500
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2501
 
  double result=0;
2502
 
  int digits=0;
2503
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "ROUNDDOWN", true ) )
2504
 
        {
2505
 
        //just 1 argument => number of decimal =0 by default
2506
 
        if ( !KSUtil::checkArgumentsCount( context, 1, "ROUNDDOWN", true ) )
2507
 
                return false;
2508
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2509
 
            return false;
2510
 
        digits=0;
2511
 
        }
2512
 
  else
2513
 
        {
2514
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2515
 
                return false;
2516
 
        if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2517
 
                return false;
2518
 
        digits=args[1]->intValue();
2519
 
        }
2520
 
  result=floor(args[0]->doubleValue()*pow(10.0,digits))/pow(10.0,digits);
2521
 
  context.setValue( new KSValue( result) );
2522
 
 
2523
 
  return true;
2524
 
}
2525
 
 
2526
 
static bool kspreadfunc_roundup( KSContext& context )
2527
 
{
2528
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2529
 
  double result=0;
2530
 
  int digits=0;
2531
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "ROUNDUP", true ) )
2532
 
        {
2533
 
        //just 1 argument => number of decimal =0 by default
2534
 
        if ( !KSUtil::checkArgumentsCount( context, 1, "ROUNDUP", true ) )
2535
 
                return false;
2536
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2537
 
            return false;
2538
 
        digits=0;
2539
 
        }
2540
 
  else
2541
 
        {
2542
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2543
 
                return false;
2544
 
        if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2545
 
                return false;
2546
 
        digits=args[1]->intValue();
2547
 
        }
2548
 
  // This is not correct solution for problem with floating point numbers and probably
2549
 
  // will fail in platforms where float and double lenghts are same.
2550
 
  if (approx_equal(floor(args[0]->doubleValue()*pow(10,digits)), args[0]->doubleValue()*pow(10,digits)))
2551
 
      result = args[0]->doubleValue();
2552
 
  else
2553
 
      result=floor(args[0]->doubleValue()*pow(10,digits)+1)/pow(10,digits);
2554
 
  context.setValue( new KSValue( result) );
2555
 
 
2556
 
  return true;
2557
 
}
2558
 
 
2559
 
static bool kspreadfunc_round( KSContext& context )
2560
 
{
2561
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2562
 
  double result=0;
2563
 
  int digits=0;
2564
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "ROUND", true ) )
2565
 
        {
2566
 
        //just 1 argument => number of decimal =0 by default
2567
 
        if ( !KSUtil::checkArgumentsCount( context, 1, "ROUND", true ) )
2568
 
                return false;
2569
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2570
 
            return false;
2571
 
        digits=0;
2572
 
        }
2573
 
  else
2574
 
        {
2575
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2576
 
                return false;
2577
 
        if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
2578
 
                return false;
2579
 
        digits=args[1]->intValue();
2580
 
        }
2581
 
  result=floor(args[0]->doubleValue()*pow(10.0,digits)+0.5)/pow(10.0,digits);
2582
 
  context.setValue( new KSValue( result) );
2583
 
 
2584
 
  return true;
2585
 
}
2586
 
 
2587
 
static QString kspreadfunc_create_complex( double real,double imag )
2588
 
{
2589
 
  QString tmp,tmp2;
2590
 
  if(imag ==0)
2591
 
        {
2592
 
        return KGlobal::locale()->formatNumber( real);
2593
 
        }
2594
 
  if(real!=0)
2595
 
        tmp=KGlobal::locale()->formatNumber(real);
2596
 
  else
2597
 
        return KGlobal::locale()->formatNumber(imag)+"i";
2598
 
  if (imag >0)
2599
 
        tmp=tmp+"+"+KGlobal::locale()->formatNumber(imag)+"i";
2600
 
  else
2601
 
        tmp=tmp+KGlobal::locale()->formatNumber(imag)+"i";
2602
 
  return tmp;
2603
 
 
2604
 
}
2605
 
 
2606
 
static bool kspreadfunc_complex( KSContext& context )
2607
 
{
2608
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2609
 
 
2610
 
  if ( !KSUtil::checkArgumentsCount( context,2, "COMPLEX",true ) )
2611
 
    return false;
2612
 
 
2613
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2614
 
    return false;
2615
 
 
2616
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
2617
 
    return false;
2618
 
  if(args[1]->doubleValue() ==0)
2619
 
        {
2620
 
        context.setValue( new KSValue(args[0]->doubleValue()));
2621
 
        return true;
2622
 
        }
2623
 
  QString tmp=kspreadfunc_create_complex(args[0]->doubleValue(),args[1]->doubleValue());
2624
 
  bool ok;
2625
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
2626
 
  if(ok)
2627
 
        {
2628
 
        context.setValue( new KSValue(result));
2629
 
        return true;
2630
 
        }
2631
 
  context.setValue( new KSValue(tmp));
2632
 
 
2633
 
  return true;
2634
 
}
2635
 
 
2636
 
 
2637
 
static double imag_complexe(QString str, bool &ok)
2638
 
{
2639
 
QString tmp=str;
2640
 
if(tmp.find('i')==-1)
2641
 
        {  //not a complex
2642
 
        ok=true;
2643
 
        return 0;
2644
 
        }
2645
 
else if( tmp.length()==1)
2646
 
        {
2647
 
        // i
2648
 
        ok=true;
2649
 
        return 1;
2650
 
        }
2651
 
else  if( tmp.length()==2 )
2652
 
        {
2653
 
        //-i,+i,
2654
 
        int pos1;
2655
 
        if((pos1=tmp.find('+'))!=-1&& pos1==0)
2656
 
                {
2657
 
                ok=true;
2658
 
                return 1;
2659
 
                }
2660
 
        else if( (pos1=tmp.find('-'))!=-1 && pos1==0 )
2661
 
                {
2662
 
                ok=true;
2663
 
                return -1;
2664
 
                }
2665
 
        else if(tmp[0].isDigit())
2666
 
                { //5i
2667
 
                ok=true;
2668
 
                return KGlobal::locale()->readNumber(tmp.left(1));
2669
 
                }
2670
 
        else
2671
 
                {
2672
 
                ok=false;
2673
 
                return 0;
2674
 
                }
2675
 
        }
2676
 
else
2677
 
        {//12+12i
2678
 
        int pos1,pos2;
2679
 
        if((pos1=tmp.find('i'))!=-1)
2680
 
                {
2681
 
                double val;
2682
 
                QString tmpStr;
2683
 
 
2684
 
                if((pos2=tmp.findRev('+'))!=-1 && pos2!=0)
2685
 
                        {
2686
 
                        if((pos1-pos2)==1)
2687
 
                                {
2688
 
                                 ok=true;
2689
 
                                 return 1;
2690
 
                                }
2691
 
                        else
2692
 
                                {
2693
 
                                tmpStr=tmp.mid(pos2,(pos1-pos2));
2694
 
                                val=KGlobal::locale()->readNumber(tmpStr, &ok);
2695
 
                                if(!ok)
2696
 
                                        val=0;
2697
 
                                return val;
2698
 
                                }
2699
 
                        }
2700
 
                else if( (pos2=tmp.findRev('-'))!=-1&& pos2!=0)
2701
 
                        {
2702
 
                        if((pos1-pos2)==1)
2703
 
                                {
2704
 
                                 ok=true;
2705
 
                                 return -1;
2706
 
                                }
2707
 
                        else
2708
 
                                {
2709
 
                                tmpStr=tmp.mid(pos2,(pos1-pos2));
2710
 
                                val=KGlobal::locale()->readNumber(tmpStr, &ok);
2711
 
                                if(!ok)
2712
 
                                        val=0;
2713
 
                                return val;
2714
 
                                }
2715
 
                        }
2716
 
                else
2717
 
                        {//15.55i
2718
 
                        tmpStr=tmp.left(pos1);
2719
 
                        val=KGlobal::locale()->readNumber(tmpStr, &ok);
2720
 
                        if(!ok)
2721
 
                                val=0;
2722
 
                        return val;
2723
 
                        }
2724
 
                }
2725
 
        }
2726
 
ok=false;
2727
 
return 0;
2728
 
}
2729
 
 
2730
 
static bool kspreadfunc_complex_imag( KSContext& context )
2731
 
{
2732
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2733
 
 
2734
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMAGINARY",true ) )
2735
 
    return false;
2736
 
  QString tmp;
2737
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2738
 
        {
2739
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2740
 
                return false;
2741
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
2742
 
        }
2743
 
  else
2744
 
        {
2745
 
        tmp=args[0]->stringValue();
2746
 
        }
2747
 
  bool good;
2748
 
  double result=imag_complexe(tmp, good);
2749
 
  if(good)
2750
 
        context.setValue( new KSValue(result));
2751
 
  else
2752
 
        context.setValue( new KSValue(i18n("Err")));
2753
 
 
2754
 
  return true;
2755
 
}
2756
 
 
2757
 
 
2758
 
static double real_complexe(QString str, bool &ok)
2759
 
{
2760
 
double val;
2761
 
int pos1,pos2;
2762
 
QString tmp=str;
2763
 
QString tmpStr;
2764
 
if((pos1=tmp.find('i'))==-1)
2765
 
        { //12.5
2766
 
        val=KGlobal::locale()->readNumber(tmp, &ok);
2767
 
        if(!ok)
2768
 
                val=0;
2769
 
        return val;
2770
 
        }
2771
 
else
2772
 
        { //15-xi
2773
 
        if((pos2=tmp.findRev('-'))!=-1 && pos2!=0)
2774
 
                {
2775
 
                tmpStr=tmp.left(pos2);
2776
 
                val=KGlobal::locale()->readNumber(tmpStr, &ok);
2777
 
                if(!ok)
2778
 
                        val=0;
2779
 
                return val;
2780
 
                } //15+xi
2781
 
        else if((pos2=tmp.findRev('+'))!=-1)
2782
 
                {
2783
 
                tmpStr=tmp.left(pos2);
2784
 
                val=KGlobal::locale()->readNumber(tmpStr, &ok);
2785
 
                if(!ok)
2786
 
                        val=0;
2787
 
                return val;
2788
 
                }
2789
 
        else
2790
 
                {
2791
 
                ok=true;
2792
 
                return 0;
2793
 
                }
2794
 
        }
2795
 
 
2796
 
ok=false;
2797
 
return 0;
2798
 
}
2799
 
 
2800
 
static bool kspreadfunc_complex_real( KSContext& context )
2801
 
{
2802
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
2803
 
 
2804
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMREAL",true ) )
2805
 
    return false;
2806
 
  QString tmp;
2807
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
2808
 
        {
2809
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
2810
 
                return false;
2811
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
2812
 
        }
2813
 
  else
2814
 
        tmp=args[0]->stringValue();
2815
 
  bool good;
2816
 
  double result=real_complexe(tmp, good);
2817
 
  if(good)
2818
 
        context.setValue( new KSValue(result));
2819
 
  else
2820
 
        context.setValue( new KSValue(i18n("Err")));
2821
 
 
2822
 
  return true;
2823
 
}
2824
 
 
2825
 
 
2826
 
static bool kspreadfunc_imsum_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2827
 
{
2828
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
2829
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
2830
 
 
2831
 
  for( ; it != end; ++it )
2832
 
  {
2833
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2834
 
    {
2835
 
      if ( !kspreadfunc_imsum_helper( context, (*it)->listValue(), result ) )
2836
 
        return false;
2837
 
    }
2838
 
    else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2839
 
      {
2840
 
      double imag,real,imag1,real1;
2841
 
      bool ok;
2842
 
      imag=imag_complexe(result, ok);
2843
 
      real=real_complexe(result,  ok);
2844
 
      imag1=imag_complexe((*it)->stringValue(), ok);
2845
 
      real1=real_complexe((*it)->stringValue(), ok);
2846
 
      result=kspreadfunc_create_complex(real+real1,imag+imag1);
2847
 
      }
2848
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2849
 
      {
2850
 
      double imag,real,imag1,real1;
2851
 
      bool ok;
2852
 
      imag=imag_complexe(result, ok);
2853
 
      real=real_complexe(result,  ok);
2854
 
      imag1=0;
2855
 
      real1=(*it)->doubleValue();
2856
 
      result=kspreadfunc_create_complex(real+real1,imag+imag1);
2857
 
      }
2858
 
    else
2859
 
      return false;
2860
 
  }
2861
 
 
2862
 
  return true;
2863
 
}
2864
 
 
2865
 
static bool kspreadfunc_imsum( KSContext& context )
2866
 
{
2867
 
  QString result ;
2868
 
  bool b = kspreadfunc_imsum_helper( context, context.value()->listValue(), result );
2869
 
  bool ok;
2870
 
  QString tmp;
2871
 
  double val=KGlobal::locale()->readNumber(result, &ok);
2872
 
  if(ok&&b)
2873
 
        context.setValue( new KSValue( val ) );
2874
 
  else if ( b )
2875
 
    context.setValue( new KSValue( result ) );
2876
 
 
2877
 
  return b;
2878
 
}
2879
 
 
2880
 
static bool kspreadfunc_imsub_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2881
 
{
2882
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
2883
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
2884
 
 
2885
 
  for( ; it != end; ++it )
2886
 
  {
2887
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2888
 
    {
2889
 
      if ( !kspreadfunc_imsub_helper( context, (*it)->listValue(), result ) )
2890
 
        return false;
2891
 
    }
2892
 
    else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2893
 
      {
2894
 
      double imag,real,imag1,real1;
2895
 
      bool ok;
2896
 
      if(!result.isEmpty())
2897
 
        {
2898
 
        imag=imag_complexe(result, ok);
2899
 
        real=real_complexe(result,  ok);
2900
 
        imag1=imag_complexe((*it)->stringValue(), ok);
2901
 
        real1=real_complexe((*it)->stringValue(), ok);
2902
 
        result=kspreadfunc_create_complex(real-real1,imag-imag1);
2903
 
        }
2904
 
      else
2905
 
        {
2906
 
        imag1=imag_complexe((*it)->stringValue(), ok);
2907
 
        real1=real_complexe((*it)->stringValue(), ok);
2908
 
        result=kspreadfunc_create_complex(real1,imag1);
2909
 
        }
2910
 
      }
2911
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2912
 
      {
2913
 
      double imag,real,imag1,real1;
2914
 
      bool ok;
2915
 
      imag=imag_complexe(result, ok);
2916
 
      real=real_complexe(result,  ok);
2917
 
      imag1=0;
2918
 
      real1=(*it)->doubleValue();
2919
 
      if(!result.isEmpty())
2920
 
        result=kspreadfunc_create_complex(real-real1,imag-imag1);
2921
 
      else
2922
 
        result=kspreadfunc_create_complex(real1,imag1);
2923
 
      }
2924
 
    else
2925
 
      return false;
2926
 
  }
2927
 
 
2928
 
  return true;
2929
 
}
2930
 
 
2931
 
static bool kspreadfunc_imsub( KSContext& context )
2932
 
{
2933
 
  QString result ;
2934
 
  bool b = kspreadfunc_imsub_helper( context, context.value()->listValue(), result );
2935
 
  bool ok;
2936
 
  QString tmp;
2937
 
  double val=KGlobal::locale()->readNumber(result, &ok);
2938
 
  if(ok&&b)
2939
 
        context.setValue( new KSValue( val ) );
2940
 
  else if ( b )
2941
 
    context.setValue( new KSValue( result ) );
2942
 
 
2943
 
  return b;
2944
 
}
2945
 
 
2946
 
 
2947
 
static bool kspreadfunc_improduct_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
2948
 
{
2949
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
2950
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
2951
 
 
2952
 
  for( ; it != end; ++it )
2953
 
  {
2954
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
2955
 
    {
2956
 
      if ( !kspreadfunc_improduct_helper( context, (*it)->listValue(), result ) )
2957
 
        return false;
2958
 
    }
2959
 
    else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
2960
 
      {
2961
 
      double imag,real,imag1,real1;
2962
 
      bool ok;
2963
 
      if(!result.isEmpty())
2964
 
        {
2965
 
        imag=imag_complexe(result, ok);
2966
 
        real=real_complexe(result,  ok);
2967
 
        imag1=imag_complexe((*it)->stringValue(), ok);
2968
 
        real1=real_complexe((*it)->stringValue(), ok);
2969
 
        result=kspreadfunc_create_complex(real*real1+(imag*imag1)*-1,real*imag1+real1*imag);
2970
 
        }
2971
 
      else
2972
 
        {
2973
 
        imag1=imag_complexe((*it)->stringValue(), ok);
2974
 
        real1=real_complexe((*it)->stringValue(), ok);
2975
 
        result=kspreadfunc_create_complex(real1,imag1);
2976
 
        }
2977
 
      }
2978
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
2979
 
      {
2980
 
      double imag,real,imag1,real1;
2981
 
      bool ok;
2982
 
      imag=imag_complexe(result, ok);
2983
 
      real=real_complexe(result,  ok);
2984
 
      imag1=0;
2985
 
      real1=(*it)->doubleValue();
2986
 
      if(!result.isEmpty())
2987
 
        result=kspreadfunc_create_complex(real*real1+(imag*imag1)*-1,real*imag1+real1*imag);
2988
 
      else
2989
 
        result=kspreadfunc_create_complex(real1,imag1);
2990
 
      }
2991
 
    else
2992
 
      return false;
2993
 
  }
2994
 
 
2995
 
  return true;
2996
 
}
2997
 
 
2998
 
static bool kspreadfunc_improduct( KSContext& context )
2999
 
{
3000
 
  QString result ;
3001
 
  bool b = kspreadfunc_improduct_helper( context, context.value()->listValue(), result );
3002
 
  bool ok;
3003
 
  QString tmp;
3004
 
  double val=KGlobal::locale()->readNumber(result, &ok);
3005
 
  if(ok&&b)
3006
 
        context.setValue( new KSValue( val ) );
3007
 
  else if ( b )
3008
 
    context.setValue( new KSValue( result ) );
3009
 
 
3010
 
  return b;
3011
 
}
3012
 
 
3013
 
 
3014
 
static bool kspreadfunc_imconjugate( KSContext& context )
3015
 
{
3016
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3017
 
 
3018
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMCONJUGATE",true ) )
3019
 
    return false;
3020
 
  QString tmp;
3021
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3022
 
        {
3023
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3024
 
                return false;
3025
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3026
 
        }
3027
 
  else
3028
 
        {
3029
 
        tmp=args[0]->stringValue();
3030
 
        }
3031
 
  bool ok;
3032
 
  double real=real_complexe(tmp,ok);
3033
 
  if(!ok)
3034
 
        {
3035
 
        context.setValue( new KSValue(i18n("Err")));
3036
 
        return false;
3037
 
        }
3038
 
  double imag=imag_complexe(tmp,ok);
3039
 
  if(!ok)
3040
 
        {
3041
 
        context.setValue( new KSValue(i18n("Err")));
3042
 
        return false;
3043
 
        }
3044
 
  tmp=kspreadfunc_create_complex(real,-imag);
3045
 
 
3046
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3047
 
  if(ok)
3048
 
        {
3049
 
        context.setValue( new KSValue(result));
3050
 
        return true;
3051
 
        }
3052
 
  context.setValue( new KSValue(tmp));
3053
 
 
3054
 
  return true;
3055
 
}
3056
 
 
3057
 
static bool kspreadfunc_imargument( KSContext& context )
3058
 
{
3059
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3060
 
 
3061
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMARGUMENT",true ) )
3062
 
    return false;
3063
 
  QString tmp;
3064
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3065
 
        {
3066
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3067
 
                return false;
3068
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3069
 
        }
3070
 
  else
3071
 
        {
3072
 
        tmp=args[0]->stringValue();
3073
 
        }
3074
 
  bool ok;
3075
 
  double real=real_complexe(tmp,ok);
3076
 
  if(!ok)
3077
 
        {
3078
 
        context.setValue( new KSValue(i18n("Err")));
3079
 
        return false;
3080
 
        }
3081
 
  double imag=imag_complexe(tmp,ok);
3082
 
  if(!ok)
3083
 
        {
3084
 
        context.setValue( new KSValue(i18n("Err")));
3085
 
        return false;
3086
 
        }
3087
 
  if(imag==0)
3088
 
        {
3089
 
        context.setValue( new KSValue(i18n("#Div/0")));
3090
 
        return true;
3091
 
        }
3092
 
  double arg=atan2(imag,real);
3093
 
 
3094
 
  context.setValue( new KSValue(arg));
3095
 
 
3096
 
  return true;
3097
 
}
3098
 
 
3099
 
static bool kspreadfunc_imabs( KSContext& context )
3100
 
{
3101
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3102
 
 
3103
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMABS",true ) )
3104
 
    return false;
3105
 
  QString tmp;
3106
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3107
 
        {
3108
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3109
 
                return false;
3110
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3111
 
        }
3112
 
  else
3113
 
        {
3114
 
        tmp=args[0]->stringValue();
3115
 
        }
3116
 
  bool ok;
3117
 
  double real=real_complexe(tmp,ok);
3118
 
  if(!ok)
3119
 
        {
3120
 
        context.setValue( new KSValue(i18n("Err")));
3121
 
        return false;
3122
 
        }
3123
 
  double imag=imag_complexe(tmp,ok);
3124
 
  if(!ok)
3125
 
        {
3126
 
        context.setValue( new KSValue(i18n("Err")));
3127
 
        return false;
3128
 
        }
3129
 
  double arg=sqrt(pow(imag,2)+pow(real,2));
3130
 
 
3131
 
  context.setValue( new KSValue(arg));
3132
 
 
3133
 
  return true;
3134
 
}
3135
 
 
3136
 
static bool kspreadfunc_imcos( KSContext& context )
3137
 
{
3138
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3139
 
 
3140
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMCOS",true ) )
3141
 
    return false;
3142
 
  QString tmp;
3143
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3144
 
        {
3145
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3146
 
                return false;
3147
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3148
 
        }
3149
 
  else
3150
 
        {
3151
 
        tmp=args[0]->stringValue();
3152
 
        }
3153
 
  bool ok;
3154
 
  double real=real_complexe(tmp,ok);
3155
 
  if(!ok)
3156
 
        {
3157
 
        context.setValue( new KSValue(i18n("Err")));
3158
 
        return false;
3159
 
        }
3160
 
  double imag=imag_complexe(tmp,ok);
3161
 
  if(!ok)
3162
 
        {
3163
 
        context.setValue( new KSValue(i18n("Err")));
3164
 
        return false;
3165
 
        }
3166
 
  double imag_res=sin(real)*sinh(imag);
3167
 
  double real_res=cos(real)*cosh(imag);
3168
 
 
3169
 
 
3170
 
  tmp=kspreadfunc_create_complex(real_res,-imag_res);
3171
 
 
3172
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3173
 
  if(ok)
3174
 
        {
3175
 
        context.setValue( new KSValue(result));
3176
 
        return true;
3177
 
        }
3178
 
  context.setValue( new KSValue(tmp));
3179
 
 
3180
 
  return true;
3181
 
}
3182
 
 
3183
 
static bool kspreadfunc_imsin( KSContext& context )
3184
 
{
3185
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3186
 
 
3187
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMSIN",true ) )
3188
 
    return false;
3189
 
  QString tmp;
3190
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3191
 
        {
3192
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3193
 
                return false;
3194
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3195
 
        }
3196
 
  else
3197
 
        {
3198
 
        tmp=args[0]->stringValue();
3199
 
        }
3200
 
  bool ok;
3201
 
  double real=real_complexe(tmp,ok);
3202
 
  if(!ok)
3203
 
        {
3204
 
        context.setValue( new KSValue(i18n("Err")));
3205
 
        return false;
3206
 
        }
3207
 
  double imag=imag_complexe(tmp,ok);
3208
 
  if(!ok)
3209
 
        {
3210
 
        context.setValue( new KSValue(i18n("Err")));
3211
 
        return false;
3212
 
        }
3213
 
  double imag_res=cos(real)*sinh(imag);
3214
 
  double real_res=sin(real)*cosh(imag);
3215
 
 
3216
 
 
3217
 
  tmp=kspreadfunc_create_complex(real_res,imag_res);
3218
 
 
3219
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3220
 
  if(ok)
3221
 
        {
3222
 
        context.setValue( new KSValue(result));
3223
 
        return true;
3224
 
        }
3225
 
  context.setValue( new KSValue(tmp));
3226
 
 
3227
 
  return true;
3228
 
}
3229
 
 
3230
 
static bool kspreadfunc_imln( KSContext& context )
3231
 
{
3232
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3233
 
 
3234
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMLN",true ) )
3235
 
    return false;
3236
 
  QString tmp;
3237
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3238
 
        {
3239
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3240
 
                return false;
3241
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3242
 
        }
3243
 
  else
3244
 
        {
3245
 
        tmp=args[0]->stringValue();
3246
 
        }
3247
 
  bool ok;
3248
 
  double real=real_complexe(tmp,ok);
3249
 
  if(!ok)
3250
 
        {
3251
 
        context.setValue( new KSValue(i18n("Err")));
3252
 
        return false;
3253
 
        }
3254
 
  double imag=imag_complexe(tmp,ok);
3255
 
  if(!ok)
3256
 
        {
3257
 
        context.setValue( new KSValue(i18n("Err")));
3258
 
        return false;
3259
 
        }
3260
 
 
3261
 
 
3262
 
  double arg=sqrt(pow(imag,2)+pow(real,2));
3263
 
  double real_res=log(arg);
3264
 
  double imag_res=atan(imag/real);
3265
 
  tmp=kspreadfunc_create_complex(real_res,imag_res);
3266
 
 
3267
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3268
 
  if(ok)
3269
 
        {
3270
 
        context.setValue( new KSValue(result));
3271
 
        return true;
3272
 
        }
3273
 
  context.setValue( new KSValue(tmp));
3274
 
  return true;
3275
 
}
3276
 
 
3277
 
static bool kspreadfunc_imexp( KSContext& context )
3278
 
{
3279
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3280
 
 
3281
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMEXP",true ) )
3282
 
    return false;
3283
 
  QString tmp;
3284
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3285
 
        {
3286
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3287
 
                return false;
3288
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3289
 
        }
3290
 
  else
3291
 
        {
3292
 
        tmp=args[0]->stringValue();
3293
 
        }
3294
 
  bool ok;
3295
 
  double real=real_complexe(tmp,ok);
3296
 
  if(!ok)
3297
 
        {
3298
 
        context.setValue( new KSValue(i18n("Err")));
3299
 
        return false;
3300
 
        }
3301
 
  double imag=imag_complexe(tmp,ok);
3302
 
  if(!ok)
3303
 
        {
3304
 
        context.setValue( new KSValue(i18n("Err")));
3305
 
        return false;
3306
 
        }
3307
 
  double imag_res=exp(real)*sin(imag);
3308
 
  double real_res=exp(real)*cos(imag);
3309
 
 
3310
 
 
3311
 
  tmp=kspreadfunc_create_complex(real_res,imag_res);
3312
 
 
3313
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3314
 
  if(ok)
3315
 
        {
3316
 
        context.setValue( new KSValue(result));
3317
 
        return true;
3318
 
        }
3319
 
  context.setValue( new KSValue(tmp));
3320
 
 
3321
 
  return true;
3322
 
}
3323
 
 
3324
 
static bool kspreadfunc_imsqrt( KSContext& context )
3325
 
{
3326
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3327
 
 
3328
 
  if ( !KSUtil::checkArgumentsCount( context,1, "IMSQRT",true ) )
3329
 
    return false;
3330
 
  QString tmp;
3331
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3332
 
        {
3333
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3334
 
                return false;
3335
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3336
 
        }
3337
 
  else
3338
 
        {
3339
 
        tmp=args[0]->stringValue();
3340
 
        }
3341
 
  bool ok;
3342
 
  double real=real_complexe(tmp,ok);
3343
 
  if(!ok)
3344
 
        {
3345
 
        context.setValue( new KSValue(i18n("Err")));
3346
 
        return false;
3347
 
        }
3348
 
  double imag=imag_complexe(tmp,ok);
3349
 
  if(!ok)
3350
 
        {
3351
 
        context.setValue( new KSValue(i18n("Err")));
3352
 
        return false;
3353
 
        }
3354
 
  double arg=sqrt(sqrt(pow(imag,2)+pow(real,2)));
3355
 
  double angle=atan(imag/real);
3356
 
 
3357
 
  double real_res=arg*cos((angle/2));
3358
 
  double imag_res=arg*sin((angle/2));
3359
 
 
3360
 
  tmp=kspreadfunc_create_complex(real_res,imag_res);
3361
 
 
3362
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3363
 
  if(ok)
3364
 
        {
3365
 
        context.setValue( new KSValue(result));
3366
 
        return true;
3367
 
        }
3368
 
  context.setValue( new KSValue(tmp));
3369
 
 
3370
 
  return true;
3371
 
}
3372
 
 
3373
 
static bool kspreadfunc_impower( KSContext& context )
3374
 
{
3375
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3376
 
 
3377
 
  if ( !KSUtil::checkArgumentsCount( context,2, "IMPOWER",true ) )
3378
 
    return false;
3379
 
  QString tmp;
3380
 
  if ( !KSUtil::checkType( context, args[0], KSValue::StringType, true ) )
3381
 
        {
3382
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3383
 
                return false;
3384
 
        tmp=KGlobal::locale()->formatNumber(args[0]->doubleValue());
3385
 
        }
3386
 
  else
3387
 
        {
3388
 
        tmp=args[0]->stringValue();
3389
 
        }
3390
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
3391
 
        return false;
3392
 
 
3393
 
  bool ok;
3394
 
  double real=real_complexe(tmp,ok);
3395
 
  if(!ok)
3396
 
        {
3397
 
        context.setValue( new KSValue(i18n("Err")));
3398
 
        return false;
3399
 
        }
3400
 
  double imag=imag_complexe(tmp,ok);
3401
 
  if(!ok)
3402
 
        {
3403
 
        context.setValue( new KSValue(i18n("Err")));
3404
 
        return false;
3405
 
        }
3406
 
 
3407
 
  double arg=pow(sqrt(pow(imag,2)+pow(real,2)),args[1]->intValue());
3408
 
  double angle=atan(imag/real);
3409
 
 
3410
 
  double real_res=arg*cos(angle*args[1]->intValue());
3411
 
  double imag_res=arg*sin(angle*args[1]->intValue());
3412
 
 
3413
 
  tmp=kspreadfunc_create_complex(real_res,imag_res);
3414
 
 
3415
 
  double result=KGlobal::locale()->readNumber(tmp, &ok);
3416
 
  if(ok)
3417
 
        {
3418
 
        context.setValue( new KSValue(result));
3419
 
        return true;
3420
 
        }
3421
 
  context.setValue( new KSValue(tmp));
3422
 
 
3423
 
  return true;
3424
 
}
3425
 
 
3426
 
 
3427
 
static bool kspreadfunc_imdiv_helper( KSContext& context, QValueList<KSValue::Ptr>& args, QString& result )
3428
 
{
3429
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
3430
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
3431
 
 
3432
 
  for( ; it != end; ++it )
3433
 
  {
3434
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3435
 
    {
3436
 
      if ( !kspreadfunc_imdiv_helper( context, (*it)->listValue(), result ) )
3437
 
        return false;
3438
 
    }
3439
 
    else if ( KSUtil::checkType( context, *it, KSValue::StringType, true ) )
3440
 
      {
3441
 
      double imag,real,imag1,real1;
3442
 
      bool ok;
3443
 
      if(!result.isEmpty())
3444
 
        {
3445
 
        imag=imag_complexe(result, ok);
3446
 
        real=real_complexe(result,  ok);
3447
 
        imag1=imag_complexe((*it)->stringValue(), ok);
3448
 
        real1=real_complexe((*it)->stringValue(), ok);
3449
 
        result=kspreadfunc_create_complex((real*real1+imag*imag1)/(real1*real1+imag1*imag1),(real1*imag-real*imag1)/(real1*real1+imag1*imag1));
3450
 
        }
3451
 
      else
3452
 
        {
3453
 
        imag1=imag_complexe((*it)->stringValue(), ok);
3454
 
        real1=real_complexe((*it)->stringValue(), ok);
3455
 
        result=kspreadfunc_create_complex(real1,imag1);
3456
 
        }
3457
 
      }
3458
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
3459
 
      {
3460
 
      double imag,real,imag1,real1;
3461
 
      bool ok;
3462
 
      imag=imag_complexe(result, ok);
3463
 
      real=real_complexe(result,  ok);
3464
 
      imag1=0;
3465
 
      real1=(*it)->doubleValue();
3466
 
      if(!result.isEmpty())
3467
 
        result=kspreadfunc_create_complex((real*real1+imag*imag1)/(real1*real1+imag1*imag1),(real1*imag-real*imag1)/(real1*real1+imag1*imag1));
3468
 
      else
3469
 
        result=kspreadfunc_create_complex(real1,imag1);
3470
 
      }
3471
 
    else
3472
 
      return false;
3473
 
  }
3474
 
 
3475
 
  return true;
3476
 
}
3477
 
 
3478
 
static bool kspreadfunc_imdiv( KSContext& context )
3479
 
{
3480
 
  QString result ;
3481
 
  bool b = kspreadfunc_imdiv_helper( context, context.value()->listValue(), result );
3482
 
  bool ok;
3483
 
  QString tmp;
3484
 
  double val=KGlobal::locale()->readNumber(result, &ok);
3485
 
  if(ok&&b)
3486
 
        context.setValue( new KSValue( val ) );
3487
 
  else if ( b )
3488
 
    context.setValue( new KSValue( result ) );
3489
 
 
3490
 
  return b;
3491
 
}
3492
 
 
3493
 
 
3494
 
 
3495
 
static bool kspreadfunc_polr( KSContext& context )
3496
 
{
3497
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3498
 
 
3499
 
  if ( !KSUtil::checkArgumentsCount( context,2, "POLR",true ) )
3500
 
    return false;
3501
 
 
3502
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3503
 
    return false;
3504
 
 
3505
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3506
 
    return false;
3507
 
  double result=sqrt(pow(args[0]->doubleValue(),2)+pow(args[1]->doubleValue(),2));
3508
 
  context.setValue( new KSValue(result));
3509
 
 
3510
 
  return true;
3511
 
}
3512
 
 
3513
 
static bool kspreadfunc_pola( KSContext& context )
3514
 
{
3515
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3516
 
 
3517
 
  if ( !KSUtil::checkArgumentsCount( context,2, "POLA",true ) )
3518
 
    return false;
3519
 
 
3520
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3521
 
    return false;
3522
 
 
3523
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3524
 
    return false;
3525
 
  double result=acos(args[0]->doubleValue()/(sqrt(pow(args[0]->doubleValue(),2)+pow(args[1]->doubleValue(),2))));
3526
 
  context.setValue( new KSValue(result));
3527
 
 
3528
 
  return true;
3529
 
}
3530
 
 
3531
 
static bool kspreadfunc_carx( KSContext& context )
3532
 
{
3533
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3534
 
 
3535
 
  if ( !KSUtil::checkArgumentsCount( context,2, "CARX",true ) )
3536
 
    return false;
3537
 
 
3538
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3539
 
    return false;
3540
 
 
3541
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3542
 
    return false;
3543
 
  double result=args[0]->doubleValue()*cos(args[1]->doubleValue());
3544
 
  context.setValue( new KSValue(result));
3545
 
 
3546
 
  return true;
3547
 
}
3548
 
 
3549
 
static bool kspreadfunc_cary( KSContext& context )
3550
 
{
3551
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3552
 
 
3553
 
  if ( !KSUtil::checkArgumentsCount( context,2, "CARY",true ) )
3554
 
    return false;
3555
 
 
3556
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3557
 
    return false;
3558
 
 
3559
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3560
 
    return false;
3561
 
  double result=args[0]->doubleValue()*sin(args[1]->doubleValue());
3562
 
  context.setValue( new KSValue(result));
3563
 
 
3564
 
  return true;
3565
 
}
3566
 
 
3567
 
 
3568
 
static bool kspreadfunc_sumproduct_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3569
 
{
3570
 
  QValueList<KSValue::Ptr>::Iterator it = list.begin();
3571
 
  QValueList<KSValue::Ptr>::Iterator end = list.end();
3572
 
  QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3573
 
  QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3574
 
 
3575
 
  for( ; it != end,it2!=end2; ++it,++it2 )
3576
 
  {
3577
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3578
 
    {
3579
 
      if ( !kspreadfunc_sumproduct_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3580
 
        return false;
3581
 
    }
3582
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3583
 
      {
3584
 
      result +=( (*it)->doubleValue()*(*it2)->doubleValue());
3585
 
      }
3586
 
    else if (!( KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3587
 
      return false;
3588
 
  }
3589
 
 
3590
 
  return true;
3591
 
}
3592
 
 
3593
 
static bool kspreadfunc_sumproduct( KSContext& context )
3594
 
{
3595
 
  double result = 0.0;
3596
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3597
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "SUMPRODUCT", true ) )
3598
 
      return false;
3599
 
 
3600
 
    if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3601
 
      return false;
3602
 
    if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3603
 
      return false;
3604
 
    if(args[0]->listValue().count() !=args[1]->listValue() .count())
3605
 
        {
3606
 
        context.setValue( new KSValue( i18n("Err") ) );
3607
 
        return true;
3608
 
        }
3609
 
  bool b = kspreadfunc_sumproduct_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3610
 
 
3611
 
  if ( b )
3612
 
    context.setValue( new KSValue( result ) );
3613
 
 
3614
 
  return b;
3615
 
}
3616
 
 
3617
 
static bool kspreadfunc_sumx2py2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3618
 
{
3619
 
  QValueList<KSValue::Ptr>::Iterator it = list.begin();
3620
 
  QValueList<KSValue::Ptr>::Iterator end = list.end();
3621
 
  QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3622
 
  QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3623
 
 
3624
 
  for( ; it != end,it2!=end2; ++it,++it2 )
3625
 
  {
3626
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3627
 
    {
3628
 
      if ( !kspreadfunc_sumx2py2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3629
 
        return false;
3630
 
    }
3631
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3632
 
      {
3633
 
      result +=( pow((*it)->doubleValue(),2)+pow((*it2)->doubleValue(),2));
3634
 
      }
3635
 
    else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3636
 
      return false;
3637
 
  }
3638
 
 
3639
 
  return true;
3640
 
}
3641
 
 
3642
 
static bool kspreadfunc_sumx2py2( KSContext& context )
3643
 
{
3644
 
  double result = 0.0;
3645
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3646
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "SUMX2PY2", true ) )
3647
 
      return false;
3648
 
 
3649
 
    if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3650
 
      return false;
3651
 
    if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3652
 
      return false;
3653
 
    if(args[0]->listValue().count() !=args[1]->listValue() .count())
3654
 
        {
3655
 
        context.setValue( new KSValue( i18n("Err") ) );
3656
 
        return true;
3657
 
        }
3658
 
  bool b = kspreadfunc_sumx2py2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3659
 
 
3660
 
  if ( b )
3661
 
    context.setValue( new KSValue( result ) );
3662
 
 
3663
 
  return b;
3664
 
}
3665
 
 
3666
 
 
3667
 
static bool kspreadfunc_sumx2my2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3668
 
{
3669
 
  QValueList<KSValue::Ptr>::Iterator it = list.begin();
3670
 
  QValueList<KSValue::Ptr>::Iterator end = list.end();
3671
 
  QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3672
 
  QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3673
 
 
3674
 
  for( ; it != end,it2!=end2; ++it,++it2 )
3675
 
  {
3676
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3677
 
    {
3678
 
      if ( !kspreadfunc_sumx2my2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3679
 
        return false;
3680
 
    }
3681
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3682
 
      {
3683
 
      result +=( pow((*it)->doubleValue(),2)-pow((*it2)->doubleValue(),2));
3684
 
      }
3685
 
    else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3686
 
      return false;
3687
 
  }
3688
 
 
3689
 
  return true;
3690
 
}
3691
 
 
3692
 
static bool kspreadfunc_sumx2my2( KSContext& context )
3693
 
{
3694
 
  double result = 0.0;
3695
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3696
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "SUMX2MY2", true ) )
3697
 
      return false;
3698
 
 
3699
 
    if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3700
 
      return false;
3701
 
    if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3702
 
      return false;
3703
 
    if(args[0]->listValue().count() !=args[1]->listValue() .count())
3704
 
        {
3705
 
        context.setValue( new KSValue( i18n("Err") ) );
3706
 
        return true;
3707
 
        }
3708
 
  bool b = kspreadfunc_sumx2my2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3709
 
 
3710
 
  if ( b )
3711
 
    context.setValue( new KSValue( result ) );
3712
 
 
3713
 
  return b;
3714
 
}
3715
 
 
3716
 
static bool kspreadfunc_sumxmy2_helper( KSContext& context, QValueList<KSValue::Ptr>& list,QValueList<KSValue::Ptr>& list2, double& result )
3717
 
{
3718
 
  QValueList<KSValue::Ptr>::Iterator it = list.begin();
3719
 
  QValueList<KSValue::Ptr>::Iterator end = list.end();
3720
 
  QValueList<KSValue::Ptr>::Iterator it2 = list2.begin();
3721
 
  QValueList<KSValue::Ptr>::Iterator end2 = list2.end();
3722
 
 
3723
 
  for( ; it != end,it2!=end2; ++it,++it2 )
3724
 
  {
3725
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
3726
 
    {
3727
 
      if ( !kspreadfunc_sumxmy2_helper( context, (*it)->listValue(),(*it2)->listValue(), result ))
3728
 
        return false;
3729
 
    }
3730
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) && KSUtil::checkType( context, *it2, KSValue::DoubleType, true ))
3731
 
      {
3732
 
      result +=pow(( (*it)->doubleValue()-(*it2)->doubleValue()),2);
3733
 
      }
3734
 
    else if(!(KSUtil::checkType( context, *it, KSValue::Empty, true ) || KSUtil::checkType( context, *it2, KSValue::Empty, true )))
3735
 
      return false;
3736
 
  }
3737
 
 
3738
 
  return true;
3739
 
}
3740
 
 
3741
 
static bool kspreadfunc_sumxmy2( KSContext& context )
3742
 
{
3743
 
  double result = 0.0;
3744
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3745
 
  if ( !KSUtil::checkArgumentsCount( context, 2, "SUM2XMY", true ) )
3746
 
      return false;
3747
 
 
3748
 
    if ( !KSUtil::checkType( context, args[0], KSValue::ListType, true ) )
3749
 
      return false;
3750
 
    if ( !KSUtil::checkType( context, args[1], KSValue::ListType, true ) )
3751
 
      return false;
3752
 
    if(args[0]->listValue().count() !=args[1]->listValue() .count())
3753
 
        {
3754
 
        context.setValue( new KSValue( i18n("Err") ) );
3755
 
        return true;
3756
 
        }
3757
 
  bool b = kspreadfunc_sumxmy2_helper( context,args[0]->listValue(),args[1]->listValue() , result );
3758
 
 
3759
 
  if ( b )
3760
 
    context.setValue( new KSValue( result ) );
3761
 
 
3762
 
  return b;
3763
 
}
3764
 
 
3765
 
 
3766
 
static bool kspreadfunc_delta( KSContext& context )
3767
 
{
3768
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3769
 
  short result;
3770
 
  if ( !KSUtil::checkArgumentsCount( context,2, "DELTA",true ) )
3771
 
    return false;
3772
 
 
3773
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3774
 
    return false;
3775
 
  if ( !KSUtil::checkType( context, args[1], KSValue::DoubleType, true ) )
3776
 
    return false;
3777
 
  if(approx_equal(args[0]->doubleValue(), args[1]->doubleValue()))
3778
 
        result=1;
3779
 
  else
3780
 
        result=0;
3781
 
  context.setValue( new KSValue(result));
3782
 
 
3783
 
  return true;
3784
 
}
3785
 
 
3786
 
static bool kspreadfunc_even( KSContext& context )
3787
 
{
3788
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3789
 
  if ( !KSUtil::checkArgumentsCount( context,1, "EVEN",true ) )
3790
 
    return false;
3791
 
 
3792
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3793
 
    return false;
3794
 
  double result;
3795
 
  double val=args[0]->doubleValue();
3796
 
  int sign=1;
3797
 
  if(val<0)
3798
 
        {
3799
 
        sign=-1;
3800
 
        val=-val;
3801
 
        }
3802
 
  if (approx_equal(val, floor(val)))
3803
 
    val = floor(val);
3804
 
 double valsup=ceil( val );
3805
 
 if(fmod(valsup,2.0)==0)
3806
 
        {
3807
 
        if(val>valsup)
3808
 
                result=(int)(sign*(valsup+2));
3809
 
        else
3810
 
                result=(int)(sign*valsup);
3811
 
        }
3812
 
 else
3813
 
        {
3814
 
        result=(int)(sign*(valsup+1));
3815
 
        }
3816
 
  context.setValue( new KSValue(result));
3817
 
 
3818
 
  return true;
3819
 
}
3820
 
 
3821
 
static bool kspreadfunc_odd( KSContext& context )
3822
 
{
3823
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3824
 
  if ( !KSUtil::checkArgumentsCount( context,1, "ODD",true ) )
3825
 
    return false;
3826
 
 
3827
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
3828
 
    return false;
3829
 
  double result;
3830
 
  double valsup;
3831
 
  int sign=1;
3832
 
  double val= args[0]->doubleValue();
3833
 
  if (val < 0)
3834
 
        {
3835
 
        sign = -1;
3836
 
        val = -val;
3837
 
        }
3838
 
  if (approx_equal(val, floor(val)))
3839
 
    val = floor(val);
3840
 
  valsup = ceil(val);
3841
 
  if (fmod(valsup, 2.0) == 1)
3842
 
        {
3843
 
        if (val > valsup)
3844
 
                result=(int) (sign * (valsup + 2));
3845
 
        else
3846
 
                result=(int) (sign * valsup);
3847
 
        }
3848
 
  else
3849
 
        result=(int) (sign * (valsup + 1));
3850
 
 
3851
 
 context.setValue( new KSValue(result));
3852
 
 
3853
 
  return true;
3854
 
}
3855
 
 
3856
 
static bool kspreadfunc_isodd( KSContext& context )
3857
 
{
3858
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3859
 
  if ( !KSUtil::checkArgumentsCount( context,1, "ISODD",true ) )
3860
 
    return false;
3861
 
  bool result=true;
3862
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3863
 
        result= false;
3864
 
  if(result)
3865
 
        {
3866
 
        //it's a integer => test if it's an odd integer
3867
 
        if(args[0]->intValue() & 1)
3868
 
                result=true;
3869
 
        else
3870
 
                result=false;
3871
 
        }
3872
 
 
3873
 
 context.setValue( new KSValue(result));
3874
 
 
3875
 
  return true;
3876
 
}
3877
 
 
3878
 
static bool kspreadfunc_iseven( KSContext& context )
3879
 
{
3880
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3881
 
  if ( !KSUtil::checkArgumentsCount( context,1, "ISEVEN",true ) )
3882
 
    return false;
3883
 
  bool result=true;
3884
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3885
 
        result= false;
3886
 
  if(result)
3887
 
        {
3888
 
        //it's a integer => test if it's an even integer
3889
 
        if(args[0]->intValue() & 1)
3890
 
                result=false;
3891
 
        else
3892
 
                result=true;
3893
 
        }
3894
 
 
3895
 
 context.setValue( new KSValue(result));
3896
 
 
3897
 
  return true;
3898
 
}
3899
 
 
3900
 
static bool isLeapYear_helper(int _year)
3901
 
{
3902
 
    return (((_year % 4) == 0) && ((_year % 100) != 0) || ((_year % 400) == 0));
3903
 
}
3904
 
 
3905
 
static bool kspreadfunc_isLeapYear ( KSContext& context )
3906
 
{
3907
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3908
 
  if ( !KSUtil::checkArgumentsCount( context,1,"isLeapYear",true ) )
3909
 
    return false;
3910
 
  bool result=true;
3911
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3912
 
    return false;
3913
 
 
3914
 
  if(result)
3915
 
  {
3916
 
    int nYear = args[0]->intValue();
3917
 
    result = isLeapYear_helper(nYear);
3918
 
  }
3919
 
 
3920
 
  context.setValue( new KSValue(result));
3921
 
 
3922
 
  return true;
3923
 
}
3924
 
 
3925
 
static bool kspreadfunc_daysInYear ( KSContext& context )
3926
 
{
3927
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3928
 
  if ( !KSUtil::checkArgumentsCount( context,1,"daysInYear",true ) )
3929
 
    return false;
3930
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3931
 
    return false;
3932
 
 
3933
 
  int nYear = args[0]->intValue();
3934
 
  bool leap = isLeapYear_helper(nYear);
3935
 
  int result;
3936
 
 
3937
 
  if (leap)
3938
 
    result = 366;
3939
 
  else
3940
 
    result = 365;
3941
 
 
3942
 
  context.setValue( new KSValue(result));
3943
 
 
3944
 
  return true;
3945
 
}
3946
 
 
3947
 
static bool kspreadfunc_weeksInYear( KSContext& context )
3948
 
{
3949
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3950
 
  if ( !KSUtil::checkArgumentsCount( context,1,"weeksInYear",true ) )
3951
 
    return false;
3952
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3953
 
    return false;
3954
 
 
3955
 
  int nYear = args[0]->intValue();
3956
 
  int result;
3957
 
  QDate _date(nYear, 1, 1);
3958
 
  int nJan1DayOfWeek = _date.dayOfWeek();   //first day of the year
3959
 
 
3960
 
  if ( nJan1DayOfWeek == 4 ) { // Thursday
3961
 
        result = 53;
3962
 
  } else if ( nJan1DayOfWeek == 3 ) { // Wednesday
3963
 
        result = isLeapYear_helper(nYear) ? 53 : 52 ;
3964
 
  } else {
3965
 
        result = 52;
3966
 
  }
3967
 
 
3968
 
  context.setValue( new KSValue(result));
3969
 
 
3970
 
  return true;
3971
 
}
3972
 
 
3973
 
static bool kspreadfunc_daysInMonth( KSContext& context )
3974
 
{
3975
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
3976
 
  if ( !KSUtil::checkArgumentsCount( context,2,"daysInMonth",true ) )
3977
 
    return false;
3978
 
 
3979
 
  if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
3980
 
  {
3981
 
    context.setValue( new KSValue( i18n("Err") ) );
3982
 
    return true;
3983
 
  }
3984
 
  if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
3985
 
  {
3986
 
    context.setValue( new KSValue( i18n("Err") ) );
3987
 
    return true;
3988
 
  }
3989
 
 
3990
 
  static uint aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
3991
 
  int nYear = args[0]->intValue();
3992
 
  int nMonth = args[1]->intValue();
3993
 
  int result;
3994
 
 
3995
 
  if ( nMonth != 2)
3996
 
    result = aDaysInMonth[nMonth-1];
3997
 
  else
3998
 
  {
3999
 
    if (isLeapYear_helper(nYear))
4000
 
        result = aDaysInMonth[nMonth-1] + 1;
4001
 
    else
4002
 
        result = aDaysInMonth[nMonth-1];
4003
 
  }
4004
 
 
4005
 
  context.setValue( new KSValue(result));
4006
 
 
4007
 
  return true;
4008
 
}
4009
 
 
4010
 
 
4011
 
static bool kspreadfunc_count_helper( KSContext& context, QValueList<KSValue::Ptr>& args, double& result)
4012
 
{
4013
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
4014
 
  QValueList<KSValue::Ptr>::Iterator end = args.end();
4015
 
 
4016
 
  for( ; it != end; ++it )
4017
 
  {
4018
 
    if ( KSUtil::checkType( context, *it, KSValue::ListType, false ) )
4019
 
      {
4020
 
 
4021
 
        if ( !kspreadfunc_count_helper( context, (*it)->listValue(), result ) )
4022
 
          return false;
4023
 
      }
4024
 
    else if ( KSUtil::checkType( context, *it, KSValue::DoubleType, true ) )
4025
 
      {
4026
 
        result++;
4027
 
      }
4028
 
  }
4029
 
 
4030
 
  return true;
4031
 
}
4032
 
 
4033
 
static bool kspreadfunc_count( KSContext& context )
4034
 
{
4035
 
  double result = 0.0;
4036
 
 
4037
 
  bool b = kspreadfunc_count_helper( context, context.value()->listValue(), result );
4038
 
 
4039
 
  if ( b )
4040
 
    context.setValue( new KSValue( result ) );
4041
 
 
4042
 
  return b;
4043
 
}
4044
 
 
4045
 
 
4046
 
static bool kspreadfunc_decsex( KSContext& context )
4047
 
{
4048
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
4049
 
  if ( !KSUtil::checkArgumentsCount( context,1, "DECSEX",true ) )
4050
 
    return false;
4051
 
 
4052
 
  if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
4053
 
    return false;
4054
 
  int inter;
4055
 
  double val=args[0]->doubleValue();
4056
 
  int hours,minutes,second;
4057
 
  if(val>0)
4058
 
    inter=1;
4059
 
  else
4060
 
    inter=-1;
4061
 
  hours=inter*(int)(fabs(val));
4062
 
  minutes=(int)(60*val-60*(int)(val));
4063
 
  second=(int)(3600*val-3600*(int)(val)-60*(int)(60*val-60*(int)(val)));
4064
 
  QTime _time(hours,minutes,second);
4065
 
  context.setValue( new KSValue(_time));
4066
 
 
4067
 
  return true;
4068
 
}
4069
 
 
4070
 
static bool kspreadfunc_sexdec( KSContext& context )
4071
 
{
4072
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
4073
 
  double result;
4074
 
  if ( !KSUtil::checkArgumentsCount( context,3, "SEXDEC",true ) )
4075
 
    {
4076
 
      if ( !KSUtil::checkArgumentsCount( context,1, "SEXDEC",true ) )
4077
 
        return false;
4078
 
      if ( !KSUtil::checkType( context, args[0], KSValue::TimeType, true ) )
4079
 
        return false;
4080
 
 
4081
 
      result=args[0]->timeValue().hour()+(double)args[0]->timeValue().minute()/60.0+(double)args[0]->timeValue().second()/3600.0;
4082
 
 
4083
 
      context.setValue( new KSValue(result));
4084
 
    }
4085
 
  else
4086
 
    {
4087
 
      if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
4088
 
        return false;
4089
 
      if ( !KSUtil::checkType( context, args[1], KSValue::IntType, true ) )
4090
 
        return false;
4091
 
      if ( !KSUtil::checkType( context, args[2], KSValue::IntType, true ) )
4092
 
        return false;
4093
 
      result=args[0]->intValue()+(double)args[1]->intValue()/60.0+(double)args[2]->intValue()/3600.0;
4094
 
 
4095
 
      context.setValue( new KSValue(result));
4096
 
    }
4097
 
 
4098
 
  return true;
4099
 
}
4100
 
 
4101
 
static bool kspreadfunc_roman( KSContext& context )
4102
 
{
4103
 
    const QCString RNUnits[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
4104
 
    const QCString RNTens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
4105
 
    const QCString RNHundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
4106
 
    const QCString RNThousands[] = {"", "M", "MM", "MMM"};
4107
 
 
4108
 
 
4109
 
    QValueList<KSValue::Ptr>& args = context.value()->listValue();
4110
 
    if ( !KSUtil::checkArgumentsCount( context,1, "ROMAN",true ) )
4111
 
        return false;
4112
 
    int value;
4113
 
    if ( !KSUtil::checkType( context, args[0], KSValue::IntType, true ) )
4114
 
    {
4115
 
        if ( !KSUtil::checkType( context, args[0], KSValue::DoubleType, true ) )
4116
 
            return false;
4117
 
        else
4118
 
            value=(int)args[0]->doubleValue();
4119
 
    }
4120
 
    else
4121
 
        value=(int)args[0]->intValue();
4122
 
    if(value<0)
4123
 
    {
4124
 
        context.setValue( new KSValue(i18n("Err")));
4125
 
        return true;
4126
 
    }
4127
 
    if(value>3999)
4128
 
    {
4129
 
        context.setValue( new KSValue(i18n("Value too big")));
4130
 
        return true;
4131
 
    }
4132
 
    QString result;
4133
 
 
4134
 
    result= QString::fromLatin1( RNThousands[ ( value / 1000 ) ] +
4135
 
                                 RNHundreds[ ( value / 100 ) % 10 ] +
4136
 
                                 RNTens[ ( value / 10 ) % 10 ] +
4137
 
                                 RNUnits[ ( value ) % 10 ] );
4138
 
    context.setValue( new KSValue(result));
4139
 
    return true;
4140
 
}
4141
143
 
4142
144
static bool kspreadfunc_cell( KSContext& context )
4143
145
{
4176
178
    if ( it2 == line.end() )
4177
179
      return FALSE;
4178
180
 
4179
 
    kdDebug(36002) <<"x= "<<x<<endl;
 
181
    kdDebug(36001) <<"x= "<<x<<endl;
4180
182
    for( ; it != lines.end(); ++it )
4181
183
    {
4182
184
      const QValueList<KSValue::Ptr>& l = (*it)->listValue();
4231
233
{
4232
234
  KSModule::Ptr module = new KSModule( interp, "kspread" );
4233
235
 
4234
 
  module->addObject( "cos", new KSValue( new KSBuiltinFunction( module, "cos", kspreadfunc_cos ) ) );
4235
 
  module->addObject( "sin", new KSValue( new KSBuiltinFunction( module, "sin", kspreadfunc_sin ) ) );
4236
 
  module->addObject( "sum", new KSValue( new KSBuiltinFunction( module, "sum", kspreadfunc_sum ) ) );
4237
 
  module->addObject( "sumsq", new KSValue( new KSBuiltinFunction( module, "sumsq", kspreadfunc_sumsq ) ) );
4238
 
  module->addObject( "sqrt", new KSValue( new KSBuiltinFunction( module, "sqrt", kspreadfunc_sqrt ) ) );
4239
 
  module->addObject( "fabs", new KSValue( new KSBuiltinFunction( module, "fabs", kspreadfunc_fabs ) ) );
4240
 
  module->addObject( "floor", new KSValue( new KSBuiltinFunction( module, "floor", kspreadfunc_floor ) ) );
4241
 
  module->addObject( "ceil", new KSValue( new KSBuiltinFunction( module, "ceil", kspreadfunc_ceil ) ) );
4242
 
  module->addObject( "tan", new KSValue( new KSBuiltinFunction( module, "tan", kspreadfunc_tan ) ) );
4243
 
  module->addObject( "exp", new KSValue( new KSBuiltinFunction( module, "exp", kspreadfunc_exp ) ) );
4244
 
  module->addObject( "ln", new KSValue( new KSBuiltinFunction( module, "ln", kspreadfunc_ln ) ) );
4245
 
  module->addObject( "atan", new KSValue( new KSBuiltinFunction( module, "atan", kspreadfunc_atan ) ) );
4246
 
  module->addObject( "asin", new KSValue( new KSBuiltinFunction( module, "asin", kspreadfunc_asin ) ) );
4247
 
  module->addObject( "acos", new KSValue( new KSBuiltinFunction( module, "acos", kspreadfunc_acos ) ) );
4248
 
  module->addObject( "log", new KSValue( new KSBuiltinFunction( module, "log", kspreadfunc_log ) ) );
4249
 
  module->addObject( "max", new KSValue( new KSBuiltinFunction( module, "max", kspreadfunc_max ) ) );
4250
 
  module->addObject( "min", new KSValue( new KSBuiltinFunction( module, "min", kspreadfunc_min ) ) );
4251
 
  module->addObject( "cosh", new KSValue( new KSBuiltinFunction( module, "cosh", kspreadfunc_cosh ) ) );
4252
 
  module->addObject( "sinh", new KSValue( new KSBuiltinFunction( module, "sinh", kspreadfunc_sinh ) ) );
4253
 
  module->addObject( "tanh", new KSValue( new KSBuiltinFunction( module, "tanh", kspreadfunc_tanh ) ) );
4254
 
  module->addObject( "acosh", new KSValue( new KSBuiltinFunction( module, "acosh", kspreadfunc_acosh ) ) );
4255
 
  module->addObject( "asinh", new KSValue( new KSBuiltinFunction( module, "asinh", kspreadfunc_asinh ) ) );
4256
 
  module->addObject( "atanh", new KSValue( new KSBuiltinFunction( module, "atanh", kspreadfunc_atanh ) ) );
4257
 
  module->addObject( "degree", new KSValue( new KSBuiltinFunction( module, "degree", kspreadfunc_degree ) ) );
4258
 
  module->addObject( "radian", new KSValue( new KSBuiltinFunction( module, "radian", kspreadfunc_radian ) ) );
4259
 
  module->addObject( "average", new KSValue( new KSBuiltinFunction( module, "average", kspreadfunc_average ) ) );
4260
 
  module->addObject( "variance", new KSValue( new KSBuiltinFunction( module, "variance", kspreadfunc_variance) ) );
4261
 
  module->addObject( "multiply", new KSValue( new KSBuiltinFunction( module, "multiply", kspreadfunc_mult) ) );
4262
 
  module->addObject( "OR", new KSValue( new KSBuiltinFunction( module, "OR", kspreadfunc_or) ) );
4263
 
  module->addObject( "AND", new KSValue( new KSBuiltinFunction( module, "AND", kspreadfunc_and) ) );
4264
 
  module->addObject( "NOR", new KSValue( new KSBuiltinFunction( module, "NOR", kspreadfunc_nor) ) );
4265
 
  module->addObject( "NAND", new KSValue( new KSBuiltinFunction( module, "NAND", kspreadfunc_nand) ) );
4266
 
 
4267
 
  module->addObject( "stddev", new KSValue( new KSBuiltinFunction( module, "stderr", kspreadfunc_stddev) ) );
4268
 
  module->addObject( "join", new KSValue( new KSBuiltinFunction( module, "join", kspreadfunc_join) ) );
4269
 
  //compatibility with kspread1.0
4270
 
  module->addObject( "not", new KSValue( new KSBuiltinFunction( module, "not", kspreadfunc_not) ) );
4271
 
  module->addObject( "NOT", new KSValue( new KSBuiltinFunction( module, "NOT", kspreadfunc_not) ) );
4272
 
  //compatibility with kspread1.0
4273
 
  module->addObject( "if", new KSValue( new KSBuiltinFunction( module, "if", kspreadfunc_if) ) );
4274
 
  module->addObject( "IF", new KSValue( new KSBuiltinFunction( module, "IF", kspreadfunc_if) ) );
4275
 
  module->addObject( "left", new KSValue( new KSBuiltinFunction( module, "left", kspreadfunc_left) ) );
4276
 
  module->addObject( "right", new KSValue( new KSBuiltinFunction( module, "right", kspreadfunc_right) ) );
4277
 
  module->addObject( "mid", new KSValue( new KSBuiltinFunction( module, "mid", kspreadfunc_mid) ) );
4278
 
  module->addObject( "len", new KSValue( new KSBuiltinFunction( module, "len", kspreadfunc_len) ) );
4279
 
  module->addObject( "EXACT", new KSValue( new KSBuiltinFunction( module, "EXACT", kspreadfunc_EXACT) ) );
4280
 
  module->addObject( "INT", new KSValue( new KSBuiltinFunction( module, "INT",kspreadfunc_INT) ) );
4281
 
  //compatibility with kspread1.0
4282
 
  module->addObject( "ENT", new KSValue( new KSBuiltinFunction( module, "ENT",kspreadfunc_INT) ) );
4283
 
  module->addObject( "PI", new KSValue( new KSBuiltinFunction( module, "PI",kspreadfunc_PI) ) );
4284
 
  module->addObject( "rand", new KSValue( new KSBuiltinFunction( module, "rand",kspreadfunc_rand) ) );
4285
 
  module->addObject( "REPT", new KSValue( new KSBuiltinFunction( module, "REPT",kspreadfunc_REPT) ) );
4286
 
 
4287
 
  module->addObject( "ISLOGIC", new KSValue( new KSBuiltinFunction( module,"ISLOGIC",kspreadfunc_islogic) ) );
4288
 
  module->addObject( "ISTEXT", new KSValue( new KSBuiltinFunction( module,"ISTEXT",kspreadfunc_istext) ) );
4289
 
  module->addObject( "ISNUM", new KSValue( new KSBuiltinFunction( module,"ISNUM",kspreadfunc_isnum) ) );
4290
 
  module->addObject( "ISNOTTEXT", new KSValue( new KSBuiltinFunction( module, "ISNOTTEXT", kspreadfunc_isnottext ) ) );
4291
 
  module->addObject( "ISODD", new KSValue( new KSBuiltinFunction( module, "ISODD", kspreadfunc_isodd ) ) );
4292
 
  module->addObject( "ISEVEN", new KSValue( new KSBuiltinFunction( module, "ISEVEN", kspreadfunc_iseven ) ) );
4293
 
 
4294
 
  module->addObject( "cell", new KSValue( new KSBuiltinFunction( module,"cell",kspreadfunc_cell) ) );
4295
 
  module->addObject( "select", new KSValue( new KSBuiltinFunction( module,"select",kspreadfunc_select) ) );
4296
 
  module->addObject( "pow", new KSValue( new KSBuiltinFunction( module,"pow",kspreadfunc_pow) ) );
4297
 
  module->addObject( "MOD", new KSValue( new KSBuiltinFunction( module,"MOD",kspreadfunc_mod) ) );
4298
 
  module->addObject( "date", new KSValue( new KSBuiltinFunction( module,"date",kspreadfunc_date) ) );
4299
 
  module->addObject( "day", new KSValue( new KSBuiltinFunction( module,"day",kspreadfunc_day) ) );
4300
 
  module->addObject( "month", new KSValue( new KSBuiltinFunction( module,"month",kspreadfunc_month) ) );
4301
 
  module->addObject( "time", new KSValue( new KSBuiltinFunction( module,"time",kspreadfunc_time) ) );
4302
 
  module->addObject( "currentTime", new KSValue( new KSBuiltinFunction( module,"currentTime",kspreadfunc_currentTime) ) );
4303
 
  module->addObject( "currentDate", new KSValue( new KSBuiltinFunction( module,"currentDate",kspreadfunc_currentDate) ) );
4304
 
  module->addObject( "currentDateTime", new KSValue( new KSBuiltinFunction( module,"currentDateTime",kspreadfunc_currentDateTime) ) );
4305
 
  module->addObject( "dayOfYear", new KSValue( new KSBuiltinFunction( module,"dayOfYear",kspreadfunc_dayOfYear) ) );
4306
 
  module->addObject( "fact", new KSValue( new KSBuiltinFunction( module,"fact",kspreadfunc_fact) ) );
4307
 
  module->addObject( "COMBIN", new KSValue( new KSBuiltinFunction( module,"COMBIN",kspreadfunc_combin) ) );
4308
 
  module->addObject( "PERMUT", new KSValue( new KSBuiltinFunction( module,"PERMUT",kspreadfunc_arrang) ) );
4309
 
  module->addObject( "BINO", new KSValue( new KSBuiltinFunction( module,"BINO",kspreadfunc_bino) ) );
4310
 
  module->addObject( "INVBINO", new KSValue( new KSBuiltinFunction( module,"INVBINO",kspreadfunc_bino_inv) ) );
4311
 
  module->addObject( "lower", new KSValue( new KSBuiltinFunction( module,"lower",kspreadfunc_lower) ) );
4312
 
  module->addObject( "upper", new KSValue( new KSBuiltinFunction( module,"upper",kspreadfunc_upper) ) );
4313
 
  module->addObject( "find", new KSValue( new KSBuiltinFunction( module,"find",kspreadfunc_find) ) );
4314
 
  module->addObject( "compound", new KSValue( new KSBuiltinFunction( module,"compound",kspreadfunc_compound) ) );
4315
 
  module->addObject( "continuous", new KSValue( new KSBuiltinFunction( module,"continuous",kspreadfunc_continuous) ) );
4316
 
  module->addObject( "effective", new KSValue( new KSBuiltinFunction( module,"effective",kspreadfunc_effective) ) );
4317
 
  module->addObject( "nominal", new KSValue( new KSBuiltinFunction( module,"nominal",kspreadfunc_nominal) ) );
4318
 
  module->addObject( "FV", new KSValue( new KSBuiltinFunction( module,"FV",kspreadfunc_fv) ) );
4319
 
  module->addObject( "PV_annuity", new KSValue( new KSBuiltinFunction( module,"PV_annuity",kspreadfunc_pv_annuity) ) );
4320
 
  module->addObject( "PV", new KSValue( new KSBuiltinFunction( module,"PV",kspreadfunc_pv) ) );
4321
 
  module->addObject( "FV_annuity", new KSValue( new KSBuiltinFunction( module,"FV_annuity",kspreadfunc_fv_annuity) ) );
4322
 
  module->addObject( "zero_coupon", new KSValue( new KSBuiltinFunction( module,"zero_coupon",kspreadfunc_zero_coupon) ) );
4323
 
  module->addObject( "level_coupon", new KSValue( new KSBuiltinFunction( module,"level_coupon",kspreadfunc_level_coupon) ) );
4324
 
  module->addObject( "sign", new KSValue( new KSBuiltinFunction( module,"sign",kspreadfunc_sign) ) );
4325
 
  module->addObject( "atan2", new KSValue( new KSBuiltinFunction( module,"atan2",kspreadfunc_atan2) ) );
4326
 
  module->addObject( "INV", new KSValue( new KSBuiltinFunction( module,"INV",kspreadfunc_inv) ) );
4327
 
 
4328
 
  module->addObject( "DEC2BIN", new KSValue( new KSBuiltinFunction( module,"DEC2BIN",kspreadfunc_dec2bin) ) );
4329
 
  module->addObject( "DEC2OCT", new KSValue( new KSBuiltinFunction( module,"DEC2OCT",kspreadfunc_dec2oct) ) );
4330
 
  module->addObject( "DEC2HEX", new KSValue( new KSBuiltinFunction( module,"DEC2HEX",kspreadfunc_dec2hex) ) );
4331
 
  //compatibility with old formula
4332
 
  module->addObject( "DECHEX", new KSValue( new KSBuiltinFunction( module,"DECHEX",kspreadfunc_dec2hex) ) );
4333
 
  module->addObject( "DECBIN", new KSValue( new KSBuiltinFunction( module,"DECBIN",kspreadfunc_dec2bin) ) );
4334
 
  module->addObject( "DECOCT", new KSValue( new KSBuiltinFunction( module,"DECOCT",kspreadfunc_dec2oct) ) );
4335
 
 
4336
 
  module->addObject( "ROUNDDOWN", new KSValue( new KSBuiltinFunction( module,"ROUNDDOWN",kspreadfunc_rounddown) ) );
4337
 
  module->addObject( "ROUNDUP", new KSValue( new KSBuiltinFunction( module,"ROUNDUP",kspreadfunc_roundup) ) );
4338
 
  module->addObject( "ROUND", new KSValue( new KSBuiltinFunction( module,"ROUND",kspreadfunc_round) ) );
4339
 
  module->addObject( "BIN2DEC", new KSValue( new KSBuiltinFunction( module,"BIN2DEC",kspreadfunc_bin2dec) ) );
4340
 
  module->addObject( "BIN2OCT", new KSValue( new KSBuiltinFunction( module,"BIN2OCT",kspreadfunc_bin2oct) ) );
4341
 
  module->addObject( "BIN2HEX", new KSValue( new KSBuiltinFunction( module,"BIN2HEX",kspreadfunc_bin2hex) ) );
4342
 
  module->addObject( "OCT2BIN", new KSValue( new KSBuiltinFunction( module,"OCT2BIN",kspreadfunc_oct2bin) ) );
4343
 
  module->addObject( "OCT2DEC", new KSValue( new KSBuiltinFunction( module,"OCT2DEC",kspreadfunc_oct2dec) ) );
4344
 
  module->addObject( "OCT2HEX", new KSValue( new KSBuiltinFunction( module,"OCT2HEX",kspreadfunc_oct2hex) ) );
4345
 
  module->addObject( "HEX2BIN", new KSValue( new KSBuiltinFunction( module,"HEX2BIN",kspreadfunc_hex2bin) ) );
4346
 
  module->addObject( "HEX2DEC", new KSValue( new KSBuiltinFunction( module,"HEX2DEC",kspreadfunc_hex2dec) ) );
4347
 
  module->addObject( "HEX2OCT", new KSValue( new KSBuiltinFunction( module,"HEX2OCT",kspreadfunc_hex2oct) ) );
4348
 
  module->addObject( "POLR", new KSValue( new KSBuiltinFunction( module,"POLR",kspreadfunc_polr) ) );
4349
 
  module->addObject( "POLA", new KSValue( new KSBuiltinFunction( module,"POLA",kspreadfunc_pola) ) );
4350
 
  module->addObject( "CARX", new KSValue( new KSBuiltinFunction( module,"CARX",kspreadfunc_carx) ) );
4351
 
  module->addObject( "CARY", new KSValue( new KSBuiltinFunction( module,"CARY",kspreadfunc_cary) ) );
4352
 
  //complex
4353
 
  module->addObject( "COMPLEX", new KSValue( new KSBuiltinFunction( module,"COMPLEX",kspreadfunc_complex) ) );
4354
 
  module->addObject( "IMAGINARY", new KSValue( new KSBuiltinFunction( module,"IMAGINARY",kspreadfunc_complex_imag) ) );
4355
 
  module->addObject( "IMREAL", new KSValue( new KSBuiltinFunction( module,"IMREAL",kspreadfunc_complex_real) ) );
4356
 
  module->addObject( "IMSUM", new KSValue( new KSBuiltinFunction( module, "IMSUM", kspreadfunc_imsum ) ) );
4357
 
  module->addObject( "IMSUB", new KSValue( new KSBuiltinFunction( module, "IMSUB", kspreadfunc_imsub ) ) );
4358
 
  module->addObject( "IMPRODUCT", new KSValue( new KSBuiltinFunction( module, "IMPRODUCT", kspreadfunc_improduct ) ) );
4359
 
  module->addObject( "IMCONJUGATE", new KSValue( new KSBuiltinFunction( module, "IMCONJUGATE", kspreadfunc_imconjugate ) ) );
4360
 
  module->addObject( "IMARGUMENT", new KSValue( new KSBuiltinFunction( module, "IMARGUMENT", kspreadfunc_imargument ) ) );
4361
 
  module->addObject( "IMABS", new KSValue( new KSBuiltinFunction( module, "IMABS", kspreadfunc_imabs ) ) );
4362
 
  module->addObject( "IMDIV", new KSValue( new KSBuiltinFunction( module, "IMDIV", kspreadfunc_imdiv ) ) );
4363
 
  module->addObject( "IMCOS", new KSValue( new KSBuiltinFunction( module, "IMCOS", kspreadfunc_imcos ) ) );
4364
 
  module->addObject( "IMSIN", new KSValue( new KSBuiltinFunction( module, "IMSIN", kspreadfunc_imsin ) ) );
4365
 
  module->addObject( "IMEXP", new KSValue( new KSBuiltinFunction( module, "IMEXP", kspreadfunc_imexp ) ) );
4366
 
  module->addObject( "IMLN", new KSValue( new KSBuiltinFunction( module, "IMLN", kspreadfunc_imln ) ) );
4367
 
  module->addObject( "IMSQRT", new KSValue( new KSBuiltinFunction( module, "IMSQRT", kspreadfunc_imsqrt ) ) );
4368
 
  module->addObject( "IMPOWER", new KSValue( new KSBuiltinFunction( module, "IMPOWER", kspreadfunc_impower ) ) );
4369
 
 
4370
 
  module->addObject( "SUMPRODUCT", new KSValue( new KSBuiltinFunction( module, "SUMPRODUCT", kspreadfunc_sumproduct ) ) );
4371
 
  module->addObject( "SUMX2PY2", new KSValue( new KSBuiltinFunction( module, "SUMX2PY2", kspreadfunc_sumx2py2 ) ) );
4372
 
  module->addObject( "SUMX2MY2", new KSValue( new KSBuiltinFunction( module, "SUMX2MY2", kspreadfunc_sumx2my2 ) ) );
4373
 
  module->addObject( "SUM2XMY", new KSValue( new KSBuiltinFunction( module, "SUM2XMY", kspreadfunc_sumxmy2 ) ) );
4374
 
  module->addObject( "DELTA", new KSValue( new KSBuiltinFunction( module, "DELTA", kspreadfunc_delta ) ) );
4375
 
  module->addObject( "EVEN", new KSValue( new KSBuiltinFunction( module, "EVEN", kspreadfunc_even ) ) );
4376
 
  module->addObject( "ODD", new KSValue( new KSBuiltinFunction( module, "ODD", kspreadfunc_odd ) ) );
4377
 
  module->addObject( "RANDBETWEEN", new KSValue( new KSBuiltinFunction( module, "RANDBETWEEN", kspreadfunc_randbetween ) ) );
4378
 
  module->addObject( "LOGn", new KSValue( new KSBuiltinFunction( module, "LOGn", kspreadfunc_logn ) ) );
4379
 
  module->addObject( "SQRTn", new KSValue( new KSBuiltinFunction( module, "SQRTn", kspreadfunc_sqrtn ) ) );
4380
 
  module->addObject( "count", new KSValue( new KSBuiltinFunction( module, "count", kspreadfunc_count ) ) );
4381
 
  module->addObject( "CUR", new KSValue( new KSBuiltinFunction( module, "CUR", kspreadfunc_cur ) ) );
4382
 
  module->addObject( "DECSEX", new KSValue( new KSBuiltinFunction( module, "DECSEX", kspreadfunc_decsex) ) );
4383
 
  module->addObject( "SEXDEC", new KSValue( new KSBuiltinFunction( module, "SEXDEC", kspreadfunc_sexdec) ) );
4384
 
  module->addObject( "ISTIME", new KSValue( new KSBuiltinFunction( module, "ISTIME", kspreadfunc_istime) ) );
4385
 
  module->addObject( "ISDATE", new KSValue( new KSBuiltinFunction( module, "ISDATE", kspreadfunc_isdate) ) );
4386
 
  module->addObject( "hours", new KSValue( new KSBuiltinFunction( module, "hours", kspreadfunc_hours) ) );
4387
 
  module->addObject( "minutes", new KSValue( new KSBuiltinFunction( module, "minutes", kspreadfunc_minutes) ) );
4388
 
  module->addObject( "isLeapYear", new KSValue( new KSBuiltinFunction( module, "isLeapYear", kspreadfunc_isLeapYear) ) );
4389
 
  module->addObject( "daysInMonth", new KSValue( new KSBuiltinFunction( module, "daysInMonth", kspreadfunc_daysInMonth) ) );
4390
 
  module->addObject( "daysInYear", new KSValue( new KSBuiltinFunction( module, "daysInYear", kspreadfunc_daysInYear) ) );
4391
 
  module->addObject( "weeksInYear", new KSValue( new KSBuiltinFunction( module, "weeksInYear", kspreadfunc_weeksInYear) ) );
4392
 
  module->addObject( "seconds", new KSValue( new KSBuiltinFunction( module, "seconds", kspreadfunc_seconds) ) );
4393
 
  module->addObject( "ROMAN", new KSValue( new KSBuiltinFunction( module, "ROMAN", kspreadfunc_roman) ) );
4394
 
  module->addObject( "shortcurrentDate", new KSValue( new KSBuiltinFunction( module, "shortcurrentDate", kspreadfunc_shortcurrentDate) ) );
 
236
  module->addObject( "CELL", new KSValue( new KSBuiltinFunction( module, "CELL", kspreadfunc_cell ) ) );
 
237
  module->addObject( "SELECT", new KSValue( new KSBuiltinFunction( module, "SELECT", kspreadfunc_select ) ) );
 
238
 
 
239
  // add all built-in functions
 
240
  KSpreadFunctionRepository* repo = KSpreadFunctionRepository::self();
 
241
  QStringList functionList = repo->regFunctionNames();
 
242
  for( unsigned i = 0; i < functionList.count(); i++ )
 
243
  {
 
244
    QString name = functionList[i];
 
245
    KSpreadFunction* function = repo->function( name );
 
246
    if ( function && function->functionPtr )
 
247
      module->addObject( name, new KSValue( new KSBuiltinFunction( module, name, function->functionPtr ) ) );
 
248
  }
 
249
 
4395
250
  return module;
4396
251
}
4397
252
 
4401
256
 *
4402
257
 *********************************************************************/
4403
258
 
4404
 
KSpreadInterpreter::KSpreadInterpreter( KSpreadDoc* doc ) : KSInterpreter()
 
259
KSpreadInterpreter::KSpreadInterpreter( KSpreadDoc * doc )
 
260
  : KSInterpreter(),
 
261
    m_cell( 0 ),
 
262
    m_doc( doc ),
 
263
    m_table( 0 )
4405
264
{
4406
 
  m_doc = doc;
4407
 
 
4408
265
  KSModule::Ptr m = kspreadCreateModule_KSpread( this );
4409
266
  m_modules.insert( m->name(), m );
4410
267
 
4447
304
    if ( cell->hasError() )
4448
305
    {
4449
306
      QString tmp( i18n("The cell %1 has an error:\n\n%2") );
4450
 
      tmp = tmp.arg( util_cellName( cell->table(), cell->column(), cell->row() ) );
 
307
      tmp = tmp.arg( cell->fullName() );
4451
308
      tmp = tmp.arg( node->getStringLiteral() );
4452
309
      context.setException( new KSException( "ErrorInCell", tmp ) );
4453
310
      return false;
4457
314
      context.setValue( new KSValue(  /*KSValue::Empty*/ 0.0 ) );
4458
315
    else if(cell->isObscured() && cell->isObscuringForced())
4459
316
      context.setValue( new KSValue( 0.0 ) );
4460
 
    else if ( cell->isValue() )
4461
 
      context.setValue( new KSValue( cell->valueDouble() ) );
4462
 
    else if ( cell->isBool() )
4463
 
      context.setValue( new KSValue( cell->valueBool() ) );
 
317
    else if ( cell->value().isNumber() )
 
318
      context.setValue( new KSValue( cell->value().asFloat() ) );
 
319
    else if ( cell->value().isBoolean() )
 
320
      context.setValue( new KSValue( cell->value().asBoolean() ) );
4464
321
    else if ( cell->isTime() )
4465
322
      context.setValue( new KSValue( cell->valueTime() ) );
4466
323
    else if ( cell->isDate() )
4467
324
      context.setValue( new KSValue( cell->valueDate() ) );
4468
 
    else if ( cell->valueString().isEmpty() )
 
325
    else if ( cell->value().asString().isEmpty() )
4469
326
      context.setValue( new KSValue( 0.0  /*KSValue::Empty*/ ) );
4470
327
    else
4471
 
      context.setValue( new KSValue( cell->valueString() ) );
 
328
      context.setValue( new KSValue( cell->value().asString() ) );
4472
329
    return true;
4473
330
  }
4474
331
  // Parse a range like "A1:B3"
4486
343
      return false;
4487
344
    }
4488
345
 
 
346
    if ( r->range.left() == r->range.right()
 
347
         && r->range.top() == r->range.bottom() )
 
348
    {
 
349
      KSpreadCell * cell = r->table->cellAt( r->range.x(), r->range.y() );
 
350
 
 
351
      if ( cell->hasError() )
 
352
      {
 
353
        QString tmp( i18n("The cell %1 has an error:\n\n%2") );
 
354
        tmp = tmp.arg( cell->fullName() );
 
355
        tmp = tmp.arg( node->getStringLiteral() );
 
356
        context.setException( new KSException( "ErrorInCell", tmp ) );
 
357
        return false;
 
358
      }
 
359
 
 
360
      if ( cell->isDefault())
 
361
        context.setValue( new KSValue(  /*KSValue::Empty*/ 0.0 ) );
 
362
      else if(cell->isObscured() && cell->isObscuringForced())
 
363
        context.setValue( new KSValue( 0.0 ) );
 
364
      else if ( cell->value().isNumber() )
 
365
        context.setValue( new KSValue( cell->value().asFloat() ) );
 
366
      else if ( cell->value().isBoolean() )
 
367
      context.setValue( new KSValue( cell->value().asBoolean() ) );
 
368
      else if ( cell->isTime() )
 
369
        context.setValue( new KSValue( cell->valueTime() ) );
 
370
      else if ( cell->isDate() )
 
371
        context.setValue( new KSValue( cell->valueDate() ) );
 
372
      else if ( cell->value().asString().isEmpty() )
 
373
        context.setValue( new KSValue( 0.0  /*KSValue::Empty*/ ) );
 
374
      else
 
375
        context.setValue( new KSValue( cell->value().asString() ) );
 
376
 
 
377
      return true;
 
378
    }
 
379
 
4489
380
    // The range is translated in a list or lists of integers
4490
381
    KSValue* v = new KSValue( KSValue::ListType );
4491
382
    for( int y = 0; y < r->range.height(); ++y )
4500
391
        if ( cell->hasError() )
4501
392
        {
4502
393
          QString tmp( i18n("The cell %1 has an error:\n\n%2") );
4503
 
          tmp = tmp.arg( util_cellName( cell->table(), cell->column(), cell->row() ) );
 
394
          tmp = tmp.arg( cell->fullName() );
4504
395
          tmp = tmp.arg( node->getStringLiteral() );
4505
396
          context.setException( new KSException( "ErrorInCell", tmp ) );
4506
397
          return false;
4508
399
 
4509
400
        if ( cell->isDefault() )
4510
401
          c = new KSValue( 0.0 /*KSValue::Empty*/);
4511
 
        else if ( cell->isValue() )
4512
 
          c = new KSValue( cell->valueDouble() );
4513
 
        else if ( cell->isBool() )
4514
 
          c = new KSValue( cell->valueBool() );
 
402
        else if ( cell->value().isNumber() )
 
403
          c = new KSValue( cell->value().asFloat() );
 
404
        else if ( cell->value().isBoolean() )
 
405
          c = new KSValue( cell->value().asBoolean() );
4515
406
        else if ( cell->isDate() )
4516
407
          c = new KSValue( cell->valueDate() );
4517
408
        else if ( cell->isTime() )
4518
409
          c = new KSValue( cell->valueTime() );
4519
 
        else if ( cell->valueString().isEmpty() )
 
410
        else if ( cell->value().asString().isEmpty() )
4520
411
          c = new KSValue( 0.0 /*KSValue::Empty*/ );
4521
412
        else
4522
 
          c = new KSValue( cell->valueString() );
 
413
          c = new KSValue( cell->value().asString() );
4523
414
        if ( !(cell->isObscured() && cell->isObscuringForced()) )
4524
415
                l->listValue().append( c );
4525
416
      }
4530
421
    return true;
4531
422
  }
4532
423
  else
4533
 
    ASSERT( 0 );
 
424
    Q_ASSERT( 0 );
4534
425
 
4535
426
  // Never reached
4536
427
  return false;
4537
428
}
4538
429
 
4539
 
KSParseNode* KSpreadInterpreter::parse( KSContext& context, KSpreadTable* table, const QString& formula, QList<KSpreadDepend>& depends )
 
430
KSParseNode* KSpreadInterpreter::parse( KSContext& context, KSpreadSheet* table, const QString& formula, QPtrList<KSpreadDependency>& depends )
4540
431
{
4541
432
    // Create the parse tree.
4542
433
    KSParser parser;
4543
434
    // Tell the parser the locale so that it can parse localized numbers.
4544
 
    if ( !parser.parse( formula.local8Bit(), KSCRIPT_EXTENSION_KSPREAD, table->doc()->locale() ) )
 
435
    if ( !parser.parse( formula.utf8(), KSCRIPT_EXTENSION_KSPREAD, table->doc()->locale() ) )
4545
436
    {
4546
437
        context.setException( new KSException( "SyntaxError", parser.errorMessage() ) );
4547
438
        return 0;
4553
444
    return n;
4554
445
}
4555
446
 
4556
 
bool KSpreadInterpreter::evaluate( KSContext& context, KSParseNode* node, KSpreadTable* table )
 
447
bool KSpreadInterpreter::evaluate( KSContext& context, KSParseNode* node, KSpreadSheet* table, KSpreadCell* cell )
4557
448
{
4558
449
    // Save the current table to make this function reentrant.
4559
 
    KSpreadTable* t = m_table;
 
450
    KSpreadSheet * t = m_table;
 
451
    KSpreadCell * c  = m_cell;
4560
452
    m_table = table;
 
453
    m_cell  = cell;
4561
454
 
4562
455
    bool b = node->eval( context );
4563
456
 
4564
457
    m_table = t;
 
458
    m_cell  = c;
4565
459
 
4566
460
    return b;
4567
461
}