case-map: Use stateless translator in case_map_create_input_translator().
[pspp] / src / data / variable.c
index 8867ba4baed33775545cb1efedd943d668fd8da0..4fdfdcbb65d83609b14f8f29b14bf1ffe6b0741a 100644 (file)
@@ -63,6 +63,7 @@ const GEnumValue align[] =
 
 const GEnumValue measure[] =
   {
+    {MEASURE_UNKNOWN, "unknown", N_("Unknown")},
     {MEASURE_NOMINAL, "nominal", N_("Nominal")},
     {MEASURE_ORDINAL, "ordinal", N_("Ordinal")},
     {MEASURE_SCALE,   "scale", N_("Scale")},
@@ -139,7 +140,7 @@ var_create (const char *name, int width)
   v->leave = var_must_leave (v);
   type = val_type_from_width (width);
   v->alignment = var_default_alignment (type);
-  v->measure = var_default_measure (type);
+  v->measure = var_default_measure_for_type (type);
   v->role = ROLE_INPUT;
   v->display_width = var_default_display_width (width);
   v->print = v->write = var_default_formats (width);
@@ -759,23 +760,20 @@ var_get_label (const struct variable *v)
 
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
-   empty string (after stripping white space), then V's variable label (if any)
-   is removed. */
+   empty string, then V's variable label (if any) is removed. */
 static void
 var_set_label_quiet (struct variable *v, const char *label)
 {
   free (v->label);
   v->label = NULL;
 
-  if (label != NULL && label[strspn (label, CC_SPACES)])
+  if (label != NULL && label[0])
     v->label = xstrdup (label);
 
   ds_destroy (&v->name_and_label);
   ds_init_empty (&v->name_and_label);
 }
 
-
-
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
    empty string (after stripping white space), then V's variable label (if any)
@@ -788,7 +786,6 @@ var_set_label (struct variable *v, const char *label)
   dict_var_changed (v, VAR_TRAIT_LABEL, ov);
 }
 
-
 /* Removes any variable label from V. */
 void
 var_clear_label (struct variable *v)
@@ -809,7 +806,8 @@ var_has_label (const struct variable *v)
 bool
 measure_is_valid (enum measure m)
 {
-  return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
+  return (m == MEASURE_UNKNOWN || m == MEASURE_NOMINAL
+          || m == MEASURE_ORDINAL || m == MEASURE_SCALE);
 }
 
 /* Returns a string version of measurement level M, for display to a user.
@@ -873,9 +871,38 @@ var_set_measure (struct variable *v, enum measure measure)
    used to reset a variable's measurement level to the
    default. */
 enum measure
-var_default_measure (enum val_type type)
+var_default_measure_for_type (enum val_type type)
 {
-  return type == VAL_NUMERIC ? MEASURE_SCALE : MEASURE_NOMINAL;
+  return type == VAL_NUMERIC ? MEASURE_UNKNOWN : MEASURE_NOMINAL;
+}
+
+/* Returns the default measurement level for a variable with the given
+   FORMAT, or MEASURE_UNKNOWN if there is no good default. */
+enum measure
+var_default_measure_for_format (enum fmt_type format)
+{
+  if (format == FMT_DOLLAR)
+    return MEASURE_SCALE;
+
+  switch (fmt_get_category (format))
+    {
+    case FMT_CAT_BASIC:
+    case FMT_CAT_LEGACY:
+    case FMT_CAT_BINARY:
+    case FMT_CAT_HEXADECIMAL:
+      return MEASURE_UNKNOWN;
+
+    case FMT_CAT_CUSTOM:
+    case FMT_CAT_DATE:
+    case FMT_CAT_TIME:
+      return MEASURE_SCALE;
+
+    case FMT_CAT_DATE_COMPONENT:
+    case FMT_CAT_STRING:
+      return MEASURE_NOMINAL;
+    }
+
+  NOT_REACHED ();
 }
 \f
 /* Returns true if M is a valid variable role,