[Kst] kdeextragear-2/kst/kst

George Staikos staikos at kde.org
Fri Mar 18 21:40:38 CET 2005


CVS commit by staikos: 

refactor to reuse later


  M +1 -22     datarangewidget.ui.h   1.6
  M +39 -0     enodes.cpp   1.45
  M +6 -0      enodes.h   1.30


--- kdeextragear-2/kst/kst/datarangewidget.ui.h  #1.5:1.6
@@ -8,8 +8,4 @@
 *****************************************************************************/
 
-extern "C" int yyparse();
-extern "C" void *ParsedEquation;
-extern "C" struct yy_buffer_state *yy_scan_string(const char*);
-
 
 void KstDataRange::init()
@@ -127,22 +123,5 @@ void KstDataRange::setNValue(double v)
 double KstDataRange::interpret(const char *txt)
 {
-    yy_scan_string(txt);
-    int rc = yyparse();
-    if (rc == 0) {
-        QMutexLocker ml(&Equation::mutex());
-        Equation::Node *eq = static_cast<Equation::Node*>(ParsedEquation);
-        ParsedEquation = 0L;
-        Equation::Context ctx;
-        ctx.sampleCount = 2;
-        ctx.noPoint = KST::NOPOINT;
-        ctx.x = 0.0;
-        ctx.xVector = 0L;
-        Equation::FoldVisitor vis(&ctx, &eq);
-        double v = eq->value(&ctx);
-        delete eq;
-        return v;
-    } else {
-        return 0.0;
-    }
+    return Equation::interpret(txt);
 }
 

--- kdeextragear-2/kst/kst/enodes.cpp  #1.44:1.45
@@ -34,4 +34,8 @@
 #include "plugincollection.h"
 
+extern "C" int yyparse();
+extern "C" void *ParsedEquation;
+extern "C" struct yy_buffer_state *yy_scan_string(const char*);
+
 using namespace Equation;
 
@@ -43,4 +47,39 @@ QMutex& Equation::mutex() {
 
 
+double Equation::interpret(const char *txt, bool *ok) {
+  if (!txt || !*txt) {
+    if (ok) {
+      *ok = false;
+    }
+    return 0.0;
+  }
+
+  yy_scan_string(txt);
+  int rc = yyparse();
+  if (rc == 0) {
+    QMutexLocker ml(&mutex());
+    Equation::Node *eq = static_cast<Equation::Node*>(ParsedEquation);
+    ParsedEquation = 0L;
+    Equation::Context ctx;
+    ctx.sampleCount = 2;
+    ctx.noPoint = KST::NOPOINT;
+    ctx.x = 0.0;
+    ctx.xVector = 0L;
+    Equation::FoldVisitor vis(&ctx, &eq);
+    double v = eq->value(&ctx);
+    delete eq;
+    if (ok) {
+      *ok = true;
+    }
+    return v;
+  } else {
+    if (ok) {
+      *ok = false;
+    }
+    return 0.0;
+  }
+}
+
+
 Node::Node() {
   _parentheses = false;

--- kdeextragear-2/kst/kst/enodes.h  #1.29:1.30
@@ -28,6 +28,12 @@ class Plugin;
 namespace Equation {
 
+  /* Global lock for the parser */
   QMutex& mutex();
 
+  /*    Evaluate the expression @p txt and returns the value as a double.
+   *    Returns the value, or 0.0 and sets ok = false on error.
+   */
+  double interpret(const char *txt, bool *ok = 0L);
+
   class Context {
     public:




More information about the Kst mailing list