1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* This file is part of the KDE project
Copyright (C) 1998-2002 The KSpread Team <calligra-devel@kde.org>
Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; only
version 2 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// built-in logical functions
#include "LogicModule.h"
#include "Function.h"
#include "FunctionModuleRegistry.h"
#include "ValueCalc.h"
#include "ValueConverter.h"
#include <KLocale>
using namespace Calligra::Sheets;
// prototypes (sorted alphabetically)
Value func_and(valVector args, ValueCalc *calc, FuncExtra *);
Value func_false(valVector args, ValueCalc *calc, FuncExtra *);
Value func_if(valVector args, ValueCalc *calc, FuncExtra *);
Value func_iferror(valVector args, ValueCalc *calc, FuncExtra *);
Value func_ifna(valVector args, ValueCalc *calc, FuncExtra *);
Value func_nand(valVector args, ValueCalc *calc, FuncExtra *);
Value func_nor(valVector args, ValueCalc *calc, FuncExtra *);
Value func_not(valVector args, ValueCalc *calc, FuncExtra *);
Value func_or(valVector args, ValueCalc *calc, FuncExtra *);
Value func_true(valVector args, ValueCalc *calc, FuncExtra *);
Value func_xor(valVector args, ValueCalc *calc, FuncExtra *);
CALLIGRA_SHEETS_EXPORT_FUNCTION_MODULE("logic", LogicModule)
LogicModule::LogicModule(QObject* parent, const QVariantList&)
: FunctionModule(parent)
{
Function *f;
f = new Function("FALSE", func_false);
f->setParamCount(0);
add(f);
f = new Function("TRUE", func_true);
f->setParamCount(0);
add(f);
f = new Function("NOT", func_not);
f->setParamCount(1);
add(f);
f = new Function("AND", func_and);
f->setParamCount(1, -1);
f->setAcceptArray();
add(f);
f = new Function("NAND", func_nand);
f->setParamCount(1, -1);
f->setAcceptArray();
add(f);
f = new Function("NOR", func_nor);
f->setParamCount(1, -1);
f->setAcceptArray();
add(f);
f = new Function("OR", func_or);
f->setParamCount(1, -1);
f->setAcceptArray();
add(f);
f = new Function("XOR", func_xor);
f->setParamCount(1, -1);
f->setAcceptArray();
add(f);
f = new Function("IF", func_if);
f->setParamCount(2, 3);
add(f);
f = new Function("IFERROR", func_iferror);
f->setParamCount(2);
add(f);
f = new Function("IFNA", func_ifna);
f->setParamCount(2);
add(f);
}
QString LogicModule::descriptionFileName() const
{
return QString("logic.xml");
}
// helper for most logical functions
static bool asBool(Value val, ValueCalc *calc, bool* ok = 0)
{
return calc->conv()->asBoolean(val, ok).asBoolean();
}
///////////////////////////////////////////////////////////////////////////////
//
// ArrayWalker: AND
//
void awAnd(ValueCalc *calc, Value &res, Value value, Value)
{
if (res.asBoolean())
res = Value(asBool(value, calc));
}
//
// ArrayWalker: OR
//
void awOr(ValueCalc *calc, Value &res, Value value, Value)
{
if (!res.asBoolean())
res = Value(asBool(value, calc));
}
//
// ArrayWalker: XOR
//
void awXor(ValueCalc *calc, Value &count, Value value, Value)
{
if (asBool(value, calc))
count = Value(count.asInteger() + 1);
}
///////////////////////////////////////////////////////////////////////////////
//
// Function: AND
//
Value func_and(valVector args, ValueCalc *calc, FuncExtra *)
{
Value result(true);
int cnt = args.count();
for (int i = 0; i < cnt; ++i) {
if (args[i].isError())
return args[i];
}
for (int i = 0; i < cnt; ++i) {
calc->arrayWalk(args[i], result, awAnd, Value(0));
if (! result.asBoolean())
// if any value is false, return false
return result;
}
// nothing is false -> return true
return result;
}
//
// Function: FALSE
//
Value func_false(valVector, ValueCalc *, FuncExtra *)
{
return Value(false);
}
//
// Function: IF
//
Value func_if(valVector args, ValueCalc *calc, FuncExtra *)
{
if ((args[0].isError()))
return args[0];
bool ok = true;
bool guard = asBool(args[0], calc, &ok);
if (!ok)
return Value::errorVALUE();
if (guard)
return args[1];
// evaluated to false
if (args.count() == 3) {
if (args[2].isNull()) {
return Value(0);
} else {
return args[2];
}
} else {
// only two arguments
return Value(false);
}
}
//
// Function: IFERROR
//
Value func_iferror(valVector args, ValueCalc *, FuncExtra *)
{
if (args[0].isError())
return args[1];
return args[0];
}
//
// Function: IFNA
//
Value func_ifna(valVector args, ValueCalc *, FuncExtra *)
{
if (args[0] == Value::errorNA())
return args[1];
return args[0];
}
//
// Function: NAND
//
Value func_nand(valVector args, ValueCalc *calc, FuncExtra *extra)
{
// AND in reverse
return Value(! func_and(args, calc, extra).asBoolean());
}
//
// Function: NOR
//
Value func_nor(valVector args, ValueCalc *calc, FuncExtra *extra)
{
// OR in reverse
return Value(! func_or(args, calc, extra).asBoolean());
}
//
// Function: NOT
//
Value func_not(valVector args, ValueCalc *calc, FuncExtra *)
{
if (args[0].isError())
return args[0];
bool ok = true;
bool val = !asBool(args[0], calc, &ok);
if (!ok) return Value::errorVALUE();
return Value(val);
}
//
// Function: OR
//
Value func_or(valVector args, ValueCalc *calc, FuncExtra *)
{
Value result(false);
int cnt = args.count();
for (int i = 0; i < cnt; ++i) {
if (args[i].isError())
return args[i];
}
for (int i = 0; i < cnt; ++i) {
calc->arrayWalk(args[i], result, awOr, Value(0));
if (result.asBoolean())
// if any value is true, return true
return result;
}
// nothing is true -> return false
return result;
}
//
// Function: TRUE
//
Value func_true(valVector, ValueCalc *, FuncExtra *)
{
return Value(true);
}
//
// Function: XOR
//
Value func_xor(valVector args, ValueCalc *calc, FuncExtra *)
{
// exclusive OR - odd number of values must be true
int cnt = args.count();
Value count(0);
for (int i = 0; i < cnt; ++i) {
if (args[i].isError())
return args[i];
}
for (int i = 0; i < cnt; ++i)
calc->arrayWalk(args[i], count, awXor, Value(0));
return Value((count.asInteger() & 1) == 1);
}
#include "LogicModule.moc"
|