gcalctool r2072 - in branches/gnome-2-22: . gcalctool



Author: rancell
Date: Sat Apr 12 08:51:23 2008
New Revision: 2072
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2072&view=rev

Log:
Fix a number of thousands separator issues (Bug #527669).


Modified:
   branches/gnome-2-22/ChangeLog
   branches/gnome-2-22/gcalctool/calctool.c
   branches/gnome-2-22/gcalctool/calctool.h
   branches/gnome-2-22/gcalctool/display.c
   branches/gnome-2-22/gcalctool/display.h
   branches/gnome-2-22/gcalctool/get.c
   branches/gnome-2-22/gcalctool/get.h
   branches/gnome-2-22/gcalctool/gtk.c
   branches/gnome-2-22/gcalctool/ui.h

Modified: branches/gnome-2-22/gcalctool/calctool.c
==============================================================================
--- branches/gnome-2-22/gcalctool/calctool.c	(original)
+++ branches/gnome-2-22/gcalctool/calctool.c	Sat Apr 12 08:51:23 2008
@@ -750,7 +750,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: branches/gnome-2-22/gcalctool/calctool.h
==============================================================================
--- branches/gnome-2-22/gcalctool/calctool.h	(original)
+++ branches/gnome-2-22/gcalctool/calctool.h	Sat Apr 12 08:51:23 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: branches/gnome-2-22/gcalctool/display.c
==============================================================================
--- branches/gnome-2-22/gcalctool/display.c	(original)
+++ branches/gnome-2-22/gcalctool/display.c	Sat Apr 12 08:51:23 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);
 }
 
 
@@ -588,7 +596,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: branches/gnome-2-22/gcalctool/display.h
==============================================================================
--- branches/gnome-2-22/gcalctool/display.h	(original)
+++ branches/gnome-2-22/gcalctool/display.h	Sat Apr 12 08:51:23 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: branches/gnome-2-22/gcalctool/get.c
==============================================================================
--- branches/gnome-2-22/gcalctool/get.c	(original)
+++ branches/gnome-2-22/gcalctool/get.c	Sat Apr 12 08:51:23 2008
@@ -225,6 +225,13 @@
 }
 
 
+int
+get_tsep_count()
+{
+    return 3;
+}
+
+
 void
 read_resources()    /* Read all possible resources from the database. */
 {

Modified: branches/gnome-2-22/gcalctool/get.h
==============================================================================
--- branches/gnome-2-22/gcalctool/get.h	(original)
+++ branches/gnome-2-22/gcalctool/get.h	Sat Apr 12 08:51:23 2008
@@ -58,5 +58,6 @@
 
 const char *get_radix();
 const char *get_tsep();
+int get_tsep_count();
 
 #endif /* GET_H */

Modified: branches/gnome-2-22/gcalctool/gtk.c
==============================================================================
--- branches/gnome-2-22/gcalctool/gtk.c	(original)
+++ branches/gnome-2-22/gcalctool/gtk.c	Sat Apr 12 08:51:23 2008
@@ -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();
 
@@ -970,7 +970,7 @@
         str = " ";
     } else {
         if (v->noparens == 0) {
-            localize_number(localized, str, MAX_LOCALIZED);
+            localize_expression(localized, str, MAX_LOCALIZED);
             str = localized;
         }
     }
@@ -2466,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);
 }
 
 
@@ -2894,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);
     

Modified: branches/gnome-2-22/gcalctool/ui.h
==============================================================================
--- branches/gnome-2-22/gcalctool/ui.h	(original)
+++ branches/gnome-2-22/gcalctool/ui.h	Sat Apr 12 08:51:23 2008
@@ -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);
 



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