From 04bbac9e357101873ccd9b5555e14f946d81b5ce Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 23 Jul 2007 04:44:11 +0000 Subject: [PATCH] * variable.c (var_set_width): Use new var_set_width function. * missing-values.c (mv_n_values): Drop assertion, which was not needed. * format.c (fmt_default_for_width): New function. (fmt_resize): New function. --- src/data/ChangeLog | 10 ++++++++++ src/data/format.c | 32 ++++++++++++++++++++++++++++++++ src/data/format.h | 1 + src/data/missing-values.c | 4 ++-- src/data/variable.c | 15 ++------------- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/data/ChangeLog b/src/data/ChangeLog index 7d8ef4dd..9225fa00 100644 --- a/src/data/ChangeLog +++ b/src/data/ChangeLog @@ -1,3 +1,13 @@ +2007-07-22 Ben Pfaff + + * variable.c (var_set_width): Use new var_set_width function. + + * missing-values.c (mv_n_values): Drop assertion, which was not + needed. + + * format.c (fmt_default_for_width): New function. + (fmt_resize): New function. + 2007-07-18 John Darrington * datasheet.c (datasheet_delete_columns): Added assertion to check diff --git a/src/data/format.c b/src/data/format.c index 069ec3af..54c62817 100644 --- a/src/data/format.c +++ b/src/data/format.c @@ -207,6 +207,16 @@ fmt_for_output_from_input (const struct fmt_spec *input) return output; } +/* Returns the default format for the given WIDTH: F8.2 format + for a numeric value, A format for a string value. */ +struct fmt_spec +fmt_default_for_width (int width) +{ + return (width == 0 + ? fmt_for_output (FMT_F, 8, 2) + : fmt_for_output (FMT_A, width, 0)); +} + /* Checks whether SPEC is valid as an input format (if FOR_INPUT) or an output format (otherwise) and returns nonzero if so. Otherwise, emits an error message and returns zero. */ @@ -368,6 +378,28 @@ fmt_equal (const struct fmt_spec *a, const struct fmt_spec *b) { return a->type == b->type && a->w == b->w && a->d == b->d; } + +/* Adjusts FMT to be valid for a value of the given WIDTH. */ +void +fmt_resize (struct fmt_spec *fmt, int width) +{ + if ((width > 0) != fmt_is_string (fmt->type)) + { + /* Changed from numeric to string or vice versa. Set to + default format for new width. */ + *fmt = fmt_default_for_width (width); + } + else if (width > 0) + { + /* Changed width of string. Preserve format type, adjust + width. */ + fmt->w = fmt->type == FMT_AHEX ? width * 2 : width; + } + else + { + /* Still numeric. */ + } +} /* Describes a display format. */ struct fmt_desc diff --git a/src/data/format.h b/src/data/format.h index f7c0ec07..fbaf0625 100644 --- a/src/data/format.h +++ b/src/data/format.h @@ -87,6 +87,7 @@ bool fmt_check_width_compat (const struct fmt_spec *, int var_width); int fmt_var_width (const struct fmt_spec *); char *fmt_to_string (const struct fmt_spec *, char s[FMT_STRING_LEN_MAX + 1]); bool fmt_equal (const struct fmt_spec *, const struct fmt_spec *); +void fmt_resize (struct fmt_spec *, int new_width); /* Format types. */ const char *fmt_name (enum fmt_type) PURE_FUNCTION; diff --git a/src/data/missing-values.c b/src/data/missing-values.c index 725b58c6..c4d40baa 100644 --- a/src/data/missing-values.c +++ b/src/data/missing-values.c @@ -208,11 +208,11 @@ mv_replace_value (struct missing_values *mv, const union value *v, int idx) } - +/* Returns the number of individual (not part of a range) missing + values in MV. */ int mv_n_values (const struct missing_values *mv) { - assert(mv_has_value(mv)); return mv->type & 3; } diff --git a/src/data/variable.c b/src/data/variable.c index a10a8ade..22159d87 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -351,7 +351,6 @@ void var_set_width (struct variable *v, int new_width) { const int old_width = v->width; - enum var_type new_type = var_type_from_width (new_width); if (mv_is_resizable (&v->miss, new_width)) mv_resize (&v->miss, new_width); @@ -369,18 +368,8 @@ var_set_width (struct variable *v, int new_width) } } - if (var_get_type (v) != new_type) - { - v->print = (new_type == VAR_NUMERIC - ? fmt_for_output (FMT_F, 8, 2) - : fmt_for_output (FMT_A, new_width, 0)); - v->write = v->print; - } - else if (new_type == VAR_STRING) - { - v->print.w = v->print.type == FMT_AHEX ? new_width * 2 : new_width; - v->write.w = v->write.type == FMT_AHEX ? new_width * 2 : new_width; - } + fmt_resize (&v->print, new_width); + fmt_resize (&v->write, new_width); v->width = new_width; -- 2.30.2