kspread

kspread_functions_logic.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 logical functions
00023 
00024 #include "functions.h"
00025 #include "valuecalc.h"
00026 #include "valueconverter.h"
00027 
00028 using namespace KSpread;
00029 
00030 // prototypes (sorted alphabetically)
00031 Value func_and (valVector args, ValueCalc *calc, FuncExtra *);
00032 Value func_false (valVector args, ValueCalc *calc, FuncExtra *);
00033 Value func_if (valVector args, ValueCalc *calc, FuncExtra *);
00034 Value func_nand (valVector args, ValueCalc *calc, FuncExtra *);
00035 Value func_nor (valVector args, ValueCalc *calc, FuncExtra *);
00036 Value func_not (valVector args, ValueCalc *calc, FuncExtra *);
00037 Value func_or (valVector args, ValueCalc *calc, FuncExtra *);
00038 Value func_true (valVector args, ValueCalc *calc, FuncExtra *);
00039 Value func_xor (valVector args, ValueCalc *calc, FuncExtra *);
00040 
00041 // registers all logic functions
00042 void RegisterLogicFunctions()
00043 {
00044   FunctionRepository* repo = FunctionRepository::self();
00045   Function *f;
00046 
00047   f = new Function ("FALSE", func_false);
00048   f->setParamCount (0);
00049   repo->add (f);
00050   f = new Function ("TRUE", func_true);
00051   f->setParamCount (0);
00052   repo->add (f);
00053   f = new Function ("NOT", func_not);
00054   f->setParamCount (1);
00055   repo->add (f);
00056   f = new Function ("AND", func_and);
00057   f->setParamCount (2, -1);
00058   repo->add (f);
00059   f = new Function ("NAND", func_nand);
00060   f->setParamCount (2, -1);
00061   repo->add (f);
00062   f = new Function ("NOR", func_nor);
00063   f->setParamCount (2, -1);
00064   repo->add (f);
00065   f = new Function ("OR", func_or);
00066   f->setParamCount (2, -1);
00067   repo->add (f);
00068   f = new Function ("XOR", func_xor);
00069   f->setParamCount (2, -1);
00070   repo->add (f);
00071   f = new Function ("IF", func_if);
00072   f->setParamCount (3);
00073   repo->add (f);
00074 }
00075 
00076 // Function: FALSE
00077 Value func_false (valVector, ValueCalc *, FuncExtra *)
00078 {
00079   return Value (false);
00080 }
00081 
00082 // Function: TRUE
00083 Value func_true (valVector, ValueCalc *, FuncExtra *)
00084 {
00085   return Value (true);
00086 }
00087 
00088 // helper for most logical functions
00089 bool asBool (Value val, ValueCalc *calc)
00090 {
00091   return calc->conv()->asBoolean (val).asBoolean ();
00092 }
00093 
00094 // Function: NOT
00095 Value func_not (valVector args, ValueCalc *calc, FuncExtra *)
00096 {
00097   bool val = asBool (args[0], calc) ? false : true;
00098   return Value (val);
00099 }
00100 
00101 // Function: OR
00102 Value func_or (valVector args, ValueCalc *calc, FuncExtra *)
00103 {
00104   int cnt = args.count();
00105   for (int i = 0; i < cnt; ++i)
00106     if (asBool (args[i], calc))
00107       // if any value is true, return true
00108       return Value (true);
00109   // nothing is true -> return false
00110   return Value (false);
00111 }
00112 
00113 // Function: NOR
00114 Value func_nor (valVector args, ValueCalc *calc, FuncExtra *)
00115 {
00116   // OR in reverse
00117   int cnt = args.count();
00118   for (int i = 0; i < cnt; ++i)
00119     if (asBool (args[i], calc))
00120       // if any value is true, return false
00121       return Value (false);
00122   // nothing is true -> return true
00123   return Value (true);
00124 }
00125 
00126 // Function: AND
00127 Value func_and (valVector args, ValueCalc *calc, FuncExtra *)
00128 {
00129   int cnt = args.count();
00130   for (int i = 0; i < cnt; ++i)
00131     if (!asBool (args[i], calc))
00132       // if any value is false, return false
00133       return Value (false);
00134   // nothing is false -> return true
00135   return Value (true);
00136 }
00137 
00138 // Function: NAND
00139 Value func_nand (valVector args, ValueCalc *calc, FuncExtra *)
00140 {
00141   // AND in reverse
00142   int cnt = args.count();
00143   for (int i = 0; i < cnt; ++i)
00144     if (!asBool (args[i], calc))
00145       // if any value is false, return true
00146       return Value (true);
00147   // nothing is false -> return false
00148   return Value (false);
00149 }
00150 
00151 // Function: XOR
00152 Value func_xor (valVector args, ValueCalc *calc, FuncExtra *)
00153 {
00154   // exclusive OR - exactly one value must be true
00155   int cnt = args.count();
00156   int count = 0;
00157   for (int i = 0; i < cnt; ++i)
00158     if (asBool (args[i], calc))
00159       count++;
00160   return Value (count == 1);
00161 }
00162 
00163 // Function: IF
00164 Value func_if (valVector args, ValueCalc *calc, FuncExtra *)
00165 {
00166   if (asBool (args[0], calc))
00167     return args[1];
00168   else
00169     return args[2];
00170 }
KDE Home | KDE Accessibility Home | Description of Access Keys