summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_functions_information.cc
blob: 2a5775aaee00917511d772a5818d2b5e2f38bef9 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* 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 information functions


#include <config.h>
#include <sys/utsname.h>

#include <tqdir.h>
#include <kdebug.h>
#include <klocale.h>

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

#include "kspread_doc.h"
#include "kspread_sheet.h"

using namespace KSpread;

// prototypes (sorted alphabetically)
Value func_filename (valVector args, ValueCalc *calc, FuncExtra *);
Value func_info (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isblank (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isdate (valVector args, ValueCalc *calc, FuncExtra *);
Value func_iserr (valVector args, ValueCalc *calc, FuncExtra *);
Value func_iserror (valVector args, ValueCalc *calc, FuncExtra *);
Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *);
Value func_islogical (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isna (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isnottext (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isnum (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *);
Value func_isref (valVector args, ValueCalc *calc, FuncExtra *);
Value func_istext (valVector args, ValueCalc *calc, FuncExtra *);
Value func_istime (valVector args, ValueCalc *calc, FuncExtra *);
Value func_n (valVector args, ValueCalc *calc, FuncExtra *);
Value func_type (valVector args, ValueCalc *calc, FuncExtra *);
Value func_version (valVector args, ValueCalc *calc, FuncExtra *);

// registers all information functions
void RegisterInformationFunctions()
{
  FunctionRepository* repo = FunctionRepository::self();
  Function *f;

  f = new Function ("FILENAME", func_filename);
  f->setParamCount (0);
  repo->add (f);
  f = new Function ("INFO", func_info);
  repo->add (f);
  f = new Function ("ISBLANK", func_isblank);
  repo->add (f);
  f = new Function ("ISDATE", func_isdate);
  repo->add (f);
  f = new Function ("ISERR", func_iserr);
  repo->add (f);
  f = new Function ("ISERROR", func_iserror);
  repo->add (f);
  f = new Function ("ISEVEN", func_iseven);
  repo->add (f);
  f = new Function ("ISLOGICAL", func_islogical);
  repo->add (f);
  f = new Function ("ISNA", func_isna);
  repo->add (f);
  f = new Function ("ISNONTEXT", func_isnottext);
  repo->add (f);
  f = new Function ("ISNOTTEXT", func_isnottext);
  repo->add (f);
  f = new Function ("ISNUM", func_isnum);
  repo->add (f);
  f = new Function ("ISNUMBER", func_isnum);
  repo->add (f);
  f = new Function ("ISODD", func_isodd);
  repo->add (f);
  f = new Function ("ISREF", func_isref);
  f->setNeedsExtra (true);
  repo->add (f);
  f = new Function ("ISTEXT", func_istext);
  repo->add (f);
  f = new Function ("ISTIME", func_istime);
  repo->add (f);
  f = new Function ("N", func_n);
  repo->add (f);
  f = new Function ("TYPE", func_type);
  f->setAcceptArray ();
  repo->add (f);
}

// Function: INFO
Value func_info (valVector args, ValueCalc *calc, FuncExtra *)
{
  TQString type = calc->conv()->asString (args[0]).asString().lower();

  if (type == "directory")
    return Value (TQDir::currentDirPath());

  if (type == "release")
    return Value (TQString (VERSION));

  if ( type == "numfile" )
    return Value ((int) Doc::documents().count());

  if (type == "recalc")
  {
    TQString result;
    if (calc->doc()) {
      if (calc->doc()->delayCalculation())
        result = i18n ("Manual");
      else
        result = i18n ("Automatic");
    }
    return Value (result);
  }

  if (type == "memavail")
    // not supported
    return Value::errorVALUE();
  if (type == "memused")
    // not supported
    return Value::errorVALUE();
  if (type == "origin")
    // not supported
    return Value::errorVALUE();

  if (type == "system") {
    struct utsname name;
    if (uname (&name) >= 0)
      return Value (TQString (name.sysname));
  }

  if (type == "totmem")
    // not supported
    return Value::errorVALUE();

  if (type == "osversion")
  {
    struct utsname name;
    if (uname (&name) >= 0)
    {
       TQString os = TQString("%1 %2 (%3)").arg( name.sysname ).
         arg( name.release ).arg( name.machine );
       return Value (os);
    }
  }

  return Value::errorVALUE();
}

// Function: ISBLANK
Value func_isblank (valVector args, ValueCalc *, FuncExtra *)
{
  return Value (args[0].isEmpty());
}

// Function: ISLOGICAL
Value func_islogical (valVector args, ValueCalc *, FuncExtra *)
{
  return Value (args[0].isBoolean());
}

// Function: ISTEXT
Value func_istext (valVector args, ValueCalc *, FuncExtra *)
{
  return Value (args[0].isString());
}

// Function: ISREF
Value func_isref (valVector, ValueCalc */*calc*/, FuncExtra *e)
{
  // no reference ?
  if ((e == 0) || (e->ranges[0].col1 == -1) || (e->ranges[0].row1 == -1))
    return Value (false);
  // if we are here, it is a reference (cell/range)
  return Value (true);
}

// Function: ISNOTTEXT
Value func_isnottext (valVector args, ValueCalc *, FuncExtra *)
{
  return Value (args[0].isString() ? false : true);
}

// Function: ISNUM
Value func_isnum (valVector args, ValueCalc *, FuncExtra *)
{
  return Value (args[0].isNumber());
}

// Function: ISTIME
Value func_istime (valVector args, ValueCalc *, FuncExtra *)
{
  return Value ((args[0].format() == Value::fmt_Time)
      || (args[0].format() == Value::fmt_DateTime));
}

// Function: ISDATE
Value func_isdate (valVector args, ValueCalc *, FuncExtra *)
{
  return Value ((args[0].format() == Value::fmt_Date)
      || (args[0].format() == Value::fmt_DateTime));
}

// Function: ISODD
Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *)
{
  return Value (calc->isEven(args[0]) ? false : true);
}

// Function: ISEVEN
Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *)
{
  return Value (calc->isEven(args[0]));
}

// Function: ISERR
Value func_iserr (valVector args, ValueCalc *, FuncExtra *)
{
  return (args[0].isError() &&
      (args[0].errorMessage() != Value::errorNA().errorMessage()));
}

// Function: ISERROR
Value func_iserror (valVector args, ValueCalc *, FuncExtra *)
{
  return args[0].isError();
}

// Function: ISNA
Value func_isna (valVector args, ValueCalc *, FuncExtra *)
{
  return (args[0].isError() &&
      (args[0].errorMessage() == Value::errorNA().errorMessage()));
}

// Function: TYPE
Value func_type (valVector args, ValueCalc *, FuncExtra *)
{
  // Returns 1 for numbers, 2 for text, 4 for boolean, 16 for error,
  // 64 for arrays
  if (args[0].isArray())
    return Value (64);
  if (args[0].isNumber())
    return Value (1);
  if (args[0].isString())
    return Value (2);
  if (args[0].isBoolean())
    return Value (4);
  if (args[0].isError())
    return Value (16);
  
  // something else ?
  return Value (0);
}

Value func_filename (valVector, ValueCalc *calc, FuncExtra *)
{
  return Value (calc->doc()->url().prettyURL());
}

// Function: N
Value func_n (valVector args, ValueCalc *calc, FuncExtra *)
{
  return calc->conv()->asFloat (args[0]);
}