* variable.c (var_set_width): Use new var_set_width function.
[pspp-builds.git] / src / data / variable.c
index aa65606f02652d826b976a7f0a423cfb3b16b3f9..22159d8765691747af4d29a990c9324727ca5fd8 100644 (file)
@@ -29,6 +29,7 @@
 #include "value-labels.h"
 #include "vardict.h"
 
+#include <libpspp/misc.h>
 #include <libpspp/alloc.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
@@ -349,7 +350,7 @@ var_get_width (const struct variable *v)
 void
 var_set_width (struct variable *v, int new_width)
 {
-  enum var_type new_type = var_type_from_width (new_width);
+  const int old_width = v->width;
 
   if (mv_is_resizable (&v->miss, new_width))
     mv_resize (&v->miss, new_width);
@@ -367,21 +368,19 @@ 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;
 
+  {
+    const int old_val_count = value_cnt_from_width (old_width);
+    const int new_val_count = value_cnt_from_width (new_width);
+
+    if ( old_val_count != new_val_count)
+        dict_var_resized (v, new_val_count - old_val_count);
+  }
+
   dict_var_changed (v);
 }