kspread

kspread_functions_information.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998-2002 The KSpread Team
3 www.koffice.org/kspread
4 Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22// built-in information functions
23
24
25#include <config.h>
26#include <sys/utsname.h>
27
28#include <tqdir.h>
29#include <kdebug.h>
30#include <tdelocale.h>
31
32#include "functions.h"
33#include "valuecalc.h"
34#include "valueconverter.h"
35
36#include "kspread_doc.h"
37#include "kspread_sheet.h"
38
39using namespace KSpread;
40
41// prototypes (sorted alphabetically)
42Value func_filename (valVector args, ValueCalc *calc, FuncExtra *);
43Value func_info (valVector args, ValueCalc *calc, FuncExtra *);
44Value func_isblank (valVector args, ValueCalc *calc, FuncExtra *);
45Value func_isdate (valVector args, ValueCalc *calc, FuncExtra *);
46Value func_iserr (valVector args, ValueCalc *calc, FuncExtra *);
47Value func_iserror (valVector args, ValueCalc *calc, FuncExtra *);
48Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *);
49Value func_islogical (valVector args, ValueCalc *calc, FuncExtra *);
50Value func_isna (valVector args, ValueCalc *calc, FuncExtra *);
51Value func_isnottext (valVector args, ValueCalc *calc, FuncExtra *);
52Value func_isnum (valVector args, ValueCalc *calc, FuncExtra *);
53Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *);
54Value func_isref (valVector args, ValueCalc *calc, FuncExtra *);
55Value func_istext (valVector args, ValueCalc *calc, FuncExtra *);
56Value func_istime (valVector args, ValueCalc *calc, FuncExtra *);
57Value func_n (valVector args, ValueCalc *calc, FuncExtra *);
58Value func_type (valVector args, ValueCalc *calc, FuncExtra *);
59Value func_version (valVector args, ValueCalc *calc, FuncExtra *);
60
61// registers all information functions
62void RegisterInformationFunctions()
63{
64 FunctionRepository* repo = FunctionRepository::self();
65 Function *f;
66
67 f = new Function ("FILENAME", func_filename);
68 f->setParamCount (0);
69 repo->add (f);
70 f = new Function ("INFO", func_info);
71 repo->add (f);
72 f = new Function ("ISBLANK", func_isblank);
73 repo->add (f);
74 f = new Function ("ISDATE", func_isdate);
75 repo->add (f);
76 f = new Function ("ISERR", func_iserr);
77 repo->add (f);
78 f = new Function ("ISERROR", func_iserror);
79 repo->add (f);
80 f = new Function ("ISEVEN", func_iseven);
81 repo->add (f);
82 f = new Function ("ISLOGICAL", func_islogical);
83 repo->add (f);
84 f = new Function ("ISNA", func_isna);
85 repo->add (f);
86 f = new Function ("ISNONTEXT", func_isnottext);
87 repo->add (f);
88 f = new Function ("ISNOTTEXT", func_isnottext);
89 repo->add (f);
90 f = new Function ("ISNUM", func_isnum);
91 repo->add (f);
92 f = new Function ("ISNUMBER", func_isnum);
93 repo->add (f);
94 f = new Function ("ISODD", func_isodd);
95 repo->add (f);
96 f = new Function ("ISREF", func_isref);
97 f->setNeedsExtra (true);
98 repo->add (f);
99 f = new Function ("ISTEXT", func_istext);
100 repo->add (f);
101 f = new Function ("ISTIME", func_istime);
102 repo->add (f);
103 f = new Function ("N", func_n);
104 repo->add (f);
105 f = new Function ("TYPE", func_type);
106 f->setAcceptArray ();
107 repo->add (f);
108}
109
110// Function: INFO
111Value func_info (valVector args, ValueCalc *calc, FuncExtra *)
112{
113 TQString type = calc->conv()->asString (args[0]).asString().lower();
114
115 if (type == "directory")
116 return Value (TQDir::currentDirPath());
117
118 if (type == "release")
119 return Value (TQString (VERSION));
120
121 if ( type == "numfile" )
122 return Value ((int) Doc::documents().count());
123
124 if (type == "recalc")
125 {
126 TQString result;
127 if (calc->doc()) {
128 if (calc->doc()->delayCalculation())
129 result = i18n ("Manual");
130 else
131 result = i18n ("Automatic");
132 }
133 return Value (result);
134 }
135
136 if (type == "memavail")
137 // not supported
138 return Value::errorVALUE();
139 if (type == "memused")
140 // not supported
141 return Value::errorVALUE();
142 if (type == "origin")
143 // not supported
144 return Value::errorVALUE();
145
146 if (type == "system") {
147 struct utsname name;
148 if (uname (&name) >= 0)
149 return Value (TQString (name.sysname));
150 }
151
152 if (type == "totmem")
153 // not supported
154 return Value::errorVALUE();
155
156 if (type == "osversion")
157 {
158 struct utsname name;
159 if (uname (&name) >= 0)
160 {
161 TQString os = TQString("%1 %2 (%3)").arg( name.sysname ).
162 arg( name.release ).arg( name.machine );
163 return Value (os);
164 }
165 }
166
167 return Value::errorVALUE();
168}
169
170// Function: ISBLANK
171Value func_isblank (valVector args, ValueCalc *, FuncExtra *)
172{
173 return Value (args[0].isEmpty());
174}
175
176// Function: ISLOGICAL
177Value func_islogical (valVector args, ValueCalc *, FuncExtra *)
178{
179 return Value (args[0].isBoolean());
180}
181
182// Function: ISTEXT
183Value func_istext (valVector args, ValueCalc *, FuncExtra *)
184{
185 return Value (args[0].isString());
186}
187
188// Function: ISREF
189Value func_isref (valVector, ValueCalc */*calc*/, FuncExtra *e)
190{
191 // no reference ?
192 if ((e == 0) || (e->ranges[0].col1 == -1) || (e->ranges[0].row1 == -1))
193 return Value (false);
194 // if we are here, it is a reference (cell/range)
195 return Value (true);
196}
197
198// Function: ISNOTTEXT
199Value func_isnottext (valVector args, ValueCalc *, FuncExtra *)
200{
201 return Value (args[0].isString() ? false : true);
202}
203
204// Function: ISNUM
205Value func_isnum (valVector args, ValueCalc *, FuncExtra *)
206{
207 return Value (args[0].isNumber());
208}
209
210// Function: ISTIME
211Value func_istime (valVector args, ValueCalc *, FuncExtra *)
212{
213 return Value ((args[0].format() == Value::fmt_Time)
214 || (args[0].format() == Value::fmt_DateTime));
215}
216
217// Function: ISDATE
218Value func_isdate (valVector args, ValueCalc *, FuncExtra *)
219{
220 return Value ((args[0].format() == Value::fmt_Date)
221 || (args[0].format() == Value::fmt_DateTime));
222}
223
224// Function: ISODD
225Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *)
226{
227 return Value (calc->isEven(args[0]) ? false : true);
228}
229
230// Function: ISEVEN
231Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *)
232{
233 return Value (calc->isEven(args[0]));
234}
235
236// Function: ISERR
237Value func_iserr (valVector args, ValueCalc *, FuncExtra *)
238{
239 return (args[0].isError() &&
240 (args[0].errorMessage() != Value::errorNA().errorMessage()));
241}
242
243// Function: ISERROR
244Value func_iserror (valVector args, ValueCalc *, FuncExtra *)
245{
246 return args[0].isError();
247}
248
249// Function: ISNA
250Value func_isna (valVector args, ValueCalc *, FuncExtra *)
251{
252 return (args[0].isError() &&
253 (args[0].errorMessage() == Value::errorNA().errorMessage()));
254}
255
256// Function: TYPE
257Value func_type (valVector args, ValueCalc *, FuncExtra *)
258{
259 // Returns 1 for numbers, 2 for text, 4 for boolean, 16 for error,
260 // 64 for arrays
261 if (args[0].isArray())
262 return Value (64);
263 if (args[0].isNumber())
264 return Value (1);
265 if (args[0].isString())
266 return Value (2);
267 if (args[0].isBoolean())
268 return Value (4);
269 if (args[0].isError())
270 return Value (16);
271
272 // something else ?
273 return Value (0);
274}
275
276Value func_filename (valVector, ValueCalc *calc, FuncExtra *)
277{
278 return Value (calc->doc()->url().prettyURL());
279}
280
281// Function: N
282Value func_n (valVector args, ValueCalc *calc, FuncExtra *)
283{
284 return calc->conv()->asFloat (args[0]);
285}
static TQValueList< Doc * > documents()
Returns list of all documents.
The ValueCalc class is used to perform all sorts of calculations.
Definition valuecalc.h:69
Provides a wrapper for cell value.
TQString asString() const
Returns the string value of this value.
static const Value & errorVALUE()
Returns constant reference to #VALUE! error.
bool isError() const
Returns true if this value holds error information.
static const Value & errorNA()
Returns constant reference to #N/A error.
The KSpread namespace.
Definition commands.h:39