[gcalctool] Remove basevals global array



commit 83efbdf6d0244dd94d6586b012fed88a5dc9da5d
Author: Robert Ancell <robert ancell gmail com>
Date:   Thu Aug 6 09:29:49 2009 +0100

    Remove basevals global array

 data/gcalctool.schemas.in |   10 ++----
 src/calctool.c            |   12 +++----
 src/calctool.h            |    6 +---
 src/display.c             |   37 ++++++++++++++--------
 src/functions.c           |    6 ++--
 src/get.c                 |    1 -
 src/get.h                 |    1 -
 src/gtk.c                 |   74 +++++++++++++++++++++++++++++---------------
 src/mp-equation.c         |    2 +-
 src/ui.h                  |    4 +-
 src/unittest.c            |   14 ++++----
 11 files changed, 95 insertions(+), 72 deletions(-)
---
diff --git a/data/gcalctool.schemas.in b/data/gcalctool.schemas.in
index 106805f..9cdb120 100644
--- a/data/gcalctool.schemas.in
+++ b/data/gcalctool.schemas.in
@@ -32,16 +32,12 @@ To change the gconftool target use the - -config-source=blah option to gconftool
       <applyto>/apps/gcalctool/base</applyto>
       <key>/schemas/apps/gcalctool/base</key>
       <owner>gcalctool</owner>
-      <type>string</type>
-      <default>DEC</default>
+      <type>int</type>
+      <default>10</default>
       <locale name="C">
         <short>Numeric Base</short>
         <long>
-          The initial numeric base. Valid values are 
-          "BIN" (binary),
-          "OCT" (octal),
-          "DEC" (decimal) and
-          "HEX" (hexadecimal)
+          The numeric base for input and display.
         </long>
       </locale>
     </schema>
diff --git a/src/calctool.c b/src/calctool.c
index 948c5c9..dcdfe86 100644
--- a/src/calctool.c
+++ b/src/calctool.c
@@ -39,8 +39,6 @@
 
 time_t time();
 
-int basevals[4] = { 2, 8, 10, 16 };
-
 /* Calctool variables and options. */
 static CalculatorVariables calc_state;
 CalculatorVariables *v;
@@ -84,7 +82,7 @@ solve(const char *equation)
     MPNumber result;
     char result_str[MAXLINE];
     
-    v->base = DEC;
+    v->base = 10;
     v->ttype = MP_DEGREES;
     v->wordlen = 32;
     v->accuracy = 9;
@@ -95,7 +93,7 @@ solve(const char *equation)
         exit(1);
     }
     else {
-        mp_cast_to_string(&result, basevals[v->base], 9, 1, result_str, MAXLINE);
+        mp_cast_to_string(&result, v->base, 9, 1, result_str, MAXLINE);
         printf("%s\n", result_str);
         exit(0);
     }
@@ -223,10 +221,10 @@ init_state(void)
        v->accuracy = DEFAULT_ACCURACY;
     }
 
-    if (get_enumerated_resource(R_BASE, Rbstr, &i))
-       v->base = (BaseType) i;
+    if (get_int_resource(R_BASE, &i))
+       v->base = i;
     else
-       v->base = DEC;
+       v->base = 10;
 
     if (get_enumerated_resource(R_TRIG, Rtstr, &i))
        v->ttype = (MPAngleUnit) i;
diff --git a/src/calctool.h b/src/calctool.h
index eb32fe0..b1e8ea6 100644
--- a/src/calctool.h
+++ b/src/calctool.h
@@ -32,9 +32,6 @@
 #define SNPRINTF     (void) snprintf
 #define STRNCPY      (void) strncpy
 
-/* Base definitions. */
-typedef enum { BIN, OCT, DEC, HEX, MAXBASES } BaseType;
-
 #define MAX_DIGITS     200         /* Maximum displayable number of digits. */
 #define MAX_LOCALIZED  (MAX_DIGITS * (1 + MB_LEN_MAX) + MB_LEN_MAX)
 
@@ -71,7 +68,7 @@ typedef struct {
     const char *tsep;         /* Locale specific thousands separator. */
     int tsep_count;           /* Number of digits between separator. */
 
-    BaseType base;            /* Numeric base (BIN, OCT, DEC or HEX). */
+    int base;                 /* Numeric base e.g. 2, 8, 10, 16 */
     MPAngleUnit ttype;        /* Angle unit type */
     int wordlen;              /* Length of word for bitwise operations */
     int accuracy;             /* Number of digits precision. */
@@ -82,7 +79,6 @@ typedef struct {
 } CalculatorVariables;
 
 extern CalculatorVariables *v; /* Calctool variables and options. */
-extern int basevals[];         /* Supported arithmetic bases. */
 
 void doerr(char *);
 
diff --git a/src/display.c b/src/display.c
index fbad570..1f82085 100644
--- a/src/display.c
+++ b/src/display.c
@@ -129,7 +129,7 @@ localize_expression(char *dest, const char *src, int dest_length, int *cursor)
             g_string_append_unichar(output, g_utf8_get_char(c));
             
             /* Insert separator after nth digit */
-            if (v->display.show_tsep && v->base == DEC &&
+            if (v->display.show_tsep && v->base == 10 &&
                 !after_radix && digit_count > 1 && digit_count % v->tsep_count == 1) {
                 g_string_append(output, v->tsep);
                 if (new_cursor > read_cursor) {
@@ -710,7 +710,7 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
     }
     mp_set_from_mp(&MPval, &MPmant);
 
-    mp_set_from_integer(basevals[base], &MP1base);
+    mp_set_from_integer(base, &MP1base);
     mp_xpowy_integer(&MP1base, 3, &MP3base);
     mp_xpowy_integer(&MP1base, 10, &MP10base);
 
@@ -742,7 +742,7 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
         }
     }
  
-    mp_cast_to_string(&MPmant, basevals[base], v->accuracy, !v->display.show_zeroes, fixed, MAX_DIGITS);
+    mp_cast_to_string(&MPmant, base, v->accuracy, !v->display.show_zeroes, fixed, MAX_DIGITS);
     len = strlen(fixed);
     for (i = 0; i < len; i++) {
         *optr++ = fixed[i];
@@ -784,14 +784,7 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *M
 void
 display_make_number(GCDisplay *display, char *target, int target_len, const MPNumber *MPnumber, int base, int ignoreError)
 {
-    static double max_fix[MAXBASES] = {
-       1.298074214e+33,    /* Binary. */
-       2.037035976e+90,    /* Octal. */
-       1.000000000e+100,   /* Decimal */
-       2.582249878e+120    /* Hexadecimal. */
-    };
-
-    double val;
+    double val, max_fix;
     
     /*  NOTE: display_make_number can currently set v->error when converting to a double.
      *        This is to provide the same look&feel as V3 even though gcalctool
@@ -808,12 +801,30 @@ display_make_number(GCDisplay *display, char *target, int target_len, const MPNu
         target[0] = '\0';
         return;
     }
+
+    switch (base)
+    {
+    case 2:
+        max_fix = 1.298074214e+33;
+        break;
+    case 8:
+        max_fix = 2.037035976e+90;
+        break;
+    case 10:
+        max_fix = 1.000000000e+100;
+        break;
+    default:
+    case 16:
+        max_fix = 2.582249878e+120;
+        break;
+    }
+    
     // FIXME: Do this based on the number of digits, not actual values
     if ((display->format == ENG) ||
         (display->format == SCI) ||
-        (display->format == FIX && val != 0.0 && (val > max_fix[base]))) {
+        (display->format == FIX && val != 0.0 && (val > max_fix))) {
         make_eng_sci(display, target, target_len, MPnumber, base);
     } else {
-        mp_cast_to_string(MPnumber, basevals[base], v->accuracy, !v->display.show_zeroes, target, target_len);
+        mp_cast_to_string(MPnumber, base, v->accuracy, !v->display.show_zeroes, target, target_len);
     }
 }
diff --git a/src/functions.c b/src/functions.c
index c1e7457..b9189d4 100644
--- a/src/functions.c
+++ b/src/functions.c
@@ -284,7 +284,7 @@ do_shift(int count)
 
 /* Change the current base setting. */
 static void
-do_base(BaseType b)
+do_base(int b)
 {
     int ret;
     MPNumber MP;
@@ -302,8 +302,8 @@ do_base(BaseType b)
         }
     }
     v->base = b;
-    set_enumerated_resource(R_BASE, Rbstr, (int) v->base);
-    display_set_base(&v->display, basevals[v->base]);
+    set_int_resource(R_BASE, v->base);
+    display_set_base(&v->display, v->base);
     ui_set_base(v->base);
     ui_make_registers();
 }
diff --git a/src/get.c b/src/get.c
index 5842703..fa27d8b 100644
--- a/src/get.c
+++ b/src/get.c
@@ -40,7 +40,6 @@
 
 /* Various string values read/written as X resources. */
 
-const char *Rbstr[] = { "BIN", "OCT", "DEC", "HEX", NULL };
 const char *Rtstr[] = { "DEG", "GRAD", "RAD", NULL };
 
 static GConfClient *client = NULL;
diff --git a/src/get.h b/src/get.h
index 807ae24..34dd65f 100644
--- a/src/get.h
+++ b/src/get.h
@@ -36,7 +36,6 @@
 #define R_XPOS     "xposition"
 #define R_YPOS     "yposition"
 
-extern const char *Rbstr[];
 extern const char *Rtstr[];
 
 void resources_init();
diff --git a/src/gtk.c b/src/gtk.c
index b01bb3b..610c494 100644
--- a/src/gtk.c
+++ b/src/gtk.c
@@ -521,7 +521,10 @@ typedef struct {
     GtkWidget *gradian_radio;          /* Gradian radio button. */
 
     /* Programming mode widgets */
-    GtkWidget *base_radios[MAXBASES];
+    GtkWidget *binary_radio;
+    GtkWidget *octal_radio;
+    GtkWidget *decimal_radio;
+    GtkWidget *hexadecimal_radio;
     GtkWidget *word_length_radios[3];             /* Wordlength radio buttons. */
 
     GdkAtom clipboard_atom;
@@ -874,7 +877,7 @@ ui_set_trigonometric_mode(MPAngleUnit units)
 
 
 void
-ui_set_numeric_mode(BaseType mode)
+ui_set_numeric_mode(DisplayFormat mode)
 {
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(X.display_mode_radios[mode]), 1);
 }
@@ -1021,7 +1024,7 @@ ui_set_mode(ModeType mode)
 
         // FIXME: These should affect display but not the actual UI settings
         if (mode != PROGRAMMING)
-            ui_set_base(DEC);
+            ui_set_base(10);
         if (mode != SCIENTIFIC) {
             ui_set_numeric_mode(FIX);
             do_button(FN_SET_ACCURACY, DEFAULT_ACCURACY);
@@ -1263,15 +1266,33 @@ ui_beep()
 
 
 void
-ui_set_base(BaseType base)
+ui_set_base(int base)
 {
-    int i, baseval = basevals[(int) base];
+    GtkWidget *widget;
+    int i;
     
     v->base = base;
 
     for (i = 0; i < 16; i++)
-        gtk_widget_set_sensitive(X.digit_buttons[i], i < baseval);
-    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(X.base_radios[base]), 1);
+        gtk_widget_set_sensitive(X.digit_buttons[i], i < base);
+    switch (base)
+    {
+    case 2:
+        widget = X.binary_radio;
+        break;
+    case 8:
+        widget = X.octal_radio;
+        break;
+    case 10:
+        widget = X.decimal_radio;
+        break;
+    case 16:
+        widget = X.hexadecimal_radio;
+        break;
+    default:
+        return;
+    }
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
 }
 
 
@@ -1492,9 +1513,9 @@ G_MODULE_EXPORT
 void
 base_cb(GtkWidget *widget)
 {
-    BaseType base;
+    int base;
 
-    base = (BaseType) g_object_get_data(G_OBJECT(widget), "base_mode");
+    base = (int) g_object_get_data(G_OBJECT(widget), "base_mode");
     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
         do_button(FN_SET_BASE, base);
 }
@@ -1583,7 +1604,8 @@ exchange_menu_cb(GtkMenuItem *menu)
 
 G_MODULE_EXPORT
 void
-finc_activate_cb(GtkWidget *widget) {
+finc_activate_cb(GtkWidget *widget)
+{
     gint dialog, field;
 
     dialog = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "finc_dialog"));
@@ -1673,7 +1695,7 @@ update_constants_menu(void)
     int i;
 
     for (i = 0; i < MAX_CONSTANTS; i++) {
-        display_make_number(&v->display, value, MAXLINE, constant_get_value(i), DEC, TRUE);
+        display_make_number(&v->display, value, MAXLINE, constant_get_value(i), 10, TRUE);
         SNPRINTF(mline, MAXLINE, 
                  "<span weight=\"bold\">%s_%1d:</span> %s [%s]", _("C"), i, 
                  value, 
@@ -1798,7 +1820,7 @@ create_constants_model()
     for (i = 0; i < MAX_CONSTANTS; i++) {
         gtk_list_store_append(model, &iter);
         
-        display_make_number(&v->display, constant, MAXLINE, constant_get_value(i), DEC, TRUE);
+        display_make_number(&v->display, constant, MAXLINE, constant_get_value(i), 10, TRUE);
         gtk_list_store_set(model, &iter,
                            COLUMN_NUMBER, i,
                            COLUMN_EDITABLE, TRUE,
@@ -1850,7 +1872,7 @@ ui_make_registers()            /* Calculate memory register frame values. */
         gtk_entry_set_text(GTK_ENTRY(X.register_entries[n]), mval);
 
         SNPRINTF(key, MAXLINE, "register%d", n);
-        display_make_number(&v->display, value, MAXLINE, &temp, DEC, TRUE);
+        display_make_number(&v->display, value, MAXLINE, &temp, 10, TRUE);
         set_resource(key, value);
     }
 }
@@ -2533,10 +2555,10 @@ create_main_window()
     X.degree_radio     = GET_WIDGET("degrees_radio");
     X.gradian_radio    = GET_WIDGET("gradians_radio");
     X.radian_radio     = GET_WIDGET("radians_radio");
-    X.base_radios[0]   = GET_WIDGET("binary_radio");
-    X.base_radios[1]   = GET_WIDGET("octal_radio");
-    X.base_radios[2]   = GET_WIDGET("decimal_radio");
-    X.base_radios[3]   = GET_WIDGET("hexadecimal_radio");
+    X.binary_radio     = GET_WIDGET("binary_radio");
+    X.octal_radio      = GET_WIDGET("octal_radio");
+    X.decimal_radio    = GET_WIDGET("decimal_radio");
+    X.hexadecimal_radio      = GET_WIDGET("hexadecimal_radio");
     X.display_mode_radios[0] = GET_WIDGET("engineering_radio");
     X.display_mode_radios[1] = GET_WIDGET("fixed_point_radio");
     X.display_mode_radios[2] = GET_WIDGET("scientific_radio");
@@ -2693,14 +2715,16 @@ create_main_window()
     gtk_widget_realize(X.main_window);
     set_win_position();
 
-    g_object_set_data(G_OBJECT(X.radian_radio), "trig_mode", GINT_TO_POINTER(MP_RADIANS));
-    g_object_set_data(G_OBJECT(X.degree_radio), "trig_mode", GINT_TO_POINTER(MP_DEGREES));
-    g_object_set_data(G_OBJECT(X.gradian_radio), "trig_mode", GINT_TO_POINTER(MP_GRADIANS));
-    for (i = 0; i < 4; i++)
-        g_object_set_data(G_OBJECT(X.base_radios[i]), "base_mode", GINT_TO_POINTER(i));
-    for (i = 0; i < 3; i++)
-        g_object_set_data(G_OBJECT(X.display_mode_radios[i]), "numeric_mode", GINT_TO_POINTER(i));
-    
+    g_object_set_data(G_OBJECT(X.radian_radio),          "trig_mode", GINT_TO_POINTER(MP_RADIANS));
+    g_object_set_data(G_OBJECT(X.degree_radio),          "trig_mode", GINT_TO_POINTER(MP_DEGREES));
+    g_object_set_data(G_OBJECT(X.gradian_radio),         "trig_mode", GINT_TO_POINTER(MP_GRADIANS));
+    g_object_set_data(G_OBJECT(X.binary_radio),          "base_mode", GINT_TO_POINTER(2));
+    g_object_set_data(G_OBJECT(X.octal_radio),           "base_mode", GINT_TO_POINTER(8));
+    g_object_set_data(G_OBJECT(X.decimal_radio),         "base_mode", GINT_TO_POINTER(10));
+    g_object_set_data(G_OBJECT(X.hexadecimal_radio),     "base_mode", GINT_TO_POINTER(16));
+    g_object_set_data(G_OBJECT(X.display_mode_radios[0]), "numeric_mode", GINT_TO_POINTER(ENG));
+    g_object_set_data(G_OBJECT(X.display_mode_radios[1]), "numeric_mode", GINT_TO_POINTER(FIX));
+    g_object_set_data(G_OBJECT(X.display_mode_radios[2]), "numeric_mode", GINT_TO_POINTER(SCI));
     g_object_set_data(G_OBJECT(X.word_length_radios[0]), "wordlen_mode", GINT_TO_POINTER(64));
     g_object_set_data(G_OBJECT(X.word_length_radios[1]), "wordlen_mode", GINT_TO_POINTER(32));
     g_object_set_data(G_OBJECT(X.word_length_radios[2]), "wordlen_mode", GINT_TO_POINTER(16));
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 1806941..72b275f 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -168,7 +168,7 @@ mp_equation_parse(const char *expression, MPNumber *result)
         return(-EINVAL);
 
     memset(&state, 0, sizeof(MPEquationParserState));
-    state.base = basevals[v->base];
+    state.base = v->base;
     state.wordlen = v->wordlen;
     state.angle_units = v->ttype;
     state.get_variable = get_variable;
diff --git a/src/ui.h b/src/ui.h
index ce22e75..d087f98 100644
--- a/src/ui.h
+++ b/src/ui.h
@@ -48,9 +48,9 @@ gchar *ui_get_display(void);
 void ui_set_registers_visible(gboolean);
 void ui_set_accuracy(int);
 void ui_set_mode(ModeType);
-void ui_set_base(BaseType);
+void ui_set_base(int);
 void ui_set_trigonometric_mode(MPAngleUnit unit);
-void ui_set_numeric_mode(BaseType);
+void ui_set_numeric_mode(DisplayFormat);
 void ui_set_show_thousands_separator(gboolean);
 void ui_set_show_trailing_zeroes(gboolean);
 void ui_set_wordlen(int);
diff --git a/src/unittest.c b/src/unittest.c
index 79ad2f0..9716ec5 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -73,7 +73,7 @@ test(char *expression, char *expected, int expected_error)
     error = mp_equation_parse(expression, &result);
 
     if(error == 0) {
-        mp_cast_to_string(&result, basevals[v->base], 9, 1, result_str, MAXLINE);
+        mp_cast_to_string(&result, v->base, 9, 1, result_str, MAXLINE);
         if(expected_error != 0)
             fail("'%s' -> %s, expected error %d", expression, result_str, expected_error);
         else if(strcmp(result_str, expected) != 0)
@@ -97,13 +97,13 @@ test_parser()
     v->wordlen = 32;
     v->accuracy = 9;
 
-    v->base = BIN;
+    v->base = 2;
     test("0", "0", 0);
     test("1", "1", 0);
     test("10", "10", 0);
     test("210", "", -1);
 
-    v->base = OCT;
+    v->base = 8;
     test("0", "0", 0);
     test("1", "1", 0);
     test("2", "2", 0);
@@ -115,7 +115,7 @@ test_parser()
     test("76543210", "76543210", 0);
     test("876543210", "", -1);
     
-    v->base = DEC;
+    v->base = 10;
     test("0", "0", 0);
     test("1", "1", 0);
     test("2", "2", 0);
@@ -129,7 +129,7 @@ test_parser()
     test("9876543210", "9876543210", 0);
     test("A9876543210", "", -7);
 
-    v->base = HEX;
+    v->base = 16;
     test("0", "0", 0);
     test("1", "1", 0);
     test("2", "2", 0);
@@ -149,7 +149,7 @@ test_parser()
     test("FEDBCA9876543210", "FEDBCA9876543210", 0);
     test("GFEDBCA9876543210", "", -7);
 
-    v->base = DEC;
+    v->base = 10;
     test("+1", "1", 0);
     test("â??1", "â??1", 0);
     test("+ 1", "1", 0); // FIXME: Should this be allowed?
@@ -405,7 +405,7 @@ test_parser()
     test("3 or 5", "7", 0);
     test("3 xor 5", "6", 0);
 
-    v->base = HEX;
+    v->base = 16;
     test("ones 1", "FFFFFFFE", 0);
     test("ones 7FFFFFFF", "80000000", 0);
     test("twos 1", "FFFFFFFF", 0);



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