summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_functions_conversion.cpp
blob: 23f2ca0153d77b3dc733b5b7528e56330b5d3060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This file is part of the KDE project
   Copyright (C) 1998-2002 The KSpread Team
                           www.koffice.org/kspread
   Copyright (C) 2005 Tomas Mecir <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

// built-in conversion functions

#include "functions.h"
#include "valuecalc.h"
#include "valueconverter.h"

using namespace KSpread;

// prototypes
Value func_arabic (valVector args, ValueCalc *calc, FuncExtra *);
Value func_carx (valVector args, ValueCalc *calc, FuncExtra *);
Value func_cary (valVector args, ValueCalc *calc, FuncExtra *);
Value func_decsex (valVector args, ValueCalc *calc, FuncExtra *);
Value func_polr (valVector args, ValueCalc *calc, FuncExtra *);
Value func_pola (valVector args, ValueCalc *calc, FuncExtra *);
Value func_roman (valVector args, ValueCalc *calc, FuncExtra *);
Value func_sexdec (valVector args, ValueCalc *calc, FuncExtra *);
Value func_AsciiToChar (valVector args, ValueCalc *calc, FuncExtra *);
Value func_CharToAscii (valVector args, ValueCalc *calc, FuncExtra *);
Value func_inttobool (valVector args, ValueCalc *calc, FuncExtra *);
Value func_booltoint (valVector args, ValueCalc *calc, FuncExtra *);
Value func_ToString (valVector args, ValueCalc *calc, FuncExtra *);

// registers all conversion functions
void RegisterConversionFunctions()
{
  FunctionRepository* repo = FunctionRepository::self();
  Function *f;
  
  f = new Function ("ARABIC", func_arabic);
  repo->add (f);
  f = new Function ("CARX", func_carx);
  f->setParamCount (2);
  repo->add (f);
  f = new Function ("CARY", func_cary);
  f->setParamCount (2);
  repo->add (f);
  f = new Function ("DECSEX", func_decsex);
  repo->add (f);
  f = new Function ("POLR", func_polr);
  f->setParamCount (2);
  repo->add (f);
  f = new Function ("POLA", func_pola);
  f->setParamCount (2);
  repo->add (f);
  f = new Function ("ROMAN", func_roman);
  repo->add (f);
  f = new Function ("SEXDEC", func_sexdec);
  f->setParamCount (1, 3);
  repo->add (f);
  f = new Function ("ASCIITOCHAR", func_AsciiToChar);
  f->setParamCount (1, -1);
  f->setAcceptArray ();
  repo->add (f);
  f = new Function ("CHARTOASCII", func_CharToAscii);
  repo->add (f);
  f = new Function ("BOOL2INT", func_booltoint);
  repo->add (f);
  f = new Function ("INT2BOOL", func_inttobool);
  repo->add (f);
  f = new Function ("BOOL2STRING", func_ToString);
  repo->add (f);
  f = new Function ("NUM2STRING", func_ToString);
  repo->add (f);
  f = new Function ("STRING", func_ToString);
  repo->add (f);
}

// Function: POLR
Value func_polr (valVector args, ValueCalc *calc, FuncExtra *)
{
  // sqrt (a^2 + b^2)
  Value a = args[0];
  Value b = args[1];
  Value res = calc->sqrt (calc->add (calc->sqr (a), calc->sqr (b)));
  return res;
}

// Function: POLA
Value func_pola (valVector args, ValueCalc *calc, FuncExtra *)
{
  // acos (a / polr(a,b))
  Value polr = func_polr (args, calc, 0);
  if (calc->isZero (polr))
    return Value::errorDIV0();
  Value res = calc->acos (calc->div (args[0], polr));
  return res;
}

// Function: CARX
Value func_carx (valVector args, ValueCalc *calc, FuncExtra *)
{
  // a * cos(b)
  Value res = calc->mul (args[0], calc->cos (args[1]));
  return res;
}

// Function: CARY
Value func_cary (valVector args, ValueCalc *calc, FuncExtra *)
{
  // a * sin(b)
  Value res = calc->mul (args[0], calc->sin (args[1]));
  return res;
}

// Function: DECSEX
Value func_decsex (valVector args, ValueCalc *calc, FuncExtra *)
{
  // original function was very compicated, but I see no reason for that,
  // when it can be done as simply as this ...
  // maybe it was due to all the infrastructure not being ready back then
  return calc->conv()->asTime (calc->div (args[0], 24));
}

// Function: SEXDEC
Value func_sexdec (valVector args, ValueCalc *calc, FuncExtra *)
{
  if (args.count() == 1)
  {
    // convert given value to number
    Value time = calc->conv()->asTime (args[0]);
    return calc->mul (calc->conv()->asFloat (time), 24);
  }
  
  // convert h/m/s to number of hours
  Value h = args[0];
  Value m = args[1];

  Value res = calc->add (h, calc->div (m, 60));
  if (args.count() == 3) {
    Value s = args[2];
    res = calc->add (res, calc->div (s, 3600));
  }
  return res;
}

// Function: ROMAN
Value func_roman (valVector args, ValueCalc *calc, FuncExtra *)
{
  const TQCString RNUnits[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
  const TQCString RNTens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
  const TQCString RNHundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
  const TQCString RNThousands[] = {"", "M", "MM", "MMM"};

  // precision loss is not a problem here, as we only use the 0-3999 range
  long value = calc->conv()->asInteger (args[0]).asInteger ();
  if ((value < 0) || (value > 3999))
    return Value::errorNA();
  TQString result;
  result = TQString::fromLatin1 (RNThousands[(value / 1000)] +
                                RNHundreds[(value / 100) % 10] +
                                RNTens[(value / 10 ) % 10] +
                                RNUnits[(value) % 10]);
  return Value (result);
}

// convert single roman character to decimal
// return < 0 if invalid
int func_arabic_helper (TQChar c)
{
  switch (c.upper().unicode())
  {
    case 'M': return 1000;
    case 'D': return 500;
    case 'C': return 100;
    case 'L': return 50;
    case 'X': return 10;
    case 'V': return 5;
    case 'I': return 1;
  }
  return -1;
}

// Function: ARABIC
Value func_arabic (valVector args, ValueCalc *calc, FuncExtra *)
{
  TQString roman = calc->conv()->asString (args[0]).asString();
  if( roman.isEmpty() ) return Value::errorVALUE();

  int val = 0, lastd = 0, d = 0;

  for (unsigned i = 0; i < roman.length(); i++) {
    d = func_arabic_helper( roman[i] );
    if( d < 0 ) return Value::errorVALUE();

    if( lastd < d ) val -= lastd;
      else val += lastd;
    lastd = d;
  }
  if( lastd < d ) val -= lastd;
    else val += lastd;

  return Value (val);
}

// helper for AsciiToChar
void func_a2c_helper (ValueCalc *calc, TQString &s, Value val)
{
  if (val.isArray()) {
    for (unsigned int row = 0; row < val.rows(); ++row)
      for (unsigned int col = 0; col < val.columns(); ++col)
        func_a2c_helper (calc, s, val.element (col, row));
  } else {
    int v = calc->conv()->asInteger (val).asInteger();
    if (v == 0) return;
    TQChar c (v);
    s = s + c;
  }
}

// Function: AsciiToChar
Value func_AsciiToChar (valVector args, ValueCalc *calc, FuncExtra *)
{
  TQString str;
  for (unsigned int i = 0; i < args.count(); i++)
    func_a2c_helper (calc, str, args[i]);
  return Value (str);
}

// Function: CharToAscii
Value func_CharToAscii (valVector args, ValueCalc *calc, FuncExtra *)
{
  TQString val = calc->conv()->asString (args[0]).asString ();
  if (val.length() == 1)
    return Value (TQString (val[0]));
  return Value::errorVALUE();
}

// Function: inttobool
Value func_inttobool (valVector args, ValueCalc *calc, FuncExtra *)
{
  return calc->conv()->asBoolean (args[0]);
}

// Function: booltoint
Value func_booltoint (valVector args, ValueCalc *calc, FuncExtra *)
{
  return calc->conv()->asInteger (args[0]);
}

// Function: BoolToString, NumberToString, String
Value func_ToString (valVector args, ValueCalc *calc, FuncExtra *)
{
  return calc->conv()->asString (args[0]);
}