gcalctool r2075 - in trunk: . gcalctool



Author: rancell
Date: Sun Apr 13 09:32:39 2008
New Revision: 2075
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2075&view=rev

Log:
Correctly populate the constant and function menus (Bug #527545)
Fix compiler warnings (Bug #527318)
Fix a number of thousands separator issues (Bug #527669).
Remove shadowed variables (Bug #526976)
			    


Modified:
   trunk/ChangeLog
   trunk/gcalctool/calctool.c
   trunk/gcalctool/calctool.h
   trunk/gcalctool/display.c
   trunk/gcalctool/display.h
   trunk/gcalctool/functions.c
   trunk/gcalctool/functions.h
   trunk/gcalctool/get.c
   trunk/gcalctool/get.h
   trunk/gcalctool/gtk.c
   trunk/gcalctool/ui.h
   trunk/gcalctool/unittest.c

Modified: trunk/gcalctool/calctool.c
==============================================================================
--- trunk/gcalctool/calctool.c	(original)
+++ trunk/gcalctool/calctool.c	Sun Apr 13 09:32:39 2008
@@ -755,7 +755,8 @@
     resources_init();          /* Initialise configuration */
 
     v->radix = get_radix();    /* Locale specific radix string. */
-    v->tsep  = get_tsep();     /* Locale specific thousands seperator. */
+    v->tsep  = get_tsep();     /* Locale specific thousands separator. */
+    v->tsep_count = get_tsep_count();
 
     init_text();               /* Setup text strings depending upon language. */
     read_resources();          /* Read resources from merged database. */

Modified: trunk/gcalctool/calctool.h
==============================================================================
--- trunk/gcalctool/calctool.h	(original)
+++ trunk/gcalctool/calctool.h	Sun Apr 13 09:32:39 2008
@@ -210,7 +210,8 @@
     const char *radix;                 /* Locale specific radix string. */
     char *shelf;                       /* PUT selection shelf contents. */
     char snum[MAX_LOCALIZED];          /* Scratchpad for scientific numbers. */
-    const char *tsep;                  /* Locale specific thousands seperator. */
+    const char *tsep;                  /* Locale specific thousands separator. */
+    int tsep_count;                    /* Number of digits between separator. */
 
     char fun_names[MAX_FUNCTIONS][MAXLINE];  /* Function names from .gcalctoolcf. */
     char fun_vals[MAX_FUNCTIONS][MAXLINE];   /* Function defs from .gcalctoolcf. */
@@ -245,7 +246,7 @@
     int old_cal_value; /* Previous calculation operator. */
     int pointed;       /* Whether a decimal point has been given. */
     int show_paren;    /* Set if we wish to show DISPLAYITEM during parens. */
-    int show_tsep;     /* Set if the thousands seperator should be shown. */
+    int show_tsep;     /* Set if the thousands separator should be shown. */
     int show_zeroes;   /* Set if trailing zeroes should be shown. */
     int toclear;       /* Indicates if display should be cleared. */
 };

Modified: trunk/gcalctool/display.c
==============================================================================
--- trunk/gcalctool/display.c	(original)
+++ trunk/gcalctool/display.c	Sun Apr 13 09:32:39 2008
@@ -46,60 +46,68 @@
  */
 
 void
-localize_number(char *dest, const char *src, int dest_length)
+localize_expression(char *dest, const char *src, int dest_length)
 {
-    char tnum[MAX_LOCALIZED], *dstp;
+    GString *output = g_string_sized_new(dest_length);
+    const char *c, *d;
+    int digit_count = -1;
+    gboolean after_radix = FALSE;
 
-    if (!v->error && v->show_tsep && v->base == DEC) {
-        const char *radixp, *srcp;
-        int n, i;
-        size_t tsep_len;
-
-        /* Process the fractional part (if any). */
-        srcp = src + strlen(src) - 1;
-        dstp = tnum;
-        if ((radixp = strchr(src, '.')) != NULL) {
-            while (srcp != radixp) {
-                *dstp++ = *srcp--;
+    /* Only modify if valid */
+    if (v->error || v->base != DEC) {
+        STRNCPY(dest, src, dest_length - 1);
+        return;
+    }
+    
+    /* Remove separators if not supported */
+    if(!v->show_tsep) {
+        for (c = src; *c; c++) {
+            if (strncmp(c, v->tsep, strlen(v->radix)) == 0) {
+                c += strlen(v->radix) - 1;
             }
-            *dstp++ = *srcp--;
-        }
-
-        /* Process the integer part, add in thousand separators. */
-        tsep_len = strlen(v->tsep);
-        n = 0;
-        while (srcp >= src) {
-            *dstp++ = *srcp--;
-            n++;
-            if (n == 3 && srcp >= src && *srcp != '-') {
-                for (i = tsep_len - 1; i >= 0; i--) {
-                    *dstp++ = v->tsep[i];
-                }
-                n = 0;
+            else {
+                g_string_append_c(output, *c);
             }
         }
-        *dstp++ = '\0';
-
-        /* Move from scratch pad to fnum, reversing the character order. */
-        srcp = tnum + strlen(tnum) - 1;
-        dstp = dest;
-        while (srcp >= tnum) {
-            *dstp++ = *srcp--;
-        }
-        *dstp++ = '\0';
-    } else {
-        STRNCPY(dest, src, dest_length - 1);
+        STRNCPY(dest, output->str, dest_length - 1);
+        return;
     }
-    dstp = strchr(dest, '.');
-    if (dstp != NULL) {
-        size_t radix_len;
-
-        radix_len = strlen(v->radix);
-        if (radix_len != 1) {
-            memmove(dstp + radix_len, dstp + 1, strlen (dstp + 1) + 1);
+    
+    /* Scan expression looking for numbers and inserting separators */
+    for (c = src; *c; c++) {
+        /* Insert separators between digits */
+        if (*c >= '0' && *c <= '9') {
+            /* Read ahead to find the number of digits */
+            if (digit_count < 0) {
+                digit_count = 1;
+                for (d = c + 1; *d >= '0' && *d <= '9'; d++)
+                    digit_count++;
+            }
+            
+            g_string_append_c(output, *c);
+            
+            /* Insert separator after nth digit */
+            if (!after_radix && digit_count > 1 && digit_count % v->tsep_count == 1) {
+                g_string_append(output, v->tsep);
+            }
+            digit_count--;
+        }
+        /* Ignore digits after the radix */
+        else if (strncmp(c, v->radix, strlen(v->radix)) == 0) {
+            digit_count = -1;
+            after_radix = TRUE;
+            c += strlen(v->radix) - 1;
+            g_string_append(output, v->radix);
+        }
+        /* Reset when encountering other characters (e.g. '+') */
+        else {
+            digit_count = -1;
+            after_radix = FALSE;
+            g_string_append_c(output, *c);
         }
-        MEMCPY(dstp, v->radix, radix_len);
     }
+    
+    STRNCPY(dest, output->str, dest_length - 1);
 }
 
 
@@ -577,7 +585,7 @@
             }
             ans = make_number(e->ans, v->base, TRUE);
 
-            localize_number(localized, ans, MAX_LOCALIZED);
+            localize_expression(localized, ans, MAX_LOCALIZED);
             t = str_replace(str, "Ans", localized);
             free(str);
             str = t;

Modified: trunk/gcalctool/display.h
==============================================================================
--- trunk/gcalctool/display.h	(original)
+++ trunk/gcalctool/display.h	Sun Apr 13 09:32:39 2008
@@ -25,7 +25,7 @@
 #include "calctool.h"
 
 void initialise();
-void localize_number(char *, const char *, int);
+void localize_expression(char *, const char *, int);
 char *make_fixed(int *, char *, int, int, int);
 char *make_number(int *, int, int);
 void clear_display(int);

Modified: trunk/gcalctool/functions.c
==============================================================================
--- trunk/gcalctool/functions.c	(original)
+++ trunk/gcalctool/functions.c	Sun Apr 13 09:32:39 2008
@@ -205,7 +205,7 @@
 }
 
 static int
-is_undo_step()
+is_undo_step(void)
 {
     return(v->h.current != v->h.begin);
 }
@@ -237,7 +237,7 @@
 
 
 void
-do_business()     /* Perform special business mode calculations. */
+do_business(void)     /* Perform special business mode calculations. */
 {
     if (v->current == KEY_FINC_CTRM) {
         calc_ctrm(v->MPdisp_val);
@@ -282,7 +282,7 @@
 
 
 void
-exp_clear()
+exp_clear(void)
 {
     exp_replace("");
 }
@@ -396,7 +396,7 @@
 
 
 static void
-exp_negate()
+exp_negate(void)
 {
     struct exprm_state *e = get_state();
 
@@ -414,7 +414,7 @@
 
 
 static void
-exp_inv()
+exp_inv(void)
 {
     struct exprm_state *e = get_state();
 
@@ -632,7 +632,7 @@
 
 
 void
-do_calc()      /* Perform arithmetic calculation and display result. */
+do_calc(void)      /* Perform arithmetic calculation and display result. */
 {
     double dval, dres;
     int MP1[MP_SIZE], MP2[MP_SIZE];
@@ -872,7 +872,7 @@
 
 /* Clear the calculator display and re-initialise. */
 void
-do_clear()
+do_clear(void)
 {
     clear_display(TRUE);
     if (v->error) {
@@ -884,7 +884,7 @@
 
 /* Clear the calculator display. */
 void
-do_clear_entry()
+do_clear_entry(void)
 {
     clear_display(FALSE);
 }
@@ -949,7 +949,7 @@
 
 /* Remove the last numeric character typed. */
 void
-do_backspace()
+do_backspace(void)
 {
     size_t len;
 
@@ -984,7 +984,7 @@
 
 
 void
-do_delete()
+do_delete(void)
 {
     /* Not required in ltr mode */
 }
@@ -1033,7 +1033,7 @@
 
 /* Get exponential number. */
 void
-do_expno()
+do_expno(void)
 {
     v->pointed = (strchr(v->display, '.') != NULL);
     if (!v->new_input) {
@@ -1218,7 +1218,7 @@
 
 
 void
-do_immed()
+do_immed(void)
 {
     do_immedfunc(v->MPdisp_val, v->MPdisp_val);
     show_display(v->MPdisp_val);
@@ -1226,7 +1226,7 @@
 
 
 void
-do_number()
+do_number(void)
 {
     int offset;
 
@@ -1283,7 +1283,7 @@
 
 
 void
-do_paren()
+do_paren(void)
 {
     ui_set_statusbar("", "");
 
@@ -1394,7 +1394,7 @@
 
 
 void
-syntaxdep_show_display()
+syntaxdep_show_display(void)
 {
     switch (v->syntax) {
         case NPA:
@@ -1412,7 +1412,7 @@
 
 
 void
-do_point()                   /* Handle numeric point. */
+do_point(void)                   /* Handle numeric point. */
 {
     if (!v->pointed) {
         if (v->toclear) {
@@ -1453,7 +1453,7 @@
 
 
 void
-do_portion()
+do_portion(void)
 {
     do_portionfunc(v->MPdisp_val);
     show_display(v->MPdisp_val);

Modified: trunk/gcalctool/functions.h
==============================================================================
--- trunk/gcalctool/functions.h	(original)
+++ trunk/gcalctool/functions.h	Sun Apr 13 09:32:39 2008
@@ -26,14 +26,14 @@
 
 void show_error(char *);
 char *str_replace(char *, char *, char *);
-void syntaxdep_show_display();
+void syntaxdep_show_display(void);
 char *gc_strdup(char *str);
 int usable_num(int MPnum[MP_SIZE]);
 
 void make_exp(char *number, int t[MP_SIZE]);
 int exp_insert(char *text, int);
 void exp_replace(char *text);
-void exp_clear();
+void exp_clear(void);
 
 struct exprm_state *get_state(void);
 
@@ -42,42 +42,41 @@
 void clear_undo_history(void);
 
 void do_base(enum base_type);
-void do_business();
-void do_calc();
-void do_lr_calc();
+void do_business(void);
+void do_calc(void);
 void do_expression(int function, int arg, int cursor);
-void do_clear();
-void do_clear_entry();
-void do_backspace();
-void do_delete();
+void do_clear(void);
+void do_clear_entry(void);
+void do_backspace(void);
+void do_delete(void);
 void do_numtype(enum num_type);
-void do_expno();
-void do_immed();
-void do_number();
-void do_paren();
+void do_expno(void);
+void do_immed(void);
+void do_number(void);
+void do_paren(void);
 void do_shift(int);
-void do_sto();
-void do_rcl();
-void do_exchange();
-void do_accuracy();
-void do_constant();
-void do_function();
-void do_point();
-void do_portion();
-void do_sin();
-void do_sinh();
-void do_asin();
-void do_asinh();
-void do_cos(); 
-void do_cosh();
-void do_acos();
-void do_acosh();
-void do_tan();
-void do_tanh();
-void do_atan();
-void do_atanh();
+void do_sto(int);
+void do_rcl(int);
+void do_exchange(int);
+void do_accuracy(int);
+void do_constant(int);
+void do_function(int);
+void do_point(void);
+void do_portion(void);
+void do_sin(void);
+void do_sinh(void);
+void do_asin(void);
+void do_asinh(void);
+void do_cos(void); 
+void do_cosh(void);
+void do_acos(void);
+void do_acosh(void);
+void do_tan(void);
+void do_tanh(void);
+void do_atan(void);
+void do_atanh(void);
 void do_trigtype(enum trig_type);
-void do_percent();
+void do_percent(void);
 void do_factorial(int *, int *);
 int do_rcl_reg(int reg, int value[MP_SIZE]);
 int do_sto_reg(int reg, int value[MP_SIZE]);

Modified: trunk/gcalctool/get.c
==============================================================================
--- trunk/gcalctool/get.c	(original)
+++ trunk/gcalctool/get.c	Sun Apr 13 09:32:39 2008
@@ -225,6 +225,13 @@
 }
 
 
+int
+get_tsep_count()
+{
+    return 3;
+}
+
+
 void
 read_resources()    /* Read all possible resources from the database. */
 {

Modified: trunk/gcalctool/get.h
==============================================================================
--- trunk/gcalctool/get.h	(original)
+++ trunk/gcalctool/get.h	Sun Apr 13 09:32:39 2008
@@ -58,5 +58,6 @@
 
 const char *get_radix();
 const char *get_tsep();
+int get_tsep_count();
 
 #endif /* GET_H */

Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c	(original)
+++ trunk/gcalctool/gtk.c	Sun Apr 13 09:32:39 2008
@@ -215,20 +215,20 @@
     { GDK_parenright, GDK_parenright, 0 }},
 
     {KEY_ADD,                "add",
-    { GDK_SHIFT_MASK, 0,        0,          0 },
-    { GDK_plus,       GDK_plus, GDK_KP_Add, 0 }},
+    { 0,        GDK_SHIFT_MASK, 0,          0 },
+    { GDK_plus, GDK_plus,       GDK_KP_Add, 0 }},
 
     {KEY_SUBTRACT,           "subtract",
-    { 0,         0,               0,      0 },
-    { GDK_minus, GDK_KP_Subtract, GDK_R4, 0 }},
+    { 0,         GDK_SHIFT_MASK, 0,               0,      0 },
+    { GDK_minus, GDK_minus,      GDK_KP_Subtract, GDK_R4, 0 }},
 
     {KEY_MULTIPLY,           "multiply",
-    { 0,            GDK_SHIFT_MASK, 0,               0,      0 },
-    { GDK_asterisk, GDK_asterisk,   GDK_KP_Multiply, GDK_R6, 0 }},
+    { 0,            GDK_SHIFT_MASK, 0,            GDK_SHIFT_MASK, 0,               0,      0 },
+    { GDK_asterisk, GDK_asterisk,   GDK_multiply, GDK_multiply,   GDK_KP_Multiply, GDK_R6, 0 }},
 
     {KEY_DIVIDE,             "divide",
-    { 0,         GDK_SHIFT_MASK, 0,             0,      GDK_SHIFT_MASK, 0 },
-    { GDK_slash, GDK_slash,      GDK_KP_Divide, GDK_R5, GDK_slash,      0 }},
+    { 0,         GDK_SHIFT_MASK, 0,            GDK_SHIFT_MASK, 0,             0,      GDK_SHIFT_MASK, 0 },
+    { GDK_slash, GDK_slash,      GDK_division, GDK_division,   GDK_KP_Divide, GDK_R5, GDK_slash,      0 }},
 
     {KEY_CHANGE_SIGN,        "change_sign_simple",
     { GDK_SHIFT_MASK, 0 },
@@ -650,7 +650,7 @@
 
 
 void
-ui_set_show_thousands_seperator(gboolean visible)
+ui_set_show_thousands_separator(gboolean visible)
 {
     GtkWidget *menu;
 
@@ -771,7 +771,7 @@
         ui_set_base(DEC);
         ui_set_numeric_mode(FIX);
         ui_set_accuracy(DEFAULT_ACCURACY);
-        ui_set_show_thousands_seperator(FALSE);
+        ui_set_show_thousands_separator(FALSE);
         ui_set_show_trailing_zeroes(FALSE);
         ui_make_registers();
 
@@ -881,7 +881,7 @@
 }
 
 static void
-set_bit_panel()
+set_bit_panel(void)
 {
     int bit_str_len, i, MP1[MP_SIZE], MP2[MP_SIZE];
     int MP[MP_SIZE];
@@ -915,34 +915,29 @@
             gtk_widget_set_sensitive(X->bit_panel, FALSE);
             break;
 
-          case EXPRS: 
-              {
-                  char *bit_str, label[3], tmp[MAXLINE];
-                  int ret = usable_num(MP);
-                  if (ret || !is_integer(MP)) {
-                      gtk_widget_set_sensitive(X->bit_panel, FALSE);
-                      return;
-                  }
-                  bit_str = make_fixed(MP, tmp, BIN, MAXLINE, FALSE);
-                  bit_str_len = strlen(bit_str);
-                  if (bit_str_len <= MAXBITS) {
-                      gtk_widget_set_sensitive(X->bit_panel, TRUE);
-                      
-                      for (i = 0; i < MAXBITS; i++) {
-                          if (i < bit_str_len) {
-                              SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
-                          } else {
-                              SNPRINTF(label, MAXLINE, " 0");
-                          }
-                          gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
-                      }
-                  } else {
-                      gtk_widget_set_sensitive(X->bit_panel, FALSE);
-                  }
-                  
-              }
+        case EXPRS: 
+            if (usable_num(MP) || !is_integer(MP)) {
+                gtk_widget_set_sensitive(X->bit_panel, FALSE);
+                return;
+            }
+            bit_str = make_fixed(MP, tmp, BIN, MAXLINE, FALSE);
+            bit_str_len = strlen(bit_str);
+            if (bit_str_len <= MAXBITS) {
+                gtk_widget_set_sensitive(X->bit_panel, TRUE);
+                
+                for (i = 0; i < MAXBITS; i++) {
+                    if (i < bit_str_len) {
+                        SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
+                    } else {
+                        SNPRINTF(label, MAXLINE, " 0");
+                    }
+                    gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
+                }
+            } else {
+                gtk_widget_set_sensitive(X->bit_panel, FALSE);
+            }
             break;
-        
+
         default:
             assert(FALSE);
     }
@@ -975,7 +970,7 @@
         str = " ";
     } else {
         if (v->noparens == 0) {
-            localize_number(localized, str, MAX_LOCALIZED);
+            localize_expression(localized, str, MAX_LOCALIZED);
             str = localized;
         }
     }
@@ -1600,6 +1595,8 @@
         gtk_list_store_set(model, &iter,
                            COLUMN_NUMBER, i,
                            COLUMN_EDITABLE, TRUE,
+                           COLUMN_VALUE, g_strdup(make_number(v->MPcon_vals[i], DEC, TRUE)),
+                           COLUMN_DESCRIPTION, g_strdup(v->con_names[i]),
                            -1);
     }
 
@@ -1623,6 +1620,8 @@
         gtk_list_store_set(model, &iter,
                            COLUMN_NUMBER, i,
                            COLUMN_EDITABLE, TRUE,
+                           COLUMN_VALUE, g_strdup(v->fun_vals[i]),
+                           COLUMN_DESCRIPTION, g_strdup(v->fun_names[i]),
                            -1);
     }
 
@@ -2467,7 +2466,7 @@
     gboolean visible;
     
     visible = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
-    ui_set_show_thousands_seperator(visible);
+    ui_set_show_thousands_separator(visible);
 }
 
 
@@ -2879,7 +2878,7 @@
 
 
 void
-ui_load()
+ui_load(void)
 {
     int boolval;
     char *resource, text[MAXLINE];
@@ -2895,7 +2894,7 @@
     resource = get_resource(R_BITCALC);
     show_bit = resource != NULL && strcmp(resource, Rcstr[0]) != 0;
 
-    ui_set_show_thousands_seperator(v->show_tsep);
+    ui_set_show_thousands_separator(v->show_tsep);
     ui_set_show_trailing_zeroes(v->show_zeroes);
     ui_set_show_bitcalculating(show_bit);
     
@@ -2928,7 +2927,7 @@
 }
 
 void
-ui_start()
+ui_start(void)
 {
     X->warn_change_mode = TRUE; // FIXME: Load from GConf
     

Modified: trunk/gcalctool/ui.h
==============================================================================
--- trunk/gcalctool/ui.h	(original)
+++ trunk/gcalctool/ui.h	Sun Apr 13 09:32:39 2008
@@ -24,9 +24,9 @@
 
 #include "calctool.h"
 
-void ui_init();
-void ui_load();
-void ui_start();
+void ui_init(int *argc, char ***argv);
+void ui_load(void);
+void ui_start(void);
 
 void ui_make_registers(void);
 void ui_set_undo_enabled(gboolean, gboolean);
@@ -43,7 +43,7 @@
 void ui_set_hyperbolic_state(gboolean);
 void ui_set_trigonometric_mode(enum trig_type);
 void ui_set_numeric_mode(enum base_type);
-void ui_set_show_thousands_seperator(gboolean);
+void ui_set_show_thousands_separator(gboolean);
 void ui_set_show_bitcalculating(gboolean);
 void ui_set_show_trailing_zeroes(gboolean);
 

Modified: trunk/gcalctool/unittest.c
==============================================================================
--- trunk/gcalctool/unittest.c	(original)
+++ trunk/gcalctool/unittest.c	Sun Apr 13 09:32:39 2008
@@ -85,7 +85,10 @@
     test("1!", "1", 0);
     test("5!", "120", 0);
     //FIXME: Need to update do_factorial() test("0.1!", "", 0);
-    //FIXME: Need to update do_factorial() test("-1!", "", 0);    
+    //FIXME: Need to update do_factorial() test("-1!", "", 0);
+    
+    test("-10^2", "-100", 0);
+    test("(-10)^2", "100", 0);    
 
     test("Sqrt(4)", "2", 0);
     test("Sqrt(2)", "1.4142135", 0);



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