kspread

kspread_functions_information.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998-2002 The KSpread Team
00003                            www.koffice.org/kspread
00004    Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 // built-in information functions
00023 
00024 
00025 #include <config.h>
00026 #include <sys/utsname.h>
00027 
00028 #include <qdir.h>
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 
00032 #include "functions.h"
00033 #include "valuecalc.h"
00034 #include "valueconverter.h"
00035 
00036 #include "kspread_doc.h"
00037 #include "kspread_sheet.h"
00038 
00039 using namespace KSpread;
00040 
00041 // prototypes (sorted alphabetically)
00042 Value func_filename (valVector args, ValueCalc *calc, FuncExtra *);
00043 Value func_info (valVector args, ValueCalc *calc, FuncExtra *);
00044 Value func_isblank (valVector args, ValueCalc *calc, FuncExtra *);
00045 Value func_isdate (valVector args, ValueCalc *calc, FuncExtra *);
00046 Value func_iserr (valVector args, ValueCalc *calc, FuncExtra *);
00047 Value func_iserror (valVector args, ValueCalc *calc, FuncExtra *);
00048 Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *);
00049 Value func_islogical (valVector args, ValueCalc *calc, FuncExtra *);
00050 Value func_isna (valVector args, ValueCalc *calc, FuncExtra *);
00051 Value func_isnottext (valVector args, ValueCalc *calc, FuncExtra *);
00052 Value func_isnum (valVector args, ValueCalc *calc, FuncExtra *);
00053 Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *);
00054 Value func_isref (valVector args, ValueCalc *calc, FuncExtra *);
00055 Value func_istext (valVector args, ValueCalc *calc, FuncExtra *);
00056 Value func_istime (valVector args, ValueCalc *calc, FuncExtra *);
00057 Value func_n (valVector args, ValueCalc *calc, FuncExtra *);
00058 Value func_type (valVector args, ValueCalc *calc, FuncExtra *);
00059 Value func_version (valVector args, ValueCalc *calc, FuncExtra *);
00060 
00061 // registers all information functions
00062 void RegisterInformationFunctions()
00063 {
00064   FunctionRepository* repo = FunctionRepository::self();
00065   Function *f;
00066 
00067   f = new Function ("FILENAME", func_filename);
00068   f->setParamCount (0);
00069   repo->add (f);
00070   f = new Function ("INFO", func_info);
00071   repo->add (f);
00072   f = new Function ("ISBLANK", func_isblank);
00073   repo->add (f);
00074   f = new Function ("ISDATE", func_isdate);
00075   repo->add (f);
00076   f = new Function ("ISERR", func_iserr);
00077   repo->add (f);
00078   f = new Function ("ISERROR", func_iserror);
00079   repo->add (f);
00080   f = new Function ("ISEVEN", func_iseven);
00081   repo->add (f);
00082   f = new Function ("ISLOGICAL", func_islogical);
00083   repo->add (f);
00084   f = new Function ("ISNA", func_isna);
00085   repo->add (f);
00086   f = new Function ("ISNONTEXT", func_isnottext);
00087   repo->add (f);
00088   f = new Function ("ISNOTTEXT", func_isnottext);
00089   repo->add (f);
00090   f = new Function ("ISNUM", func_isnum);
00091   repo->add (f);
00092   f = new Function ("ISNUMBER", func_isnum);
00093   repo->add (f);
00094   f = new Function ("ISODD", func_isodd);
00095   repo->add (f);
00096   f = new Function ("ISREF", func_isref);
00097   f->setNeedsExtra (true);
00098   repo->add (f);
00099   f = new Function ("ISTEXT", func_istext);
00100   repo->add (f);
00101   f = new Function ("ISTIME", func_istime);
00102   repo->add (f);
00103   f = new Function ("N", func_n);
00104   repo->add (f);
00105   f = new Function ("TYPE", func_type);
00106   f->setAcceptArray ();
00107   repo->add (f);
00108 }
00109 
00110 // Function: INFO
00111 Value func_info (valVector args, ValueCalc *calc, FuncExtra *)
00112 {
00113   QString type = calc->conv()->asString (args[0]).asString().lower();
00114 
00115   if (type == "directory")
00116     return Value (QDir::currentDirPath());
00117 
00118   if (type == "release")
00119     return Value (QString (VERSION));
00120 
00121   if ( type == "numfile" )
00122     return Value ((int) Doc::documents().count());
00123 
00124   if (type == "recalc")
00125   {
00126     QString result;
00127     if (calc->doc()) {
00128       if (calc->doc()->delayCalculation())
00129         result = i18n ("Manual");
00130       else
00131         result = i18n ("Automatic");
00132     }
00133     return Value (result);
00134   }
00135 
00136   if (type == "memavail")
00137     // not supported
00138     return Value::errorVALUE();
00139   if (type == "memused")
00140     // not supported
00141     return Value::errorVALUE();
00142   if (type == "origin")
00143     // not supported
00144     return Value::errorVALUE();
00145 
00146   if (type == "system") {
00147     struct utsname name;
00148     if (uname (&name) >= 0)
00149       return Value (QString (name.sysname));
00150   }
00151 
00152   if (type == "totmem")
00153     // not supported
00154     return Value::errorVALUE();
00155 
00156   if (type == "osversion")
00157   {
00158     struct utsname name;
00159     if (uname (&name) >= 0)
00160     {
00161        QString os = QString("%1 %2 (%3)").arg( name.sysname ).
00162          arg( name.release ).arg( name.machine );
00163        return Value (os);
00164     }
00165   }
00166 
00167   return Value::errorVALUE();
00168 }
00169 
00170 // Function: ISBLANK
00171 Value func_isblank (valVector args, ValueCalc *, FuncExtra *)
00172 {
00173   return Value (args[0].isEmpty());
00174 }
00175 
00176 // Function: ISLOGICAL
00177 Value func_islogical (valVector args, ValueCalc *, FuncExtra *)
00178 {
00179   return Value (args[0].isBoolean());
00180 }
00181 
00182 // Function: ISTEXT
00183 Value func_istext (valVector args, ValueCalc *, FuncExtra *)
00184 {
00185   return Value (args[0].isString());
00186 }
00187 
00188 // Function: ISREF
00189 Value func_isref (valVector, ValueCalc */*calc*/, FuncExtra *e)
00190 {
00191   // no reference ?
00192   if ((e == 0) || (e->ranges[0].col1 == -1) || (e->ranges[0].row1 == -1))
00193     return Value (false);
00194   // if we are here, it is a reference (cell/range)
00195   return Value (true);
00196 }
00197 
00198 // Function: ISNOTTEXT
00199 Value func_isnottext (valVector args, ValueCalc *, FuncExtra *)
00200 {
00201   return Value (args[0].isString() ? false : true);
00202 }
00203 
00204 // Function: ISNUM
00205 Value func_isnum (valVector args, ValueCalc *, FuncExtra *)
00206 {
00207   return Value (args[0].isNumber());
00208 }
00209 
00210 // Function: ISTIME
00211 Value func_istime (valVector args, ValueCalc *, FuncExtra *)
00212 {
00213   return Value ((args[0].format() == Value::fmt_Time)
00214       || (args[0].format() == Value::fmt_DateTime));
00215 }
00216 
00217 // Function: ISDATE
00218 Value func_isdate (valVector args, ValueCalc *, FuncExtra *)
00219 {
00220   return Value ((args[0].format() == Value::fmt_Date)
00221       || (args[0].format() == Value::fmt_DateTime));
00222 }
00223 
00224 // Function: ISODD
00225 Value func_isodd (valVector args, ValueCalc *calc, FuncExtra *)
00226 {
00227   return Value (calc->isEven(args[0]) ? false : true);
00228 }
00229 
00230 // Function: ISEVEN
00231 Value func_iseven (valVector args, ValueCalc *calc, FuncExtra *)
00232 {
00233   return Value (calc->isEven(args[0]));
00234 }
00235 
00236 // Function: ISERR
00237 Value func_iserr (valVector args, ValueCalc *, FuncExtra *)
00238 {
00239   return (args[0].isError() &&
00240       (args[0].errorMessage() != Value::errorNA().errorMessage()));
00241 }
00242 
00243 // Function: ISERROR
00244 Value func_iserror (valVector args, ValueCalc *, FuncExtra *)
00245 {
00246   return args[0].isError();
00247 }
00248 
00249 // Function: ISNA
00250 Value func_isna (valVector args, ValueCalc *, FuncExtra *)
00251 {
00252   return (args[0].isError() &&
00253       (args[0].errorMessage() == Value::errorNA().errorMessage()));
00254 }
00255 
00256 // Function: TYPE
00257 Value func_type (valVector args, ValueCalc *, FuncExtra *)
00258 {
00259   // Returns 1 for numbers, 2 for text, 4 for boolean, 16 for error,
00260   // 64 for arrays
00261   if (args[0].isArray())
00262     return Value (64);
00263   if (args[0].isNumber())
00264     return Value (1);
00265   if (args[0].isString())
00266     return Value (2);
00267   if (args[0].isBoolean())
00268     return Value (4);
00269   if (args[0].isError())
00270     return Value (16);
00271   
00272   // something else ?
00273   return Value (0);
00274 }
00275 
00276 Value func_filename (valVector, ValueCalc *calc, FuncExtra *)
00277 {
00278   return Value (calc->doc()->url().prettyURL());
00279 }
00280 
00281 // Function: N
00282 Value func_n (valVector args, ValueCalc *calc, FuncExtra *)
00283 {
00284   return calc->conv()->asFloat (args[0]);
00285 }
KDE Home | KDE Accessibility Home | Description of Access Keys