From 3079a93f224dfc9d0fa8638ccba7a2faac900977 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 26 May 2013 16:43:01 -0700 Subject: [PATCH] Avoid letting data fields overflow in output and GUI. When the output for data_out() is too big for the field width, it produces output that consists of just asterisks: ******. This is OK when the output is really going into a fixed-width space, such as the output for PRINT and WRITE. But it is obnoxious if the output is going into PSPP output or the GUI. This commit introduces a new function that typically does not do this, and starts using it in output and the GUI. Bug #35829. Reported by John Darrington. Bug #30731. Reported by lavila . --- src/data/data-out.c | 24 +++++++++++++++++++++++- src/data/data-out.h | 4 +++- src/output/tab.c | 12 ++++++------ src/ui/gui/helper.c | 4 ++-- tests/language/dictionary/weight.at | 4 ++-- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/data/data-out.c b/src/data/data-out.c index d3125955b8..03aa67ff4e 100644 --- a/src/data/data-out.c +++ b/src/data/data-out.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2012, 2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -193,6 +193,28 @@ data_out_pool (const union value *input, const char *encoding, } } +/* Like data_out_pool(), except that for basic numeric formats (F, COMMA, DOT, + COLLAR, PCT, E) and custom currency formats are formatted as wide as + necessary to fully display the selected number of decimal places. */ +char * +data_out_stretchy (const union value *input, const char *encoding, + const struct fmt_spec *format, struct pool *pool) +{ + struct fmt_spec wide_format; + + if (fmt_get_category (format->type) & (FMT_CAT_BASIC | FMT_CAT_CUSTOM)) + { + /* XXX In the common case this wastes memory for 40 bytes of mostly + spaces. */ + wide_format.type = format->type; + wide_format.w = 40; + wide_format.d = format->d; + format = &wide_format; + } + + return data_out_pool (input, encoding, format, pool); +} + char * data_out (const union value *input, const char *encoding, const struct fmt_spec *format) { diff --git a/src/data/data-out.h b/src/data/data-out.h index 7dff441289..b18d067436 100644 --- a/src/data/data-out.h +++ b/src/data/data-out.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2011 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2011, 2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,6 +29,8 @@ char * data_out (const union value *, const char *encoding, const struct fmt_spe char * data_out_pool (const union value *, const char *encoding, const struct fmt_spec *, struct pool *pool); +char *data_out_stretchy (const union value *, const char *encoding, const struct fmt_spec *, struct pool *); + void data_out_recode (const union value *input, const char *input_encoding, const struct fmt_spec *, struct string *output, const char *output_encoding); diff --git a/src/output/tab.c b/src/output/tab.c index f5f1f1f84b..bc4ac534d0 100644 --- a/src/output/tab.c +++ b/src/output/tab.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -379,9 +379,9 @@ tab_value (struct tab_table *table, int c, int r, unsigned char opt, } #endif - contents = data_out_pool (v, var_get_encoding (var), - f != NULL ? f : var_get_print_format (var), - table->container); + contents = data_out_stretchy (v, var_get_encoding (var), + f != NULL ? f : var_get_print_format (var), + table->container); table->cc[c + r * table->cf] = contents; table->ct[c + r * table->cf] = opt; @@ -419,7 +419,7 @@ tab_fixed (struct tab_table *table, int c, int r, unsigned char opt, #endif double_value.f = val; - s = data_out_pool (&double_value, C_ENCODING, &f, table->container); + s = data_out_stretchy (&double_value, C_ENCODING, &f, table->container); table->cc[c + r * table->cf] = s + strspn (s, " "); table->ct[c + r * table->cf] = opt; @@ -461,7 +461,7 @@ tab_double (struct tab_table *table, int c, int r, unsigned char opt, #endif double_value.f = val; - s = data_out_pool (&double_value, C_ENCODING, fmt, table->container); + s = data_out_stretchy (&double_value, C_ENCODING, fmt, table->container); table->cc[c + r * table->cf] = s + strspn (s, " "); table->ct[c + r * table->cf] = opt; } diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index 84df352fdd..7fc951d3cd 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2007, 2009, 2010, 2011, 2012 Free Software Foundation + Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -96,7 +96,7 @@ value_to_text__ (union value v, { gchar *s; - s = data_out (&v, encoding, format); + s = data_out_stretchy (&v, encoding, format, NULL); if (fmt_is_numeric (format->type)) g_strchug (s); else diff --git a/tests/language/dictionary/weight.at b/tests/language/dictionary/weight.at index 40226924d7..a85347e516 100644 --- a/tests/language/dictionary/weight.at +++ b/tests/language/dictionary/weight.at @@ -72,7 +72,7 @@ BVAR,1,6- 10,F5.0 Table: Valid cases = 730; cases with missing value(s) = 0. Variable,Valid N,Missing N,Mean,S.E. Mean,Std Dev,Variance,Kurtosis,S.E. Kurt,Skewness,S.E. Skew,Range,Minimum,Maximum,Sum -AVAR,730,0,31.515,.405,10.937,119.608,2.411,.181,1.345,.090,76.000,18.000,94.000,23006.00 +AVAR,730,0,31.515,.405,10.937,119.608,2.411,.181,1.345,.090,76.000,18.000,94.000,23006.000 Table: AVAR Value Label,Value,Frequency,Percent,Valid Percent,Cum Percent @@ -145,7 +145,7 @@ S.E. Skew,,.090 Range,,76.000 Minimum,,18.000 Maximum,,94.000 -Sum,,23006.00 +Sum,,23006.000 Percentiles,50 (Median),28 ]) AT_CLEANUP -- 2.30.2