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.

184 lines
5.0KB

  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 logical functions
  19. #include "functions.h"
  20. #include "valuecalc.h"
  21. #include "valueconverter.h"
  22. using namespace KSpread;
  23. // prototypes (sorted alphabetically)
  24. Value func_and (valVector args, ValueCalc *calc, FuncExtra *);
  25. Value func_false (valVector args, ValueCalc *calc, FuncExtra *);
  26. Value func_if (valVector args, ValueCalc *calc, FuncExtra *);
  27. Value func_nand (valVector args, ValueCalc *calc, FuncExtra *);
  28. Value func_nor (valVector args, ValueCalc *calc, FuncExtra *);
  29. Value func_not (valVector args, ValueCalc *calc, FuncExtra *);
  30. Value func_or (valVector args, ValueCalc *calc, FuncExtra *);
  31. Value func_true (valVector args, ValueCalc *calc, FuncExtra *);
  32. Value func_xor (valVector args, ValueCalc *calc, FuncExtra *);
  33. // registers all logic functions
  34. void RegisterLogicFunctions()
  35. {
  36. FunctionRepository* repo = FunctionRepository::self();
  37. Function *f;
  38. f = new Function ("FALSE", func_false);
  39. f->setParamCount (0);
  40. repo->add (f);
  41. f = new Function ("TRUE", func_true);
  42. f->setParamCount (0);
  43. repo->add (f);
  44. f = new Function ("NOT", func_not);
  45. f->setParamCount (1);
  46. repo->add (f);
  47. f = new Function ("AND", func_and);
  48. f->setParamCount (1, -1);
  49. f->setAcceptArray ();
  50. repo->add (f);
  51. f = new Function ("NAND", func_nand);
  52. f->setParamCount (1, -1);
  53. f->setAcceptArray ();
  54. repo->add (f);
  55. f = new Function ("NOR", func_nor);
  56. f->setParamCount (1, -1);
  57. f->setAcceptArray ();
  58. repo->add (f);
  59. f = new Function ("OR", func_or);
  60. f->setParamCount (1, -1);
  61. f->setAcceptArray ();
  62. repo->add (f);
  63. f = new Function ("XOR", func_xor);
  64. f->setParamCount (1, -1);
  65. f->setAcceptArray ();
  66. repo->add (f);
  67. f = new Function ("IF", func_if);
  68. f->setParamCount (3);
  69. repo->add (f);
  70. }
  71. // Function: FALSE
  72. Value func_false (valVector, ValueCalc *, FuncExtra *)
  73. {
  74. return Value (false);
  75. }
  76. // Function: TRUE
  77. Value func_true (valVector, ValueCalc *, FuncExtra *)
  78. {
  79. return Value (true);
  80. }
  81. // helper for most logical functions
  82. bool asBool (Value val, ValueCalc *calc)
  83. {
  84. return calc->conv()->asBoolean (val).asBoolean ();
  85. }
  86. // Function: NOT
  87. Value func_not (valVector args, ValueCalc *calc, FuncExtra *)
  88. {
  89. bool val = asBool (args[0], calc) ? false : true;
  90. return Value (val);
  91. }
  92. // Function: OR
  93. void awOr (ValueCalc *calc, Value &res, Value value, Value)
  94. {
  95. if (! res.asBoolean())
  96. res = Value ( asBool (value, calc) );
  97. }
  98. Value func_or (valVector args, ValueCalc *calc, FuncExtra *)
  99. {
  100. Value result(false);
  101. int cnt = args.count();
  102. for (int i = 0; i < cnt; ++i) {
  103. calc->arrayWalk (args[i], result, awOr, 0);
  104. if (result.asBoolean())
  105. // if any value is true, return true
  106. return result;
  107. }
  108. // nothing is true -> return false
  109. return result;
  110. }
  111. // Function: NOR
  112. Value func_nor (valVector args, ValueCalc *calc, FuncExtra *extra)
  113. {
  114. // OR in reverse
  115. return Value(! func_or(args, calc, extra).asBoolean());
  116. }
  117. // Function: AND
  118. void awAnd (ValueCalc *calc, Value &res, Value value, Value)
  119. {
  120. if (res.asBoolean())
  121. res = Value ( asBool (value, calc) );
  122. }
  123. Value func_and (valVector args, ValueCalc *calc, FuncExtra *)
  124. {
  125. Value result(true);
  126. int cnt = args.count();
  127. for (int i = 0; i < cnt; ++i) {
  128. calc->arrayWalk (args[i], result, awAnd, 0);
  129. if (! result.asBoolean())
  130. // if any value is false, return false
  131. return result;
  132. }
  133. // nothing is false -> return true
  134. return result;
  135. }
  136. // Function: NAND
  137. Value func_nand (valVector args, ValueCalc *calc, FuncExtra *extra)
  138. {
  139. // AND in reverse
  140. return Value(! func_and(args, calc, extra).asBoolean());
  141. }
  142. // Function: XOR
  143. void awXor (ValueCalc *calc, Value &count, Value value, Value)
  144. {
  145. if (asBool (value, calc))
  146. count = Value( count.asInteger() + 1 );
  147. }
  148. Value func_xor (valVector args, ValueCalc *calc, FuncExtra *)
  149. {
  150. // exclusive OR - exactly one value must be true
  151. int cnt = args.count();
  152. Value count(0);
  153. for (int i = 0; i < cnt; ++i)
  154. calc->arrayWalk (args[i], count, awXor, 0);
  155. return Value (count.asInteger() == 1);
  156. }
  157. // Function: IF
  158. Value func_if (valVector args, ValueCalc *calc, FuncExtra *)
  159. {
  160. if (asBool (args[0], calc))
  161. return args[1];
  162. else
  163. return args[2];
  164. }