Need to include <sys/stat.h>. Bug #21516.
[pspp] / src / data / variable.c
index 59f452217c1178ea95ac79264f4749e6256f1f51..4a2e44057c70c23aeff99d17ee206cc205ad5105 100644 (file)
 #include "vardict.h"
 
 #include <libpspp/misc.h>
-#include <libpspp/alloc.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
 #include <libpspp/hash.h>
 #include <libpspp/message.h>
 #include <libpspp/str.h>
 
+#include "xalloc.h"
+
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
@@ -112,16 +113,15 @@ var_create (const char *name, int width)
     {
       v->print = fmt_for_output (FMT_F, 8, 2);
       v->alignment = ALIGN_RIGHT;
-      v->display_width = 8;
       v->measure = MEASURE_SCALE;
     }
   else
     {
       v->print = fmt_for_output (FMT_A, var_get_width (v), 0);
       v->alignment = ALIGN_LEFT;
-      v->display_width = 8;
       v->measure = MEASURE_NOMINAL;
     }
+  v->display_width = var_default_display_width (width);
   v->write = v->print;
   v->val_labs = NULL;
   v->label = NULL;
@@ -174,6 +174,7 @@ var_destroy (struct variable *v)
     {
       assert (!var_has_vardict (v));
       cat_stored_values_destroy (v->obs_vals);
+      var_clear_short_names (v);
       var_clear_aux (v);
       val_labs_destroy (v->val_labs);
       var_clear_label (v);
@@ -722,9 +723,6 @@ var_get_display_width (const struct variable *v)
   return v->display_width;
 }
 
-
-
-
 /* Sets V's display width to DISPLAY_WIDTH. */
 void
 var_set_display_width (struct variable *v, int display_width)
@@ -732,6 +730,15 @@ var_set_display_width (struct variable *v, int display_width)
   v->display_width = display_width;
   dict_var_changed (v);
 }
+
+/* Returns the default display width for a variable of the given
+   WIDTH, as set by var_create.  The return value can be used to
+   reset a variable's display width to the default. */
+int
+var_default_display_width (int width)
+{
+  return width == 0 ? 8 : MIN (width, 32);
+}
 \f
 /* Returns true if A is a valid alignment,
    false otherwise. */
@@ -835,7 +842,6 @@ var_set_short_name (struct variable *var, size_t idx, const char *short_name)
         {
           size_t old_cnt = var->short_name_cnt;
           size_t i;
-          
           var->short_name_cnt = MAX (idx * 2, 1);
           var->short_names = xnrealloc (var->short_names, var->short_name_cnt,
                                         sizeof *var->short_names);
@@ -854,9 +860,10 @@ void
 var_clear_short_names (struct variable *v)
 {
   size_t i;
-  
+
   for (i = 0; i < v->short_name_cnt; i++)
     free (v->short_names[i]);
+  free (v->short_names);
   v->short_names = NULL;
   v->short_name_cnt = 0;
 }