[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    KSpread logic operations
From:       Ivor Hewitt <ivor () ivor ! org>
Date:       2006-04-26 4:25:48
Message-ID: 200604260525.49182.ivor () ivor ! org
[Download RAW message or body]

Hi!
I like to use kspread to 'doodle' while I develop but always wanted to be
able to do binary logic operations to calculate/decode various lookup
values rather than writing little scripts/c apps.
As a result I added a handful of binary math functions to kspread to help me
out and I wondered if anyone liked the idea - or had any suggestions for
improvements or other additions.

Also... having never done any koffice coding before, any comments or noting
mistakes welcome.

Cheers, 
Ivor

["kspread_prog_func.diff" (text/x-diff)]

Index: extensions/programming.xml
===================================================================
--- extensions/programming.xml	(revision 0)
+++ extensions/programming.xml	(revision 0)
@@ -0,0 +1,119 @@
+<!DOCTYPE KSpreadFunctions>
+<KSpreadFunctions>
+    
+    <Group>
+        <GroupName>Programming</GroupName>
+        
+        <Function>
+            <Name>LAND</Name>
+            <Type>Int</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The LAND() function returns logical AND of its two \
parameters.</Text> +                <Syntax>LAND(value;value)</Syntax>
+                <Example>LAND(1;3) returns 1</Example>
+            </Help>
+        </Function>
+        
+        <Function>
+            <Name>LOR</Name>
+            <Type>Int</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The LOR() function returns logical OR of its two \
parameters.</Text> +                <Syntax>LOR(value;value)</Syntax>
+                <Example>LOR(1;2) returns 3</Example>
+            </Help>
+        </Function>
+        
+        <Function>
+            <Name>LXOR</Name>
+            <Type>Integer</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The LXOR() function returns logical exclusive OR of its two \
parameters.</Text> +                <Syntax>LXOR(value;value)</Syntax>
+                <Example>LXOR(1;3) returns 2</Example>
+            </Help>
+        </Function>
+        
+        <Function>
+            <Name>LSHL</Name>
+            <Type>Integer</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter>
+                <Comment>Shift</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The LSHL() function returns the value parameter binary shifted \
left by the shift parameter.</Text> +                \
<Syntax>LSHL(value;shift)</Syntax> +                <Example>LSHL(1;2) returns \
4</Example> +            </Help>
+        </Function>
+        
+        
+        <Function>
+            <Name>LSHR</Name>
+            <Type>Integer</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter>
+                <Comment>Shift</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The LSHR() function returns the value parameter binary shifted \
right by the shift parameter.</Text> +                \
<Syntax>LSHR(value;shift)</Syntax> +                <Example>LSHR(5;1) returns \
2</Example> +            </Help>
+        </Function>
+        
+        <Function>
+            <Name>NOT</Name>
+            <Type>Integer</Type>
+            <Parameter>
+                <Comment>Value</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Parameter optional="true">
+                <Comment>Width</Comment>
+                <Type>Int</Type>
+            </Parameter>
+            <Help>
+                <Text>The NOT() function returns the binary complement of the value. \
If the Width parameter is specified it determines the bit width of the operation. +   \
</Text> +                <Syntax>NOT(value;width)</Syntax>
+                <Example>NOT(5; 8) returns 250</Example>
+            </Help>
+        </Function>
+        
+    </Group>
+    
+</KSpreadFunctions>
Index: extensions/Makefile.am
===================================================================
--- extensions/Makefile.am	(revision 533600)
+++ extensions/Makefile.am	(working copy)
@@ -1,6 +1,6 @@
 
 scripts_DATA = builtin.xml financial.xml trig.xml conversion.xml datetime.xml \
 	database.xml statistical.xml text.xml logic.xml math.xml information.xml \
-	engineering.xml reference.xml
+	engineering.xml reference.xml programming.xml
 scriptsdir = $(kde_datadir)/kspread/extensions
 
Index: Makefile.am
===================================================================
--- Makefile.am	(revision 533600)
+++ Makefile.am	(working copy)
@@ -16,7 +16,7 @@
 	kspread_functions_engineering.cc kspread_functions_financial.cc \
kspread_functions_helper.cc \  kspread_functions_information.cc \
kspread_functions_logic.cc kspread_functions_math.cc \  \
kspread_functions_reference.cc kspread_functions_statistical.cc \
                kspread_functions_text.cc \
-	kspread_functions_trig.cc kspread_handler.cc kspread_locale.cc kspread_map.cc \
+	kspread_functions_trig.cc kspread_functions_prog.cc kspread_handler.cc \
kspread_locale.cc kspread_map.cc \  kspread_numformat.cc kspread_object.cc \
kspread_style.cc kspread_style_manager.cc \  kspread_undo.cc kspread_value.cc \
kspread_view.cc kspread_sheet.cc kspread_sheetprint.cc \  kspread_util.cc \
                kspread_toolbox.cc 	valuecalc.cc 	valueconverter.cc \
                valueformatter.cc \
Index: functions.cc
===================================================================
--- functions.cc	(revision 533600)
+++ functions.cc	(working copy)
@@ -175,6 +175,7 @@
 void RegisterStatisticalFunctions();
 void RegisterTextFunctions();
 void RegisterTrigFunctions();
+void RegisterProgFunctions();
 
 
 static KStaticDeleter<FunctionRepository> fr_sd;
@@ -203,6 +204,7 @@
     RegisterStatisticalFunctions();
     RegisterTextFunctions();
     RegisterTrigFunctions();
+    RegisterProgFunctions();
   
     kdDebug() << "Functions registered, loading descriptions" << endl;
   
Index: kspread_functions_prog.cc
===================================================================
--- kspread_functions_prog.cc	(revision 0)
+++ kspread_functions_prog.cc	(revision 0)
@@ -0,0 +1,123 @@
+/* This file is part of the KDE project
+   Copyright (C) 1998-2002 The KSpread Team
+                           www.koffice.org/kspread
+   Copyright (C) 2006 Ivor Hewitt <ivor@ivor.org>
+
+   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; either
+   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 "functions.h"
+#include "valuecalc.h"
+#include "valueconverter.h"
+
+using namespace KSpread;
+
+// prototypes (sorted alphabetically)
+
+Value func_land (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_lnot (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_lor (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_lshl (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_lshr (valVector args, ValueCalc *calc, FuncExtra *);
+Value func_lxor (valVector args, ValueCalc *calc, FuncExtra *);
+
+// registers all programmatic functions
+
+void RegisterProgFunctions()
+{
+  FunctionRepository* repo = FunctionRepository::self();
+  Function *f;
+
+  f = new Function("LSHR", func_lshr);
+  f->setParamCount(2);
+  repo->add(f);
+
+  f = new Function("LSHL", func_lshl);
+  f->setParamCount(2);
+  repo->add(f);
+
+  f = new Function("LAND", func_land);
+  f->setParamCount(2);
+  repo->add(f);
+
+  f = new Function("LOR", func_lor);
+  f->setParamCount(2);
+  repo->add(f);
+  
+  f = new Function("LXOR", func_lxor);
+  f->setParamCount(2);
+  repo->add(f);
+
+  f = new Function("LNOT", func_lnot);
+  f->setParamCount(1,2);
+  repo->add(f);
+
+}
+
+Value func_lshr (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+
+  return Value ((int)( a >> b ));
+}
+
+Value func_lshl (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+
+  return Value ((int)( a << b ));
+}
+Value func_land (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+
+  return Value ((int)( a & b ));
+}
+Value func_lor (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+
+  return Value ((int)( a | b ));
+}
+
+Value func_lxor (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+
+  return Value ((int)( a^b ));
+}
+
+Value func_lnot (valVector args, ValueCalc *calc, FuncExtra *)
+{
+  int cnt = args.count();
+  unsigned int a = calc->conv()->asInteger (args[0]).asInteger();
+  unsigned int mask = ~0;
+  if (cnt>1)
+  {
+      unsigned int b = calc->conv()->asInteger (args[1]).asInteger();
+      mask <<= b;
+      mask = ~mask;
+  }
+  return Value ((int)( ~a & mask ));
+}
+



_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic