From 1143458131cbd2e409280650cd9a470f15e5f853 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Sun, 13 Apr 2008 08:32:39 +0000 Subject: [PATCH] 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) svn path=/trunk/; revision=2075 --- ChangeLog | 9 +++++ gcalctool/calctool.c | 3 +- gcalctool/calctool.h | 5 ++- gcalctool/display.c | 102 +++++++++++++++++++++++++++----------------------- gcalctool/display.h | 2 +- gcalctool/functions.c | 34 ++++++++--------- gcalctool/functions.h | 67 ++++++++++++++++----------------- gcalctool/get.c | 7 ++++ gcalctool/get.h | 1 + gcalctool/gtk.c | 85 +++++++++++++++++++++-------------------- gcalctool/ui.h | 8 ++-- gcalctool/unittest.c | 5 ++- 12 files changed, 178 insertions(+), 150 deletions(-) diff --git a/ChangeLog b/ChangeLog index 03f4a14c..d7a8cbb7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,14 @@ gcalctool change history. ========================= +2008-04-12 Robert Ancell + + * gcalctool/gtk.c: Correctly populate the constant and function menus (Bug #527545) + * gcalctool/gtk.c: + gcalctool/functions.[ch]: + gcalctool/ui.h: Fix compiler warnings (Bug #527318) + * Fix a number of thousands separator issues (Bug #527669). + 2008-04-10 Robert Ancell * gcalctool/ce_parser.y: Change parser precedence to perform negation after @@ -15,6 +23,7 @@ gcalctool change history. po/POTFILES.skip: m4/Makefile.am: configure.in: Fix build so make distcheck works (Bug #526702) + * gcalctool/gtk.c: Remove shadowed variables (Bug #526976) 2008-03-08 Robert Ancell diff --git a/gcalctool/calctool.c b/gcalctool/calctool.c index dbf915ac..999bc3d2 100644 --- a/gcalctool/calctool.c +++ b/gcalctool/calctool.c @@ -755,7 +755,8 @@ main(int argc, char **argv) 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. */ diff --git a/gcalctool/calctool.h b/gcalctool/calctool.h index ba27d473..ffeb60e6 100644 --- a/gcalctool/calctool.h +++ b/gcalctool/calctool.h @@ -210,7 +210,8 @@ struct calcVars { /* Calctool variables and options. */ 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 @@ struct calcVars { /* Calctool variables and options. */ 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. */ }; diff --git a/gcalctool/display.c b/gcalctool/display.c index 2f3fa16a..e5b1862b 100644 --- a/gcalctool/display.c +++ b/gcalctool/display.c @@ -46,60 +46,68 @@ static char *make_eng_sci(int *, int); */ 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; - - 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--; + GString *output = g_string_sized_new(dest_length); + const char *c, *d; + int digit_count = -1; + gboolean after_radix = FALSE; + + /* 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; + } + else { + g_string_append_c(output, *c); } - *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; + STRNCPY(dest, output->str, dest_length - 1); + return; + } + + /* 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--; } - *dstp++ = '\0'; - - /* Move from scratch pad to fnum, reversing the character order. */ - srcp = tnum + strlen(tnum) - 1; - dstp = dest; - while (srcp >= tnum) { - *dstp++ = *srcp--; + /* 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); } - *dstp++ = '\0'; - } else { - STRNCPY(dest, src, dest_length - 1); - } - 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); + /* 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 @@ refresh_display(int cursor) } 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; diff --git a/gcalctool/display.h b/gcalctool/display.h index 598cfdb5..c3b321ed 100644 --- a/gcalctool/display.h +++ b/gcalctool/display.h @@ -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); diff --git a/gcalctool/functions.c b/gcalctool/functions.c index 0a6f6724..cb59fecb 100644 --- a/gcalctool/functions.c +++ b/gcalctool/functions.c @@ -205,7 +205,7 @@ peek_previous_state(void) } static int -is_undo_step() +is_undo_step(void) { return(v->h.current != v->h.begin); } @@ -237,7 +237,7 @@ do_accuracy(int value) /* Set display accuracy. */ 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 @@ exp_insert(char *text, int cursor) void -exp_clear() +exp_clear(void) { exp_replace(""); } @@ -396,7 +396,7 @@ exp_replace(char *text) static void -exp_negate() +exp_negate(void) { struct exprm_state *e = get_state(); @@ -414,7 +414,7 @@ exp_negate() static void -exp_inv() +exp_inv(void) { struct exprm_state *e = get_state(); @@ -632,7 +632,7 @@ do_expression(int function, int arg, int cursor) 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 @@ do_percent(void) /* Clear the calculator display and re-initialise. */ void -do_clear() +do_clear(void) { clear_display(TRUE); if (v->error) { @@ -884,7 +884,7 @@ do_clear() /* Clear the calculator display. */ void -do_clear_entry() +do_clear_entry(void) { clear_display(FALSE); } @@ -949,7 +949,7 @@ do_constant(int index) /* Remove the last numeric character typed. */ void -do_backspace() +do_backspace(void) { size_t len; @@ -984,7 +984,7 @@ do_backspace() void -do_delete() +do_delete(void) { /* Not required in ltr mode */ } @@ -1033,7 +1033,7 @@ do_exchange(int index) /* Get exponential number. */ void -do_expno() +do_expno(void) { v->pointed = (strchr(v->display, '.') != NULL); if (!v->new_input) { @@ -1218,7 +1218,7 @@ do_immedfunc(int s[MP_SIZE], int t[MP_SIZE]) void -do_immed() +do_immed(void) { do_immedfunc(v->MPdisp_val, v->MPdisp_val); show_display(v->MPdisp_val); @@ -1226,7 +1226,7 @@ do_immed() void -do_number() +do_number(void) { int offset; @@ -1283,7 +1283,7 @@ do_numtype(enum num_type n) /* Set number display type. */ void -do_paren() +do_paren(void) { ui_set_statusbar("", ""); @@ -1394,7 +1394,7 @@ do_rcl_reg(int reg, int value[MP_SIZE]) void -syntaxdep_show_display() +syntaxdep_show_display(void) { switch (v->syntax) { case NPA: @@ -1412,7 +1412,7 @@ syntaxdep_show_display() void -do_point() /* Handle numeric point. */ +do_point(void) /* Handle numeric point. */ { if (!v->pointed) { if (v->toclear) { @@ -1453,7 +1453,7 @@ do_portionfunc(int num[MP_SIZE]) void -do_portion() +do_portion(void) { do_portionfunc(v->MPdisp_val); show_display(v->MPdisp_val); diff --git a/gcalctool/functions.h b/gcalctool/functions.h index a9415485..d8b0037c 100644 --- a/gcalctool/functions.h +++ b/gcalctool/functions.h @@ -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 perform_redo(void); 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]); diff --git a/gcalctool/get.c b/gcalctool/get.c index f1867ff8..6d73cf82 100644 --- a/gcalctool/get.c +++ b/gcalctool/get.c @@ -225,6 +225,13 @@ get_tsep() } +int +get_tsep_count() +{ + return 3; +} + + void read_resources() /* Read all possible resources from the database. */ { diff --git a/gcalctool/get.h b/gcalctool/get.h index e61a0bbc..ecfe8aca 100644 --- a/gcalctool/get.h +++ b/gcalctool/get.h @@ -58,5 +58,6 @@ int get_boolean_resource(char *key, int *value); const char *get_radix(); const char *get_tsep(); +int get_tsep_count(); #endif /* GET_H */ diff --git a/gcalctool/gtk.c b/gcalctool/gtk.c index d2f7ef61..394b0194 100644 --- a/gcalctool/gtk.c +++ b/gcalctool/gtk.c @@ -215,20 +215,20 @@ static struct button_widget button_widgets[] = { { 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 @@ ui_set_numeric_mode(enum base_type mode) void -ui_set_show_thousands_seperator(gboolean visible) +ui_set_show_thousands_separator(gboolean visible) { GtkWidget *menu; @@ -771,7 +771,7 @@ ui_set_mode(enum mode_type mode) 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 @@ ui_set_statusbar(gchar *text, const gchar *imagename) } 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 @@ set_bit_panel() 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 @@ ui_set_display(char *str, int cursor) str = " "; } else { if (v->noparens == 0) { - localize_number(localized, str, MAX_LOCALIZED); + localize_expression(localized, str, MAX_LOCALIZED); str = localized; } } @@ -1600,6 +1595,8 @@ create_constants_model() 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 @@ create_functions_model() 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 @@ show_thousands_separator_cb(GtkWidget *widget) 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 @@ ui_init(int *argc, char ***argv) void -ui_load() +ui_load(void) { int boolval; char *resource, text[MAXLINE]; @@ -2895,7 +2894,7 @@ ui_load() 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 @@ ui_load() } void -ui_start() +ui_start(void) { X->warn_change_mode = TRUE; // FIXME: Load from GConf diff --git a/gcalctool/ui.h b/gcalctool/ui.h index 2b8dde7a..810e6c28 100644 --- a/gcalctool/ui.h +++ b/gcalctool/ui.h @@ -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_inverse_state(gboolean); 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); diff --git a/gcalctool/unittest.c b/gcalctool/unittest.c index 8e3c9db7..7e980138 100644 --- a/gcalctool/unittest.c +++ b/gcalctool/unittest.c @@ -85,7 +85,10 @@ test_parser() 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); -- 2.11.4.GIT