[gcalctool] Make MP code not dependent on any other gcalctool code



commit 323b1ae7b43748570f7f977a0cfa3a9991a846c0
Author: Robert Ancell <robert ancell gmail com>
Date:   Tue Aug 11 01:14:27 2009 +0100

    Make MP code not dependent on any other gcalctool code

 src/calctool.c    |   29 -----------------------------
 src/calctool.h    |    4 ----
 src/functions.c   |    4 ++--
 src/mp-equation.c |   10 +++++-----
 src/mp-equation.h |    1 +
 src/mp.c          |   26 +++++++++++++++++++++-----
 src/mp.h          |    7 +++++++
 src/unittest.c    |   22 +++++++++++-----------
 8 files changed, 47 insertions(+), 56 deletions(-)
---
diff --git a/src/calctool.c b/src/calctool.c
index 4a64995..c69c451 100644
--- a/src/calctool.c
+++ b/src/calctool.c
@@ -24,7 +24,6 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <sys/types.h>
-#include <math.h>
 #include <glib-object.h>
 
 #include "calctool.h"
@@ -43,31 +42,6 @@ time_t time();
 static CalculatorVariables calc_state;
 CalculatorVariables *v;
 
-/* Calctools' customised math library error-handling routine. */
-void
-doerr(char *errmes)
-{
-    v->math_error = -MPMATH_ERR;
-    free(v->math_error_text);
-    v->math_error_text = strdup(errmes);
-}
-
-/* Default math library exception handling routine. */
-
-/*ARGSUSED*/
-int
-matherr(struct exception *exc)
-{
-    char text[MAXLINE];
-    
-    /* Translators: Error displayed to user when the math library reports an
-     * error */
-    snprintf(text, MAXLINE, _("Error in math library function %s"), exc->name);
-    doerr(text);
-
-    return 1;
-}
-
 static void
 version()
 {
@@ -207,9 +181,6 @@ init_state(void)
     v->tsep          = get_tsep();     /* Locale specific thousands separator. */
     v->tsep_count    = get_tsep_count();
     
-    v->math_error = 0;
-    v->math_error_text = strdup("");
-   
     if (get_int_resource(R_ACCURACY, &i))
         v->accuracy = i;
     else
diff --git a/src/calctool.h b/src/calctool.h
index b1e8ea6..c13c744 100644
--- a/src/calctool.h
+++ b/src/calctool.h
@@ -74,12 +74,8 @@ typedef struct {
     int accuracy;             /* Number of digits precision. */
 
     int error;                /* true if there is a display error */
-    int math_error;           /* Math error */
-    char *math_error_text;    /* Text for math error */
 } CalculatorVariables;
 
 extern CalculatorVariables *v; /* Calctool variables and options. */
 
-void doerr(char *);
-
 #endif /*CALCTOOL_H*/
diff --git a/src/functions.c b/src/functions.c
index 8912254..4b00dcf 100644
--- a/src/functions.c
+++ b/src/functions.c
@@ -620,8 +620,8 @@ do_expression(int function, int arg, int cursor_start, int cursor_end)
                        message = _("Unknown function");
                        break;
 
-                    case -MPMATH_ERR:
-                        message = v->math_error_text;
+                    case -PARSER_ERR_MP:
+                        message = mp_get_error();
                         break;
 
                     default:
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 022a520..9cffbbd 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -25,7 +25,6 @@
 #include "mp-equation-private.h"
 #include "mp-equation-parser.h"
 #include "mp-equation-lexer.h"
-#include "calctool.h" // FIXME v->math_error
 
 extern int _mp_equation_parse(yyscan_t yyscanner);
 
@@ -160,7 +159,7 @@ mp_equation_parse(const char *expression, MPEquationOptions *options, MPNumber *
     YY_BUFFER_STATE buffer;
 
     if (!(expression && result) || strlen(expression) == 0)
-        return(-EINVAL);
+        return -EINVAL;
 
     memset(&state, 0, sizeof(MPEquationParserState));
     state.options = options;
@@ -168,7 +167,8 @@ mp_equation_parse(const char *expression, MPEquationOptions *options, MPNumber *
     state.set_variable = set_variable;
     state.get_function = get_function;
     state.error = 0;
-    v->math_error = 0; // FIXME: Move up one level
+
+    mp_clear_error();
 
     _mp_equation_lex_init_extra(&state, &yyscanner);
     buffer = _mp_equation__scan_string(expression, yyscanner);
@@ -186,8 +186,8 @@ mp_equation_parse(const char *expression, MPEquationOptions *options, MPNumber *
     if (state.error)
         return state.error;
 
-    if (v->math_error)
-        return v->math_error;
+    if (mp_get_error())
+        return -PARSER_ERR_MP;
 
     mp_set_from_mp(&state.ret, result);
 
diff --git a/src/mp-equation.h b/src/mp-equation.h
index 80a0be3..4895f09 100644
--- a/src/mp-equation.h
+++ b/src/mp-equation.h
@@ -33,6 +33,7 @@
 #define PARSER_ERR_UNKNOWN_VARIABLE 6
 #define PARSER_ERR_UNKNOWN_FUNCTION 7
 #define PARSER_ERR_INVALID_BASE     8
+#define PARSER_ERR_MP               9
 
 /* Options for parser */
 typedef struct {
diff --git a/src/mp.c b/src/mp.c
index c18a73d..e75fd2f 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -27,11 +27,10 @@
 
 #include "mp.h"
 #include "mp-internal.h"
-#include "calctool.h" // FIXME: Required for doerr() and MAXLINE
-
 
 // FIXME: Re-add overflow and underflow detection
 
+char *mp_error = NULL;
 
 /*  THIS ROUTINE IS CALLED WHEN AN ERROR CONDITION IS ENCOUNTERED, AND
  *  AFTER A MESSAGE HAS BEEN WRITTEN TO STDERR.
@@ -39,13 +38,30 @@
 void
 mperr(const char *format, ...)
 {
-    char text[MAXLINE];
+    char text[1024];
     va_list args;
     
     va_start(args, format);
-    vsnprintf(text, MAXLINE, format, args);
+    vsnprintf(text, 1024, format, args);
     va_end(args);
-    doerr(text);
+
+    if (mp_error)
+        free(mp_error);
+    mp_error = strdup(text);
+}
+
+
+const char *mp_get_error()
+{
+    return mp_error;
+}
+
+
+void mp_clear_error()
+{
+    if (mp_error)
+        free(mp_error);
+    mp_error = NULL;
 }
 
 
diff --git a/src/mp.h b/src/mp.h
index 7511fe5..2478724 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -70,6 +70,13 @@ typedef enum
     MP_GRADIANS
 } MPAngleUnit;
 
+/* Returns error string or NULL if no error */
+// FIXME: Global variable
+const char *mp_get_error();
+
+/* Clear any current error */
+void mp_clear_error();
+
 /* Returns:
  *  0 if x == y
  * <0 if x < y
diff --git a/src/unittest.c b/src/unittest.c
index 85bb38b..39ce089 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -242,8 +242,8 @@ test_parser()
     test("1÷4", "0.25", 0);
     test("1÷3", "0.333333333", 0);
     test("2÷3", "0.666666667", 0);
-    test("1÷0", "", -20001);
-    test("0÷0", "", -20001);
+    test("1÷0", "", -9);
+    test("0÷0", "", -9);
     
     /* Precision */
     test("1000000000000000â??1000000000000000", "0", 0);
@@ -270,9 +270,9 @@ test_parser()
     test("1!", "1", 0);
     test("5!", "120", 0);
     test("69!", "171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000", 0);
-    test("0.1!", "", -20001);
+    test("0.1!", "", -9);
     test("â??1!", "â??1", 0);
-    test("(â??1)!", "", -20001);
+    test("(â??1)!", "", -9);
     test("â??(1!)", "â??1", 0);
 
     /* Powers */
@@ -283,7 +283,7 @@ test_parser()
     test("2^1", "2", 0);
     test("2^2", "4", 0);
     test("2�¹", "0.5", 0);
-    //test("2â?»", "", -20001); // FIXME: Maybe an error in bison?
+    //test("2â?»", "", -9); // FIXME: Maybe an error in bison?
     test("2^â??1", "0.5", 0);
     test("2^(â??1)", "0.5", 0);
     test("â??10^2", "â??100", 0);
@@ -307,7 +307,7 @@ test_parser()
     test("Sqrt(2)", "1.414213562", 0);
     test("4^0.5", "2", 0);
     test("2^0.5", "1.414213562", 0);
-    test("(â??4)^0.5", "", -20001);
+    test("(â??4)^0.5", "", -9);
     test("(â??8)^(1÷3)", "â??2", 0);
     
     test("0 mod 7", "0", 0);
@@ -329,8 +329,8 @@ test_parser()
     test("abs 1", "1", 0);
     test("abs â??1", "1", 0);
 
-    test("log â??1", "", -20001);
-    test("log 0", "", -20001);
+    test("log â??1", "", -9);
+    test("log 0", "", -9);
     test("log 1", "0", 0);
     test("log 2", "0.301029996", 0);
     test("log 10", "1", 0);
@@ -338,8 +338,8 @@ test_parser()
     test("logâ?? 2", "1", 0);
     test("2 log 2", "0.602059991", 0);
 
-    test("ln â??1", "", -20001);
-    test("ln 0", "", -20001);
+    test("ln â??1", "", -9);
+    test("ln 0", "", -9);
     test("ln 1", "0", 0);
     test("ln 2", "0.693147181", 0);
     test("ln e", "1", 0);
@@ -364,7 +364,7 @@ test_parser()
 
     test("tan 0", "0", 0);
     test("tan 10 â?? sin 10÷cos 10", "0", 0);
-    test("tan 90", "", -20001);
+    test("tan 90", "", -9);
     test("tan 10", "0.176326981", 0);    
     test("tan²10", "0.031091204", 0);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]