KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.7KB

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