From bd981ce2f73ef902f90362913c8cac741f86e1e7 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 23 Jun 2013 12:07:56 +0200 Subject: [PATCH] variable.c: New function var_set_width_and_formats Added a new function to set the width and formats of a variable as an atomic operation. This is necessary to allow values to re-interpreted without going through an intermediate format and thereby perhaps loosing information. Closes bug #39252 --- src/data/variable.c | 40 ++++++++++++++++++++++++++++------ src/data/variable.h | 6 ++++- src/ui/gui/psppire-var-sheet.c | 4 ++-- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/data/variable.c b/src/data/variable.c index e981ba661f..720def8e6c 100644 --- a/src/data/variable.c +++ b/src/data/variable.c @@ -77,6 +77,8 @@ struct variable }; +static void var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print); +static void var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write); static bool var_set_label_quiet (struct variable *v, const char *label, bool issue_warning); static void var_set_name_quiet (struct variable *v, const char *name); @@ -242,18 +244,13 @@ var_get_width (const struct variable *v) return v->width; } -/* Changes the width of V to NEW_WIDTH. - This function should be used cautiously. */ void -var_set_width (struct variable *v, int new_width) +var_set_width_and_formats (struct variable *v, int new_width, + const struct fmt_spec *print, const struct fmt_spec *write) { struct variable *ov; - const int old_width = v->width; unsigned int traits = 0; - if (old_width == new_width) - return; - ov = var_clone (v); if (var_has_missing_values (v)) @@ -288,9 +285,38 @@ var_set_width (struct variable *v, int new_width) v->width = new_width; traits |= VAR_TRAIT_WIDTH; + + if (print) + { + var_set_print_format_quiet (v, print); + traits |= VAR_TRAIT_PRINT_FORMAT; + } + + if (write) + { + var_set_write_format_quiet (v, write); + traits |= VAR_TRAIT_WRITE_FORMAT; + } + dict_var_changed (v, traits, ov); } +/* Changes the width of V to NEW_WIDTH. + This function should be used cautiously. */ +void +var_set_width (struct variable *v, int new_width) +{ + const int old_width = v->width; + + if (old_width == new_width) + return; + + var_set_width_and_formats (v, new_width, NULL, NULL); +} + + + + /* Returns true if variable V is numeric, false otherwise. */ bool var_is_numeric (const struct variable *v) diff --git a/src/data/variable.h b/src/data/variable.h index ee8ee94ac2..a62902882f 100644 --- a/src/data/variable.h +++ b/src/data/variable.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2009, 2010, 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 @@ -64,10 +64,14 @@ unsigned hash_var_ptr_by_name (const void *, const void *); int compare_var_ptrs_by_dict_index (const void *, const void *, const void *); +struct fmt_spec; + /* Types and widths of values associated with a variable. */ enum val_type var_get_type (const struct variable *); int var_get_width (const struct variable *); void var_set_width (struct variable *, int width); +void var_set_width_and_formats (struct variable *v, int new_width, + const struct fmt_spec *print, const struct fmt_spec *write); bool var_is_numeric (const struct variable *); bool var_is_alpha (const struct variable *); diff --git a/src/ui/gui/psppire-var-sheet.c b/src/ui/gui/psppire-var-sheet.c index 9d23cb9656..d62a97e1fd 100644 --- a/src/ui/gui/psppire-var-sheet.c +++ b/src/ui/gui/psppire-var-sheet.c @@ -453,8 +453,8 @@ on_type_click (PsppireCellRendererButton *cell, format = *var_get_print_format (var); psppire_var_type_dialog_run (GTK_WINDOW (toplevel), &format); - var_set_width (var, fmt_var_width (&format)); - var_set_both_formats (var, &format); + + var_set_width_and_formats (var, fmt_var_width (&format), &format, &format); } static void -- 2.30.2